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,
url: await minio.presignedGetObject(
MINIO_BUCKET, MINIO_BUCKET,
`${attachmentLocation(record.customerId, branchId)}/${v}`, `${attachmentLocation(record.customerId, branchId)}/${filename}`,
),
uploadUrl: await minio.presignedPutObject(
MINIO_BUCKET,
`${attachmentLocation(record.customerId, branchId)}/${v}`,
12 * 60 * 60, 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(
payload.file.map(async (v) => {
await minio.removeObject( await minio.removeObject(
MINIO_BUCKET, MINIO_BUCKET,
`${attachmentLocation(record.customerId, branchId)}/${v}`, `${attachmentLocation(record.customerId, branchId)}/${filename}`,
{ { forceDelete: true },
forceDelete: true,
},
);
}),
); );
} }
} }