update logic upload file and get centerstaff
This commit is contained in:
@@ -138,6 +138,124 @@ export class UploadedFileSchema extends PothosSchema {
|
||||
return uploadedFile;
|
||||
},
|
||||
}),
|
||||
|
||||
multipleUpload: t.prismaField({
|
||||
type: [this.uploadedFile()],
|
||||
args: {
|
||||
userId: t.arg({
|
||||
type: 'String',
|
||||
required: true,
|
||||
}),
|
||||
files: t.arg({
|
||||
type: ['Upload'],
|
||||
required: true,
|
||||
}),
|
||||
fileType: t.arg({
|
||||
type: UploadedFileType,
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
resolve: async (query, root, args) => {
|
||||
const user = await this.prisma.user.findUnique({
|
||||
where: {
|
||||
id: args.userId,
|
||||
},
|
||||
});
|
||||
if (!user) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
const uploadedFiles = await Promise.all(
|
||||
args.files.map((file) =>
|
||||
this.minioService.uploadFile(file, 'files'),
|
||||
),
|
||||
);
|
||||
// get file urls
|
||||
const fileUrls = await Promise.all(
|
||||
uploadedFiles.map((file) =>
|
||||
this.minioService.getFileUrl(file.filename, 'files'),
|
||||
),
|
||||
);
|
||||
// map uploadedFiles to db
|
||||
const dbFiles = uploadedFiles.map((file, index) => ({
|
||||
userId: user.id,
|
||||
fileName: file.filename,
|
||||
type: file.mimetype,
|
||||
fileType: args.fileType,
|
||||
fileUrl: fileUrls[index],
|
||||
uploadedAt: new Date(),
|
||||
}));
|
||||
// create files in db
|
||||
const createdFiles =
|
||||
await this.prisma.uploadedFile.createManyAndReturn({
|
||||
data: dbFiles,
|
||||
});
|
||||
return createdFiles;
|
||||
},
|
||||
}),
|
||||
|
||||
deleteUploadedFile: t.prismaField({
|
||||
type: this.uploadedFile(),
|
||||
args: {
|
||||
id: t.arg({
|
||||
type: 'String',
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
resolve: async (query, root, args) => {
|
||||
const file = await this.prisma.uploadedFile.findUnique({
|
||||
where: {
|
||||
id: args.id,
|
||||
},
|
||||
});
|
||||
if (!file) {
|
||||
throw new Error('File not found');
|
||||
}
|
||||
await this.minioService.deleteFile(file.fileName, 'files');
|
||||
await this.prisma.uploadedFile.delete({
|
||||
where: {
|
||||
id: file.id,
|
||||
},
|
||||
});
|
||||
return file;
|
||||
},
|
||||
}),
|
||||
|
||||
deleteUploadedFiles: t.prismaField({
|
||||
type: [this.uploadedFile()],
|
||||
args: {
|
||||
ids: t.arg({
|
||||
type: ['String'],
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
resolve: async (query, root, args) => {
|
||||
const files = await this.prisma.uploadedFile.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: args.ids,
|
||||
},
|
||||
},
|
||||
});
|
||||
const fileUrls = await Promise.all(
|
||||
files.map((file) =>
|
||||
this.minioService.getFileUrl(file.fileName, 'files'),
|
||||
),
|
||||
);
|
||||
await this.prisma.uploadedFile.deleteMany({
|
||||
where: {
|
||||
id: {
|
||||
in: args.ids,
|
||||
},
|
||||
},
|
||||
});
|
||||
await Promise.all(
|
||||
files.map((file, index) =>
|
||||
this.minioService.deleteFile(file.fileName, 'files'),
|
||||
),
|
||||
);
|
||||
return files;
|
||||
},
|
||||
}),
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user