refactor: user relation
This commit is contained in:
parent
a74d8b63b1
commit
2bd30b735d
21 changed files with 607 additions and 185 deletions
|
|
@ -79,7 +79,7 @@ export class BranchContactController extends Controller {
|
|||
throw new HttpError(HttpStatus.BAD_REQUEST, "Branch cannot be found.", "branchBadReq");
|
||||
}
|
||||
const record = await prisma.branchContact.create({
|
||||
data: { ...body, branchId, createdBy: req.user.name, updatedBy: req.user.name },
|
||||
data: { ...body, branchId, createdByUserId: req.user.sub, updatedByUserId: req.user.sub },
|
||||
});
|
||||
|
||||
this.setStatus(HttpStatus.CREATED);
|
||||
|
|
@ -107,7 +107,7 @@ export class BranchContactController extends Controller {
|
|||
}
|
||||
|
||||
const record = await prisma.branchContact.update({
|
||||
data: { ...body, updatedBy: req.user.name },
|
||||
data: { ...body, updatedByUserId: req.user.sub },
|
||||
where: { id: contactId, branchId },
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -286,8 +286,8 @@ export class BranchController extends Controller {
|
|||
district: { connect: districtId ? { id: districtId } : undefined },
|
||||
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
|
||||
headOffice: { connect: headOfficeId ? { id: headOfficeId } : undefined },
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdBy: { connect: { id: req.user.sub } },
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
});
|
||||
},
|
||||
|
|
@ -403,7 +403,7 @@ export class BranchController extends Controller {
|
|||
connect: headOfficeId ? { id: headOfficeId } : undefined,
|
||||
disconnect: headOfficeId === null || undefined,
|
||||
},
|
||||
updatedBy: req.user.name,
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
where: { id: branchId },
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { Branch, Prisma, Status, User, UserType } from "@prisma/client";
|
||||
import { Branch, Prisma, Status, User } from "@prisma/client";
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
|
|
@ -113,11 +113,7 @@ export class BranchUserController extends Controller {
|
|||
]);
|
||||
|
||||
if (!branch) {
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Branch cannot be found.",
|
||||
"branchBadReq",
|
||||
);
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Branch cannot be found.", "branchBadReq");
|
||||
}
|
||||
|
||||
if (user.length !== body.user.length) {
|
||||
|
|
@ -139,8 +135,8 @@ export class BranchUserController extends Controller {
|
|||
.map((v) => ({
|
||||
branchId,
|
||||
userId: v.id,
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
})),
|
||||
}),
|
||||
]);
|
||||
|
|
@ -249,8 +245,8 @@ export class UserBranchController extends Controller {
|
|||
.map((v) => ({
|
||||
branchId: v.id,
|
||||
userId,
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
})),
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -294,8 +294,8 @@ export class CustomerBranchController extends Controller {
|
|||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
||||
district: { connect: districtId ? { id: districtId } : undefined },
|
||||
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdBy: { connect: { id: req.user.sub } },
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
});
|
||||
},
|
||||
|
|
@ -380,8 +380,7 @@ export class CustomerBranchController extends Controller {
|
|||
connect: subDistrictId ? { id: subDistrictId } : undefined,
|
||||
disconnect: subDistrictId === null || undefined,
|
||||
},
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -305,13 +305,13 @@ export class CustomerController extends Controller {
|
|||
...v,
|
||||
branchNo: i + 1,
|
||||
code: `${last.key.slice(9)}${last.value.toString().padStart(6, "0")}-${(i + 1).toString().padStart(2, "0")}`,
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
})) || [],
|
||||
},
|
||||
},
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
|
@ -436,20 +436,20 @@ export class CustomerController extends Controller {
|
|||
...v,
|
||||
branchNo: i + 1,
|
||||
code: `${customer.code}-${(i + 1).toString().padStart(2, "0")}`,
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
id: undefined,
|
||||
},
|
||||
update: {
|
||||
...v,
|
||||
branchNo: i + 1,
|
||||
code: `${customer.code}-${(i + 1).toString().padStart(2, "0")}`,
|
||||
updatedBy: req.user.name,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
})),
|
||||
}) ||
|
||||
undefined,
|
||||
updatedBy: req.user.name,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
})
|
||||
.then((v) => {
|
||||
|
|
|
|||
|
|
@ -90,8 +90,8 @@ export class EmployeeCheckupController extends Controller {
|
|||
...rest,
|
||||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
||||
employee: { connect: { id: employeeId } },
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdBy: { connect: { id: req.user.sub } },
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -142,8 +142,7 @@ export class EmployeeCheckupController extends Controller {
|
|||
data: {
|
||||
...rest,
|
||||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -418,8 +418,8 @@ export class EmployeeController extends Controller {
|
|||
district: { connect: districtId ? { id: districtId } : undefined },
|
||||
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
|
||||
customerBranch: { connect: { id: customerBranchId } },
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdBy: { connect: { id: req.user.sub } },
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
});
|
||||
},
|
||||
|
|
@ -578,13 +578,13 @@ export class EmployeeController extends Controller {
|
|||
where: { id: v.id || "" },
|
||||
create: {
|
||||
...v,
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
id: undefined,
|
||||
},
|
||||
update: {
|
||||
...v,
|
||||
updatedBy: req.user.name,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
})),
|
||||
}
|
||||
|
|
@ -602,13 +602,13 @@ export class EmployeeController extends Controller {
|
|||
create: {
|
||||
...v,
|
||||
provinceId: !v.provinceId ? undefined : v.provinceId,
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
id: undefined,
|
||||
},
|
||||
update: {
|
||||
...v,
|
||||
updatedBy: req.user.name,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
})),
|
||||
}
|
||||
|
|
@ -631,8 +631,8 @@ export class EmployeeController extends Controller {
|
|||
connect: subDistrictId ? { id: subDistrictId } : undefined,
|
||||
disconnect: subDistrictId === null || undefined,
|
||||
},
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdBy: { connect: { id: req.user.sub } },
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -51,18 +51,14 @@ export class EmployeeOtherInfo extends Controller {
|
|||
@Body() body: EmployeeOtherInfoPayload,
|
||||
) {
|
||||
if (!(await prisma.employee.findUnique({ where: { id: employeeId } })))
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Employee cannot be found.",
|
||||
"employeeBadReq",
|
||||
);
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Employee cannot be found.", "employeeBadReq");
|
||||
|
||||
const record = await prisma.employeeOtherInfo.create({
|
||||
data: {
|
||||
...body,
|
||||
employee: { connect: { id: employeeId } },
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdBy: { connect: { id: req.user.sub } },
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -88,7 +84,7 @@ export class EmployeeOtherInfo extends Controller {
|
|||
|
||||
const record = await prisma.employeeOtherInfo.update({
|
||||
where: { id: otherInfoId, employeeId },
|
||||
data: { ...body, createdBy: req.user.name, updatedBy: req.user.name },
|
||||
data: { ...body, updatedByUserId: req.user.sub },
|
||||
});
|
||||
|
||||
this.setStatus(HttpStatus.CREATED);
|
||||
|
|
|
|||
|
|
@ -46,7 +46,11 @@ export class EmployeeWorkController extends Controller {
|
|||
where: { id: workId, employeeId },
|
||||
});
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Employee work cannot be found.", "employeeWorkNotFound");
|
||||
throw new HttpError(
|
||||
HttpStatus.NOT_FOUND,
|
||||
"Employee work cannot be found.",
|
||||
"employeeWorkNotFound",
|
||||
);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
|
@ -58,18 +62,14 @@ export class EmployeeWorkController extends Controller {
|
|||
@Body() body: EmployeeWorkPayload,
|
||||
) {
|
||||
if (!(await prisma.employee.findUnique({ where: { id: employeeId } })))
|
||||
throw new HttpError(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
"Employee cannot be found.",
|
||||
"employeeBadReq",
|
||||
);
|
||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Employee cannot be found.", "employeeBadReq");
|
||||
|
||||
const record = await prisma.employeeWork.create({
|
||||
data: {
|
||||
...body,
|
||||
employee: { connect: { id: employeeId } },
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdBy: { connect: { id: req.user.sub } },
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -86,12 +86,16 @@ export class EmployeeWorkController extends Controller {
|
|||
@Body() body: EmployeeWorkPayload,
|
||||
) {
|
||||
if (!(await prisma.employeeWork.findUnique({ where: { id: workId, employeeId } }))) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Employee work cannot be found.", "employeeWorkNotFound");
|
||||
throw new HttpError(
|
||||
HttpStatus.NOT_FOUND,
|
||||
"Employee work cannot be found.",
|
||||
"employeeWorkNotFound",
|
||||
);
|
||||
}
|
||||
|
||||
const record = await prisma.employeeWork.update({
|
||||
where: { id: workId, employeeId },
|
||||
data: { ...body, createdBy: req.user.name, updatedBy: req.user.name },
|
||||
data: { ...body, updatedByUserId: req.user.sub },
|
||||
});
|
||||
|
||||
this.setStatus(HttpStatus.CREATED);
|
||||
|
|
@ -104,7 +108,11 @@ export class EmployeeWorkController extends Controller {
|
|||
const record = await prisma.employeeWork.findFirst({ where: { id: workId, employeeId } });
|
||||
|
||||
if (!record) {
|
||||
throw new HttpError(HttpStatus.NOT_FOUND, "Employee work cannot be found.", "employeeWorkNotFound");
|
||||
throw new HttpError(
|
||||
HttpStatus.NOT_FOUND,
|
||||
"Employee work cannot be found.",
|
||||
"employeeWorkNotFound",
|
||||
);
|
||||
}
|
||||
|
||||
return await prisma.employeeWork.delete({ where: { id: workId, employeeId } });
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import {
|
|||
createUser,
|
||||
deleteUser,
|
||||
editUser,
|
||||
getRoles,
|
||||
listRole,
|
||||
removeUserRoles,
|
||||
} from "../services/keycloak";
|
||||
|
||||
|
|
@ -38,7 +38,7 @@ export class KeycloakController extends Controller {
|
|||
|
||||
@Get("role")
|
||||
async getRole() {
|
||||
const role = await getRoles();
|
||||
const role = await listRole();
|
||||
if (Array.isArray(role))
|
||||
return role.filter(
|
||||
(a) =>
|
||||
|
|
@ -49,7 +49,7 @@ export class KeycloakController extends Controller {
|
|||
|
||||
@Post("{userId}/role")
|
||||
async addRole(@Path() userId: string, @Body() body: { role: string[] }) {
|
||||
const list = await getRoles();
|
||||
const list = await listRole();
|
||||
|
||||
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
|
||||
|
||||
|
|
@ -63,7 +63,7 @@ export class KeycloakController extends Controller {
|
|||
|
||||
@Delete("{userId}/role/{roleId}")
|
||||
async deleteRole(@Path() userId: string, @Path() roleId: string) {
|
||||
const list = await getRoles();
|
||||
const list = await listRole();
|
||||
|
||||
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
|
||||
|
||||
|
|
|
|||
|
|
@ -143,8 +143,8 @@ export class ProductGroup extends Controller {
|
|||
...body,
|
||||
statusOrder: +(body.status === "INACTIVE"),
|
||||
code: `G${last.value.toString().padStart(2, "0")}`,
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
|
@ -171,7 +171,7 @@ export class ProductGroup extends Controller {
|
|||
}
|
||||
|
||||
const record = await prisma.productGroup.update({
|
||||
data: { ...body, statusOrder: +(body.status === "INACTIVE"), updatedBy: req.user.name },
|
||||
data: { ...body, statusOrder: +(body.status === "INACTIVE"), updatedByUserId: req.user.sub },
|
||||
where: { id: groupId },
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -173,8 +173,8 @@ export class ProductController extends Controller {
|
|||
...body,
|
||||
statusOrder: +(body.status === "INACTIVE"),
|
||||
code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`,
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
|
@ -230,7 +230,7 @@ export class ProductController extends Controller {
|
|||
}
|
||||
|
||||
const record = await prisma.product.update({
|
||||
data: { ...body, statusOrder: +(body.status === "INACTIVE"), updatedBy: req.user.name },
|
||||
data: { ...body, statusOrder: +(body.status === "INACTIVE"), updatedByUserId: req.user.sub },
|
||||
where: { id: productId },
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -132,8 +132,8 @@ export class ProductType extends Controller {
|
|||
...body,
|
||||
statusOrder: +(body.status === "INACTIVE"),
|
||||
code: `T${productGroup.code}${last.value.toString().padStart(2, "0")}`,
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
|
@ -178,7 +178,7 @@ export class ProductType extends Controller {
|
|||
}
|
||||
|
||||
const record = await prisma.productType.update({
|
||||
data: { ...body, statusOrder: +(body.status === "INACTIVE"), updatedBy: req.user.name },
|
||||
data: { ...body, statusOrder: +(body.status === "INACTIVE"), updatedByUserId: req.user.sub },
|
||||
where: { id: typeId },
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -245,8 +245,8 @@ export class ServiceController extends Controller {
|
|||
statusOrder: +(body.status === "INACTIVE"),
|
||||
code: `${body.code.toLocaleUpperCase()}${last.value.toString().padStart(3, "0")}`,
|
||||
work: { connect: workList.map((v) => ({ id: v.id })) },
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
|
@ -308,7 +308,7 @@ export class ServiceController extends Controller {
|
|||
deleteMany: {},
|
||||
connect: workList.map((v) => ({ id: v.id })),
|
||||
},
|
||||
updatedBy: req.user.name,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
where: { id: serviceId },
|
||||
});
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ import {
|
|||
createUser,
|
||||
deleteUser,
|
||||
editUser,
|
||||
getRoles,
|
||||
listRole,
|
||||
getUserRoles,
|
||||
removeUserRoles,
|
||||
} from "../services/keycloak";
|
||||
|
|
@ -256,7 +256,7 @@ export class UserController extends Controller {
|
|||
|
||||
const { provinceId, districtId, subDistrictId, username, ...rest } = body;
|
||||
|
||||
let list = await getRoles();
|
||||
let list = await listRole();
|
||||
|
||||
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
|
||||
if (Array.isArray(list)) {
|
||||
|
|
@ -297,8 +297,8 @@ export class UserController extends Controller {
|
|||
province: { connect: provinceId ? { id: provinceId } : undefined },
|
||||
district: { connect: districtId ? { id: districtId } : undefined },
|
||||
subDistrict: { connect: subDistrictId ? { id: subDistrictId } : undefined },
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdBy: { connect: { id: req.user.sub } },
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -355,7 +355,7 @@ export class UserController extends Controller {
|
|||
let userRole: string | undefined;
|
||||
|
||||
if (body.userRole) {
|
||||
let list = await getRoles();
|
||||
let list = await listRole();
|
||||
|
||||
if (!Array.isArray(list)) throw new Error("Failed. Cannot get role(s) data from the server.");
|
||||
if (Array.isArray(list)) {
|
||||
|
|
@ -438,7 +438,7 @@ export class UserController extends Controller {
|
|||
connect: subDistrictId ? { id: subDistrictId } : undefined,
|
||||
disconnect: subDistrictId === null || undefined,
|
||||
},
|
||||
updatedBy: req.user.name,
|
||||
updatedBy: { connect: { id: req.user.sub } },
|
||||
},
|
||||
where: { id: userId },
|
||||
});
|
||||
|
|
|
|||
|
|
@ -183,13 +183,13 @@ export class WorkController extends Controller {
|
|||
data: productId.map((v, i) => ({
|
||||
order: i + 1,
|
||||
productId: v,
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
})),
|
||||
},
|
||||
},
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
});
|
||||
|
||||
|
|
@ -281,13 +281,13 @@ export class WorkController extends Controller {
|
|||
create: {
|
||||
order: i + 1,
|
||||
productId: v,
|
||||
createdBy: req.user.name,
|
||||
updatedBy: req.user.name,
|
||||
createdByUserId: req.user.sub,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
})),
|
||||
}
|
||||
: undefined,
|
||||
updatedBy: req.user.name,
|
||||
updatedByUserId: req.user.sub,
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue