refactor source code

This commit is contained in:
2024-10-29 17:42:54 +07:00
parent 3b23d9e0b7
commit 152bb50da8
83 changed files with 8473 additions and 7577 deletions

View File

@@ -1,6 +1,6 @@
import { Module, Global } from '@nestjs/common';
import { UploadedFileSchema } from './uploadedfile.schema';
import { MinioModule } from '../Minio/minio.module';
import { Module, Global } from '@nestjs/common'
import { UploadedFileSchema } from './uploadedfile.schema'
import { MinioModule } from '../Minio/minio.module'
@Global()
@Module({
imports: [MinioModule],

View File

@@ -1,14 +1,14 @@
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common'
import {
Pothos,
PothosRef,
PothosSchema,
SchemaBuilderToken,
} from '@smatch-corp/nestjs-pothos';
import { Builder } from '../Graphql/graphql.builder';
import { PrismaService } from '../Prisma/prisma.service';
import { MinioService } from 'src/Minio/minio.service';
import { UploadedFileType } from '@prisma/client';
} from '@smatch-corp/nestjs-pothos'
import { Builder } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service'
import { MinioService } from 'src/Minio/minio.service'
import { UploadedFileType } from '@prisma/client'
@Injectable()
export class UploadedFileSchema extends PothosSchema {
constructor(
@@ -16,7 +16,7 @@ export class UploadedFileSchema extends PothosSchema {
private readonly prisma: PrismaService,
private readonly minioService: MinioService,
) {
super();
super()
}
@PothosRef()
@@ -62,7 +62,7 @@ export class UploadedFileSchema extends PothosSchema {
description: 'The workshop that the file belongs to.',
}),
}),
});
})
}
@Pothos()
@@ -77,16 +77,16 @@ export class UploadedFileSchema extends PothosSchema {
const file = await this.prisma.uploadedFile.findUnique({
...query,
where: args.where,
});
})
if (!file) {
throw new Error('File not found');
throw new Error('File not found')
}
const fileUrl = await this.minioService.getFileUrl(file.id, 'files');
const fileUrl = await this.minioService.getFileUrl(file.id, 'files')
if (!fileUrl) {
throw new Error('Cannot retrieve file url');
throw new Error('Cannot retrieve file url')
}
file.fileUrl = fileUrl;
return file;
file.fileUrl = fileUrl
return file
},
}),
uploadedFiles: t.prismaField({
@@ -101,17 +101,17 @@ export class UploadedFileSchema extends PothosSchema {
take: args.take ?? undefined,
orderBy: args.orderBy ?? undefined,
where: args.filter ?? undefined,
});
})
const fileUrls = await Promise.all(
files.map((file) => this.minioService.getFileUrl(file.id, 'files')),
);
)
return files.map((file, index) => ({
...file,
fileUrl: fileUrls[index] ?? '',
}));
}))
},
}),
}));
}))
// Mutations section
this.builder.mutationFields((t) => ({
@@ -137,18 +137,18 @@ export class UploadedFileSchema extends PothosSchema {
where: {
id: args.userId,
},
});
})
if (!user) {
throw new Error('User not found');
throw new Error('User not found')
}
const { filename, mimetype, actualFileName } =
await this.minioService.uploadFile(args.file, 'files');
await this.minioService.uploadFile(args.file, 'files')
if (!mimetype) {
throw new Error('File type not supported');
throw new Error('File type not supported')
}
const fileUrl = await this.minioService.getFileUrl(filename, 'files');
const fileUrl = await this.minioService.getFileUrl(filename, 'files')
if (!fileUrl) {
throw new Error('Cannot retrieve file url, please try again later');
throw new Error('Cannot retrieve file url, please try again later')
}
const uploadedFile = await this.prisma.uploadedFile.create({
data: {
@@ -160,8 +160,8 @@ export class UploadedFileSchema extends PothosSchema {
fileUrl: fileUrl ?? '',
uploadedAt: new Date(),
},
});
return uploadedFile;
})
return uploadedFile
},
}),
@@ -187,21 +187,21 @@ export class UploadedFileSchema extends PothosSchema {
where: {
id: args.userId,
},
});
})
if (!user) {
throw new Error('User not found');
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,
@@ -211,13 +211,13 @@ export class UploadedFileSchema extends PothosSchema {
actualFileName: file.actualFileName,
fileUrl: fileUrls[index] ?? '',
uploadedAt: new Date(),
}));
}))
// create files in db
const createdFiles =
await this.prisma.uploadedFile.createManyAndReturn({
data: dbFiles,
});
return createdFiles;
})
return createdFiles
},
}),
@@ -235,17 +235,17 @@ export class UploadedFileSchema extends PothosSchema {
where: {
id: args.id,
},
});
})
if (!file) {
throw new Error('File not found');
throw new Error('File not found')
}
await this.minioService.deleteFile(file.fileName, 'files');
await this.minioService.deleteFile(file.fileName, 'files')
await this.prisma.uploadedFile.delete({
where: {
id: file.id,
},
});
return file;
})
return file
},
}),
@@ -266,22 +266,22 @@ export class UploadedFileSchema extends PothosSchema {
in: args.ids,
},
},
});
})
await this.prisma.uploadedFile.deleteMany({
where: {
id: {
in: args.ids,
},
},
});
})
await Promise.all(
files.map((file) =>
this.minioService.deleteFile(file.fileName, 'files'),
),
);
return files;
)
return files
},
}),
}));
}))
}
}