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