refactor: update customer branch file upload endpoint

This commit is contained in:
Methapon Metanipat 2024-08-27 09:00:51 +07:00
parent 2d056d3b56
commit 9c8786bf62

View file

@ -553,16 +553,15 @@ export class CustomerAttachmentController extends Controller {
stream.on("error", () => reject(new Error("MinIO error.")));
});
return await Promise.all(
list.map(async (v) => ({
name: v.split("/").at(-1) as string,
url: await minio.presignedGetObject(MINIO_BUCKET, v, 12 * 60 * 60),
})),
);
return list.map((v) => v.split("/").at(-1) as string);
}
@Post()
async addAttachment(@Path() branchId: string, @Body() payload: { file: string[] }) {
@Put("{filename}")
async addAttachment(
@Request() req: RequestWithUser,
@Path() branchId: string,
@Path() filename: string,
) {
const record = await prisma.customerBranch.findFirst({
where: { id: branchId },
});
@ -575,24 +574,17 @@ export class CustomerAttachmentController extends Controller {
);
}
return await Promise.all(
payload.file.map(async (v) => ({
name: v,
url: await minio.presignedGetObject(
MINIO_BUCKET,
`${attachmentLocation(record.customerId, branchId)}/${v}`,
),
uploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
`${attachmentLocation(record.customerId, branchId)}/${v}`,
12 * 60 * 60,
),
})),
return req.res?.redirect(
await minio.presignedPutObject(
MINIO_BUCKET,
`${attachmentLocation(record.customerId, branchId)}/${filename}`,
12 * 60 * 60,
),
);
}
@Delete()
async deleteAttachment(@Path() branchId: string, @Body() payload: { file: string[] }) {
@Delete("{filename}")
async deleteAttachment(@Path() branchId: string, @Path() filename: string) {
const record = await prisma.customerBranch.findFirst({
where: { id: branchId },
});
@ -605,16 +597,10 @@ export class CustomerAttachmentController extends Controller {
);
}
await Promise.all(
payload.file.map(async (v) => {
await minio.removeObject(
MINIO_BUCKET,
`${attachmentLocation(record.customerId, branchId)}/${v}`,
{
forceDelete: true,
},
);
}),
await minio.removeObject(
MINIO_BUCKET,
`${attachmentLocation(record.customerId, branchId)}/${filename}`,
{ forceDelete: true },
);
}
}