feat: add relation for agent
This commit is contained in:
parent
a412aef7ac
commit
e76292fe38
4 changed files with 62 additions and 11 deletions
|
|
@ -0,0 +1,15 @@
|
||||||
|
/*
|
||||||
|
Warnings:
|
||||||
|
|
||||||
|
- You are about to drop the column `agent` on the `CustomerBranch` table. All the data in the column will be lost.
|
||||||
|
|
||||||
|
*/
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "CustomerBranch" DROP COLUMN "agent",
|
||||||
|
ADD COLUMN "agentUserId" TEXT;
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "ProductGroup" ADD COLUMN "shared" BOOLEAN NOT NULL DEFAULT false;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "CustomerBranch" ADD CONSTRAINT "CustomerBranch_agentUserId_fkey" FOREIGN KEY ("agentUserId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||||
|
|
@ -389,6 +389,7 @@ model User {
|
||||||
userMenuComponentPermission UserMenuComponentPermission[]
|
userMenuComponentPermission UserMenuComponentPermission[]
|
||||||
workflowTemplateStepUser WorkflowTemplateStepUser[]
|
workflowTemplateStepUser WorkflowTemplateStepUser[]
|
||||||
requestWork RequestWork[]
|
requestWork RequestWork[]
|
||||||
|
customerBranch CustomerBranch[]
|
||||||
|
|
||||||
userCreated User[] @relation("UserCreatedByUser")
|
userCreated User[] @relation("UserCreatedByUser")
|
||||||
userUpdated User[] @relation("UserUpdatedByUser")
|
userUpdated User[] @relation("UserUpdatedByUser")
|
||||||
|
|
@ -513,7 +514,8 @@ model CustomerBranch {
|
||||||
contactTel String
|
contactTel String
|
||||||
officeTel String
|
officeTel String
|
||||||
contactName String
|
contactName String
|
||||||
agent String
|
agentUserId String?
|
||||||
|
agentUser User? @relation(fields: [agentUserId], references: [id], onDelete: SetNull)
|
||||||
|
|
||||||
// NOTE: Business
|
// NOTE: Business
|
||||||
businessType String
|
businessType String
|
||||||
|
|
@ -952,6 +954,7 @@ model ProductGroup {
|
||||||
name String
|
name String
|
||||||
detail String
|
detail String
|
||||||
remark String
|
remark String
|
||||||
|
shared Boolean @default(false)
|
||||||
|
|
||||||
status Status @default(CREATED)
|
status Status @default(CREATED)
|
||||||
statusOrder Int @default(0)
|
statusOrder Int @default(0)
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ function globalAllow(user: RequestWithUser["user"]) {
|
||||||
|
|
||||||
const permissionCondCompany = createPermCondition((_) => true);
|
const permissionCondCompany = createPermCondition((_) => true);
|
||||||
const permissionCond = createPermCondition(globalAllow);
|
const permissionCond = createPermCondition(globalAllow);
|
||||||
|
const permissionCheckCompany = createPermCheck((_) => true);
|
||||||
const permissionCheck = createPermCheck(globalAllow);
|
const permissionCheck = createPermCheck(globalAllow);
|
||||||
|
|
||||||
export type CustomerBranchCreate = {
|
export type CustomerBranchCreate = {
|
||||||
|
|
@ -103,7 +104,7 @@ export type CustomerBranchCreate = {
|
||||||
contactTel: string;
|
contactTel: string;
|
||||||
officeTel: string;
|
officeTel: string;
|
||||||
contactName: string;
|
contactName: string;
|
||||||
agent: string;
|
agentUserId?: string;
|
||||||
|
|
||||||
businessType: string;
|
businessType: string;
|
||||||
jobPosition: string;
|
jobPosition: string;
|
||||||
|
|
@ -161,7 +162,7 @@ export type CustomerBranchUpdate = {
|
||||||
contactTel?: string;
|
contactTel?: string;
|
||||||
officeTel?: string;
|
officeTel?: string;
|
||||||
contactName?: string;
|
contactName?: string;
|
||||||
agent?: string;
|
agentUserId?: string;
|
||||||
|
|
||||||
businessType?: string;
|
businessType?: string;
|
||||||
jobPosition?: string;
|
jobPosition?: string;
|
||||||
|
|
@ -322,7 +323,7 @@ export class CustomerBranchController extends Controller {
|
||||||
@Post()
|
@Post()
|
||||||
@Security("keycloak", MANAGE_ROLES)
|
@Security("keycloak", MANAGE_ROLES)
|
||||||
async create(@Request() req: RequestWithUser, @Body() body: CustomerBranchCreate) {
|
async create(@Request() req: RequestWithUser, @Body() body: CustomerBranchCreate) {
|
||||||
const [province, district, subDistrict, customer] = await prisma.$transaction([
|
const [province, district, subDistrict, customer, agent] = await prisma.$transaction([
|
||||||
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
||||||
prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
|
prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
|
||||||
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
|
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
|
||||||
|
|
@ -338,17 +339,30 @@ export class CustomerBranchController extends Controller {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
prisma.user.findFirst({
|
||||||
|
where: { id: body.customerId || undefined },
|
||||||
|
include: {
|
||||||
|
branch: {
|
||||||
|
include: { branch: { include: branchRelationPermInclude(req.user) } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
]);
|
]);
|
||||||
if (body.provinceId && !province) throw relationError("Province");
|
if (body.provinceId && !province) throw relationError("Province");
|
||||||
if (body.districtId && !district) throw relationError("District");
|
if (body.districtId && !district) throw relationError("District");
|
||||||
if (body.subDistrictId && !subDistrict) throw relationError("SubDistrict");
|
if (body.subDistrictId && !subDistrict) throw relationError("SubDistrict");
|
||||||
|
if (body.agentUserId && !agent) throw relationError("User");
|
||||||
if (!customer) throw relationError("Customer");
|
if (!customer) throw relationError("Customer");
|
||||||
|
if (agent) {
|
||||||
|
await Promise.all(agent.branch.map(({ branch }) => permissionCheckCompany(req.user, branch)));
|
||||||
|
}
|
||||||
|
await permissionCheck(req.user, customer.registeredBranch);
|
||||||
|
|
||||||
let company = await permissionCheck(req.user, customer.registeredBranch).then(
|
let company = await permissionCheck(req.user, customer.registeredBranch).then(
|
||||||
(v) => (v.headOffice || v).code,
|
(v) => (v.headOffice || v).code,
|
||||||
);
|
);
|
||||||
|
|
||||||
const { provinceId, districtId, subDistrictId, customerId, ...rest } = body;
|
const { provinceId, districtId, subDistrictId, customerId, agentUserId, ...rest } = body;
|
||||||
|
|
||||||
const record = await prisma.$transaction(
|
const record = await prisma.$transaction(
|
||||||
async (tx) => {
|
async (tx) => {
|
||||||
|
|
@ -394,12 +408,14 @@ export class CustomerBranchController extends Controller {
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
...rest,
|
...rest,
|
||||||
|
|
||||||
code: `${runningKey.replace(`CUSTOMER_BRANCH_${company}_`, "")}-${`${last.value - 1}`.padStart(2, "0")}`,
|
code: `${runningKey.replace(`CUSTOMER_BRANCH_${company}_`, "")}-${`${last.value - 1}`.padStart(2, "0")}`,
|
||||||
codeCustomer: runningKey.replace(`CUSTOMER_BRANCH_${company}_`, ""),
|
codeCustomer: runningKey.replace(`CUSTOMER_BRANCH_${company}_`, ""),
|
||||||
customer: { connect: { id: customerId } },
|
customer: { connect: { id: customerId } },
|
||||||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
agentUser: connectOrNot(agentUserId),
|
||||||
district: { connect: districtId ? { id: districtId } : undefined },
|
province: connectOrNot(provinceId),
|
||||||
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
|
district: connectOrNot(districtId),
|
||||||
|
subDistrict: connectOrNot(subDistrictId),
|
||||||
createdBy: { connect: { id: req.user.sub } },
|
createdBy: { connect: { id: req.user.sub } },
|
||||||
updatedBy: { connect: { id: req.user.sub } },
|
updatedBy: { connect: { id: req.user.sub } },
|
||||||
},
|
},
|
||||||
|
|
@ -440,7 +456,7 @@ export class CustomerBranchController extends Controller {
|
||||||
if (!body.customerId) body.customerId = branch.customerId;
|
if (!body.customerId) body.customerId = branch.customerId;
|
||||||
|
|
||||||
if (body.provinceId || body.districtId || body.subDistrictId || body.customerId) {
|
if (body.provinceId || body.districtId || body.subDistrictId || body.customerId) {
|
||||||
const [province, district, subDistrict, customer] = await prisma.$transaction([
|
const [province, district, subDistrict, customer, agent] = await prisma.$transaction([
|
||||||
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
prisma.province.findFirst({ where: { id: body.provinceId || undefined } }),
|
||||||
prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
|
prisma.district.findFirst({ where: { id: body.districtId || undefined } }),
|
||||||
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
|
prisma.subDistrict.findFirst({ where: { id: body.subDistrictId || undefined } }),
|
||||||
|
|
@ -452,15 +468,29 @@ export class CustomerBranchController extends Controller {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
prisma.user.findFirst({
|
||||||
|
where: { id: body.customerId || undefined },
|
||||||
|
include: {
|
||||||
|
branch: {
|
||||||
|
include: { branch: { include: branchRelationPermInclude(req.user) } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
]);
|
]);
|
||||||
if (body.provinceId && !province) throw relationError("Province");
|
if (body.provinceId && !province) throw relationError("Province");
|
||||||
if (body.districtId && !district) throw relationError("District");
|
if (body.districtId && !district) throw relationError("District");
|
||||||
if (body.subDistrictId && !subDistrict) throw relationError("SubDistrict");
|
if (body.subDistrictId && !subDistrict) throw relationError("SubDistrict");
|
||||||
|
if (body.agentUserId && !agent) throw relationError("User");
|
||||||
if (!customer) throw relationError("Customer");
|
if (!customer) throw relationError("Customer");
|
||||||
|
if (agent) {
|
||||||
|
await Promise.all(
|
||||||
|
agent.branch.map(({ branch }) => permissionCheckCompany(req.user, branch)),
|
||||||
|
);
|
||||||
|
}
|
||||||
await permissionCheck(req.user, customer.registeredBranch);
|
await permissionCheck(req.user, customer.registeredBranch);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { provinceId, districtId, subDistrictId, customerId, ...rest } = body;
|
const { provinceId, districtId, subDistrictId, customerId, agentUserId, ...rest } = body;
|
||||||
|
|
||||||
return await prisma.customerBranch.update({
|
return await prisma.customerBranch.update({
|
||||||
where: { id: branchId },
|
where: { id: branchId },
|
||||||
|
|
@ -474,6 +504,7 @@ export class CustomerBranchController extends Controller {
|
||||||
data: {
|
data: {
|
||||||
...rest,
|
...rest,
|
||||||
statusOrder: +(rest.status === "INACTIVE"),
|
statusOrder: +(rest.status === "INACTIVE"),
|
||||||
|
agentUser: connectOrNot(agentUserId),
|
||||||
customer: connectOrNot(customerId),
|
customer: connectOrNot(customerId),
|
||||||
province: connectOrDisconnect(provinceId),
|
province: connectOrDisconnect(provinceId),
|
||||||
district: connectOrDisconnect(districtId),
|
district: connectOrDisconnect(districtId),
|
||||||
|
|
|
||||||
|
|
@ -103,7 +103,7 @@ export type CustomerCreate = {
|
||||||
contactTel: string;
|
contactTel: string;
|
||||||
officeTel: string;
|
officeTel: string;
|
||||||
contactName: string;
|
contactName: string;
|
||||||
agent: string;
|
agentUserId?: string;
|
||||||
|
|
||||||
businessType: string;
|
businessType: string;
|
||||||
jobPosition: string;
|
jobPosition: string;
|
||||||
|
|
@ -316,6 +316,8 @@ export class CustomerController extends Controller {
|
||||||
...v,
|
...v,
|
||||||
code: `${runningKey.replace(`CUSTOMER_BRANCH_${company}_`, "")}-${`${last.value - branch.length + i}`.padStart(2, "0")}`,
|
code: `${runningKey.replace(`CUSTOMER_BRANCH_${company}_`, "")}-${`${last.value - branch.length + i}`.padStart(2, "0")}`,
|
||||||
codeCustomer: runningKey.replace(`CUSTOMER_BRANCH_${company}_`, ""),
|
codeCustomer: runningKey.replace(`CUSTOMER_BRANCH_${company}_`, ""),
|
||||||
|
agentUser: connectOrNot(v.agentUserId),
|
||||||
|
agentUserId: undefined,
|
||||||
province: connectOrNot(v.provinceId),
|
province: connectOrNot(v.provinceId),
|
||||||
provinceId: undefined,
|
provinceId: undefined,
|
||||||
district: connectOrNot(v.districtId),
|
district: connectOrNot(v.districtId),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue