@memberjunction/server 0.9.247 → 0.9.250

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,38 @@
1
+ import { EntityPermissionType, Metadata } from '@memberjunction/core';
2
+ import { FileCategoryEntity, FileEntity } from '@memberjunction/core-entities';
3
+ import { AppContext, Arg, Ctx, Int, Mutation } from '@memberjunction/server';
4
+ import { mj_core_schema } from '../config';
5
+ import { FileCategoryResolver as FileCategoryResolverBase, FileCategory_ } from '../generated/generated';
6
+
7
+ export class FileResolver extends FileCategoryResolverBase {
8
+ @Mutation(() => FileCategory_)
9
+ async DeleteFileCategory(@Arg('ID', () => Int) ID: number, @Ctx() { dataSource, userPayload }: AppContext) {
10
+ if (!(await this.BeforeDelete(dataSource, ID))) {
11
+ return null;
12
+ }
13
+
14
+ const md = await new Metadata();
15
+ const user = this.GetUserFromPayload(userPayload);
16
+ const fileEntity = await md.GetEntityObject<FileEntity>('Files', user);
17
+ const fileCategoryEntity = await md.GetEntityObject<FileCategoryEntity>('File Categories', user);
18
+
19
+ fileEntity.CheckPermissions(EntityPermissionType.Update, true);
20
+ fileCategoryEntity.CheckPermissions(EntityPermissionType.Delete, true);
21
+
22
+ await fileCategoryEntity.Load(ID);
23
+ const returnValue = fileCategoryEntity.GetAll();
24
+
25
+ // Any files using the deleted category fall back to its parent
26
+ await dataSource.transaction(async () => {
27
+ const sSQL = `UPDATE [${mj_core_schema}].[File]
28
+ SET [CategoryID]=${fileCategoryEntity.ParentID}
29
+ WHERE [CategoryID]=${fileCategoryEntity.ID}`;
30
+
31
+ await dataSource.query(sSQL);
32
+ await fileCategoryEntity.Delete();
33
+ });
34
+
35
+ await this.AfterDelete(dataSource, ID); // fire event
36
+ return returnValue;
37
+ }
38
+ }
@@ -1,4 +1,4 @@
1
- import { Metadata } from '@memberjunction/core';
1
+ import { EntityPermissionType, Metadata } from '@memberjunction/core';
2
2
  import { FileEntity, FileStorageProviderEntity } from '@memberjunction/core-entities';
3
3
  import {
4
4
  AppContext,
@@ -47,8 +47,10 @@ export class FileResolver extends FileResolverBase {
47
47
  @PubSub() pubSub: PubSubEngine
48
48
  ) {
49
49
  const md = new Metadata();
50
- const userInfo = this.GetUserFromPayload(userPayload);
51
- const providerEntity = await md.GetEntityObject<FileStorageProviderEntity>('File Storage Providers', userInfo);
50
+ const user = this.GetUserFromPayload(userPayload);
51
+ const fileEntity = await md.GetEntityObject<FileEntity>('Files', user);
52
+ const providerEntity = await md.GetEntityObject<FileStorageProviderEntity>('File Storage Providers', user);
53
+ fileEntity.CheckPermissions(EntityPermissionType.Create, true);
52
54
 
53
55
  const fileRecord = (await super.CreateFile({ ...input, Status: 'Pending' }, { dataSource, userPayload }, pubSub)) as File_;
54
56
 
@@ -61,7 +63,6 @@ export class FileResolver extends FileResolverBase {
61
63
  const { updatedInput, UploadUrl } = await createUploadUrl(providerEntity, fileRecord);
62
64
 
63
65
  // Save the file record with the updated input
64
- const fileEntity = <FileEntity>await new Metadata().GetEntityObject('Files', userInfo);
65
66
  fileEntity.LoadFromData(input);
66
67
  fileEntity.SetMany(updatedInput);
67
68
  await fileEntity.Save();
@@ -73,10 +74,11 @@ export class FileResolver extends FileResolverBase {
73
74
  @FieldResolver(() => String)
74
75
  async DownloadUrl(@Root() file: File_, @Ctx() { userPayload }: AppContext) {
75
76
  const md = new Metadata();
76
- const providerEntity = await md.GetEntityObject<FileStorageProviderEntity>(
77
- 'File Storage Providers',
78
- this.GetUserFromPayload(userPayload)
79
- );
77
+ const user = this.GetUserFromPayload(userPayload);
78
+ const fileEntity = await md.GetEntityObject<FileEntity>('Files', user);
79
+ fileEntity.CheckPermissions(EntityPermissionType.Read, true);
80
+
81
+ const providerEntity = await md.GetEntityObject<FileStorageProviderEntity>('File Storage Providers', user);
80
82
  await providerEntity.Load(file.ProviderID);
81
83
 
82
84
  const url = await createDownloadUrl(providerEntity, file.ProviderKey ?? file.ID);
@@ -89,13 +91,15 @@ export class FileResolver extends FileResolverBase {
89
91
  const md = new Metadata();
90
92
  const userInfo = this.GetUserFromPayload(userPayload);
91
93
 
92
- const fileEntity = <FileEntity>await md.GetEntityObject('Files', userInfo);
94
+ const fileEntity = await md.GetEntityObject<FileEntity>('Files', userInfo);
93
95
  await fileEntity.Load(ID);
94
96
  if (!fileEntity) {
95
97
  return null;
96
98
  }
99
+ fileEntity.CheckPermissions(EntityPermissionType.Delete, true);
97
100
 
98
- if (fileEntity.Status === 'Uploaded') {
101
+ // Only delete the object from the provider if it's actually been uploaded
102
+ if (fileEntity.Status === 'Uploaded') {
99
103
  const providerEntity = await md.GetEntityObject<FileStorageProviderEntity>('File Storage Providers', userInfo);
100
104
  await providerEntity.Load(fileEntity.ProviderID);
101
105
  await deleteObject(providerEntity, fileEntity.ProviderKey ?? fileEntity.ID);