@memberjunction/server 0.9.238 → 0.9.240

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,104 @@
1
+ import { Metadata } from '@memberjunction/core';
2
+ import { FileEntity, FileStorageProviderEntity } from '@memberjunction/core-entities';
3
+ import {
4
+ AppContext,
5
+ Arg,
6
+ Ctx,
7
+ Field,
8
+ FieldResolver,
9
+ InputType,
10
+ Int,
11
+ Mutation,
12
+ ObjectType,
13
+ PubSub,
14
+ PubSubEngine,
15
+ Resolver,
16
+ Root,
17
+ } from '@memberjunction/server';
18
+ import { createDownloadUrl, createUploadUrl, deleteObject } from '@memberjunction/storage';
19
+ import { CreateFileInput, FileResolver as FileResolverBase, File_ } from '../generated/generated';
20
+
21
+ @InputType()
22
+ export class CreateUploadURLInput {
23
+ @Field(() => Int)
24
+ FileID: number;
25
+ }
26
+
27
+ @ObjectType()
28
+ export class CreateFilePayload {
29
+ @Field(() => File_)
30
+ File: File_;
31
+ @Field(() => String)
32
+ UploadUrl: string;
33
+ }
34
+
35
+ @ObjectType()
36
+ export class FileExt extends File_ {
37
+ @Field(() => String)
38
+ DownloadUrl: string;
39
+ }
40
+
41
+ @Resolver(File_)
42
+ export class FileResolver extends FileResolverBase {
43
+ @Mutation(() => CreateFilePayload)
44
+ async CreateFile(
45
+ @Arg('input', () => CreateFileInput) input: CreateFileInput,
46
+ @Ctx() { dataSource, userPayload }: AppContext,
47
+ @PubSub() pubSub: PubSubEngine
48
+ ) {
49
+ const md = new Metadata();
50
+ const userInfo = this.GetUserFromPayload(userPayload);
51
+ const providerEntity = await md.GetEntityObject<FileStorageProviderEntity>('File Storage Providers', userInfo);
52
+
53
+ const fileRecord = (await super.CreateFile({ ...input, Status: 'Pending' }, { dataSource, userPayload }, pubSub)) as File_;
54
+
55
+ // If there's a problem creating the file record, the base resolver will return null
56
+ if (!fileRecord) {
57
+ return null;
58
+ }
59
+
60
+ // Create the upload URL and get the record updates (provider key, content type, etc)
61
+ const { updatedInput, UploadUrl } = await createUploadUrl(providerEntity, fileRecord);
62
+
63
+ // Save the file record with the updated input
64
+ const fileEntity = <FileEntity>await new Metadata().GetEntityObject('Files', userInfo);
65
+ fileEntity.LoadFromData(input);
66
+ fileEntity.SetMany(updatedInput);
67
+ await fileEntity.Save();
68
+ const File = fileEntity.GetAll();
69
+
70
+ return { File, UploadUrl };
71
+ }
72
+
73
+ @FieldResolver(() => String)
74
+ async DownloadUrl(@Root() file: File_, @Ctx() { userPayload }: AppContext) {
75
+ const md = new Metadata();
76
+ const providerEntity = await md.GetEntityObject<FileStorageProviderEntity>(
77
+ 'File Storage Providers',
78
+ this.GetUserFromPayload(userPayload)
79
+ );
80
+
81
+ const url = await createDownloadUrl(providerEntity, file.ProviderKey ?? file.ID);
82
+
83
+ return url;
84
+ }
85
+
86
+ @Mutation(() => File_)
87
+ async DeleteFile(@Arg('ID', () => Int) ID: number, @Ctx() { dataSource, userPayload }: AppContext, @PubSub() pubSub: PubSubEngine) {
88
+ const md = new Metadata();
89
+ const userInfo = this.GetUserFromPayload(userPayload);
90
+
91
+ const entityObject = <FileEntity>await md.GetEntityObject('Files', userInfo);
92
+ await entityObject.Load(ID);
93
+ if (!entityObject) {
94
+ return null;
95
+ }
96
+
97
+ if (entityObject.Status === 'Uploaded') {
98
+ const providerEntity = await md.GetEntityObject<FileStorageProviderEntity>('File Storage Providers', userInfo);
99
+ await deleteObject(providerEntity, entityObject.ProviderKey ?? entityObject.ID);
100
+ }
101
+
102
+ return super.DeleteFile(ID, { dataSource, userPayload }, pubSub);
103
+ }
104
+ }