feat: upload and select multiple image
This commit is contained in:
parent
5a50ab209d
commit
ed832148fc
4 changed files with 78 additions and 24 deletions
|
|
@ -0,0 +1,2 @@
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "Customer" ADD COLUMN "selectedImage" TEXT;
|
||||||
|
|
@ -418,6 +418,8 @@ model Customer {
|
||||||
registeredBranchId String?
|
registeredBranchId String?
|
||||||
registeredBranch Branch? @relation(fields: [registeredBranchId], references: [id])
|
registeredBranch Branch? @relation(fields: [registeredBranchId], references: [id])
|
||||||
|
|
||||||
|
selectedImage String?
|
||||||
|
|
||||||
createdAt DateTime @default(now())
|
createdAt DateTime @default(now())
|
||||||
createdBy User? @relation(name: "CustomerCreatedByUser", fields: [createdByUserId], references: [id], onDelete: SetNull)
|
createdBy User? @relation(name: "CustomerCreatedByUser", fields: [createdByUserId], references: [id], onDelete: SetNull)
|
||||||
createdByUserId String?
|
createdByUserId String?
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,13 @@ import {
|
||||||
} from "tsoa";
|
} from "tsoa";
|
||||||
import { RequestWithUser } from "../interfaces/user";
|
import { RequestWithUser } from "../interfaces/user";
|
||||||
import prisma from "../db";
|
import prisma from "../db";
|
||||||
import minio, { deleteFolder, presignedGetObjectIfExist } from "../services/minio";
|
import minio, { deleteFolder } from "../services/minio";
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
import { isSystem } from "../utils/keycloak";
|
import { isSystem } from "../utils/keycloak";
|
||||||
import { branchRelationPermInclude, createPermCheck } from "../services/permission";
|
import { branchRelationPermInclude, createPermCheck } from "../services/permission";
|
||||||
import { filterStatus } from "../services/prisma";
|
import { filterStatus } from "../services/prisma";
|
||||||
|
import { fileLocation, listFile } from "../utils/minio";
|
||||||
|
|
||||||
if (!process.env.MINIO_BUCKET) {
|
if (!process.env.MINIO_BUCKET) {
|
||||||
throw Error("Require MinIO bucket.");
|
throw Error("Require MinIO bucket.");
|
||||||
|
|
@ -74,10 +75,6 @@ export type CustomerUpdate = {
|
||||||
birthDate?: Date;
|
birthDate?: Date;
|
||||||
};
|
};
|
||||||
|
|
||||||
function imageLocation(id: string) {
|
|
||||||
return `customer/${id}/profile-image`;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Route("api/v1/customer")
|
@Route("api/v1/customer")
|
||||||
@Tags("Customer")
|
@Tags("Customer")
|
||||||
export class CustomerController extends Controller {
|
export class CustomerController extends Controller {
|
||||||
|
|
@ -397,22 +394,46 @@ export class CustomerController extends Controller {
|
||||||
async (data) => await deleteFolder(MINIO_BUCKET, `customer/${customerId}`).then(() => data),
|
async (data) => await deleteFolder(MINIO_BUCKET, `customer/${customerId}`).then(() => data),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Get("{customerId}/image")
|
@Route("api/v1/customer/{customerId}/image")
|
||||||
async getCustomerImageById(@Request() req: RequestWithUser, @Path() customerId: string) {
|
@Tags("Customer")
|
||||||
const url = await presignedGetObjectIfExist(MINIO_BUCKET, imageLocation(customerId), 60 * 60);
|
export class CustomerImageController extends Controller {
|
||||||
|
@Get()
|
||||||
if (!url) {
|
@Security("keycloak")
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
|
async listImage(@Path() customerId: string) {
|
||||||
|
const customer = await prisma.customer.findUnique({
|
||||||
|
where: { id: customerId },
|
||||||
|
});
|
||||||
|
if (!customer) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found", "customerNotFound");
|
||||||
|
}
|
||||||
|
return await listFile(fileLocation.customer.img(customerId));
|
||||||
}
|
}
|
||||||
|
|
||||||
return req.res?.redirect(url);
|
@Get("{name}")
|
||||||
|
async getImage(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Path() customerId: string,
|
||||||
|
@Path() name: string,
|
||||||
|
) {
|
||||||
|
return req.res?.redirect(
|
||||||
|
await minio.presignedGetObject(
|
||||||
|
MINIO_BUCKET,
|
||||||
|
fileLocation.customer.img(customerId, name),
|
||||||
|
12 * 60 * 60,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put("{customerId}/image")
|
@Put("{name}")
|
||||||
@Security("keycloak", MANAGE_ROLES)
|
@Security("keycloak")
|
||||||
async setCustomerImageById(@Request() req: RequestWithUser, @Path() customerId: string) {
|
async putImage(
|
||||||
const record = await prisma.customer.findFirst({
|
@Request() req: RequestWithUser,
|
||||||
|
@Path() customerId: string,
|
||||||
|
@Path() name: string,
|
||||||
|
) {
|
||||||
|
const customer = await prisma.customer.findUnique({
|
||||||
where: { id: customerId },
|
where: { id: customerId },
|
||||||
include: {
|
include: {
|
||||||
registeredBranch: {
|
registeredBranch: {
|
||||||
|
|
@ -420,15 +441,41 @@ export class CustomerController extends Controller {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
if (!customer) {
|
||||||
if (!record) {
|
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "Customer cannot be found.", "customerNotFound");
|
|
||||||
}
|
}
|
||||||
|
console.log(customer.registeredBranch);
|
||||||
await permissionCheck(req.user, record.registeredBranch);
|
await permissionCheck(req.user, customer.registeredBranch);
|
||||||
|
|
||||||
return req.res?.redirect(
|
return req.res?.redirect(
|
||||||
await minio.presignedPutObject(MINIO_BUCKET, imageLocation(customerId), 12 * 60 * 60),
|
await minio.presignedPutObject(
|
||||||
|
MINIO_BUCKET,
|
||||||
|
fileLocation.customer.img(customerId, name),
|
||||||
|
12 * 60 * 60,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Delete("{name}")
|
||||||
|
@Security("keycloak")
|
||||||
|
async deleteImage(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Path() customerId: string,
|
||||||
|
@Path() name: string,
|
||||||
|
) {
|
||||||
|
const customer = await prisma.customer.findUnique({
|
||||||
|
where: { id: customerId },
|
||||||
|
include: {
|
||||||
|
registeredBranch: {
|
||||||
|
include: branchRelationPermInclude(req.user),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!customer) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "Image cannot be found", "imageNotFound");
|
||||||
|
}
|
||||||
|
await permissionCheck(req.user, customer.registeredBranch);
|
||||||
|
await minio.removeObject(MINIO_BUCKET, fileLocation.customer.img(customerId, name), {
|
||||||
|
forceDelete: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,4 +50,7 @@ export const fileLocation = {
|
||||||
profile: (userId: string, name?: string) => `user/profile-image-${userId}/${name || ""}`,
|
profile: (userId: string, name?: string) => `user/profile-image-${userId}/${name || ""}`,
|
||||||
attachment: (userId: string, name?: string) => `user/attachment-${userId}/${name || ""}`,
|
attachment: (userId: string, name?: string) => `user/attachment-${userId}/${name || ""}`,
|
||||||
},
|
},
|
||||||
|
customer: {
|
||||||
|
img: (customerId: string, name?: string) => `customer/img-${customerId}/${name || ""}`,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue