refactor: user relation
This commit is contained in:
parent
a74d8b63b1
commit
2bd30b735d
21 changed files with 607 additions and 185 deletions
49
src/app.ts
49
src/app.ts
|
|
@ -6,6 +6,8 @@ import swaggerDocument from "./swagger.json";
|
|||
import error from "./middlewares/error";
|
||||
import { RegisterRoutes } from "./routes";
|
||||
import logMiddleware from "./middlewares/log";
|
||||
import { addUserRoles, createUser, getRoleByName, listUser } from "./services/keycloak";
|
||||
import prisma from "./db";
|
||||
|
||||
const APP_HOST = process.env.APP_HOST || "0.0.0.0";
|
||||
const APP_PORT = +(process.env.APP_PORT || 3000);
|
||||
|
|
@ -13,6 +15,53 @@ const APP_PORT = +(process.env.APP_PORT || 3000);
|
|||
(async () => {
|
||||
const app = express();
|
||||
|
||||
let users = await (async () => {
|
||||
let list = await listUser();
|
||||
while (!list) {
|
||||
list = await listUser();
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
}
|
||||
return list;
|
||||
})();
|
||||
|
||||
if (users.length === 0) {
|
||||
const role = await getRoleByName("system");
|
||||
const userId = await createUser("admin", "1234", {
|
||||
firstName: "Admin",
|
||||
lastName: "System",
|
||||
email: "admin@jws.local",
|
||||
requiredActions: ["UPDATE_PASSWORD"],
|
||||
enabled: true,
|
||||
});
|
||||
|
||||
if (!userId || typeof userId !== "string") {
|
||||
throw new Error("Error create user with keycloak service.");
|
||||
}
|
||||
|
||||
if (role) await addUserRoles(userId, [role]);
|
||||
|
||||
await prisma.user.create({
|
||||
include: { province: true, district: true, subDistrict: true },
|
||||
data: {
|
||||
id: userId,
|
||||
email: "admin@jws.local",
|
||||
gender: "",
|
||||
address: "",
|
||||
addressEN: "",
|
||||
zipCode: "",
|
||||
userType: "USER",
|
||||
userRole: "system",
|
||||
telephoneNo: "",
|
||||
firstName: "Admin",
|
||||
firstNameEN: "Admin",
|
||||
lastName: "System",
|
||||
lastNameEN: "System",
|
||||
statusOrder: 0,
|
||||
username: "admin",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
app.use(cors());
|
||||
app.use(json());
|
||||
app.use(urlencoded({ extended: true }));
|
||||
|
|
|
|||
8
src/config.json
Normal file
8
src/config.json
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"branch": {
|
||||
"maxHeadOfficeBranch": 10
|
||||
},
|
||||
"personnel": {
|
||||
"type": ["USER", "MESSENGER", "DELEGATE", "AGENCY"]
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -59,6 +59,61 @@ export async function getToken() {
|
|||
return token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get keycloak user list
|
||||
*
|
||||
* @returns user list if success, false otherwise.
|
||||
*/
|
||||
export async function listUser(search = "", page = 1, pageSize = 30) {
|
||||
const res = await fetch(
|
||||
`${KC_URL}/admin/realms/${KC_REALM}/users?first=${(page - 1) * pageSize}&max=${pageSize}`.concat(
|
||||
!!search ? `&search=${search}` : "",
|
||||
),
|
||||
{
|
||||
headers: {
|
||||
authorization: `Bearer ${await getToken()}`,
|
||||
"content-type": `application/json`,
|
||||
},
|
||||
},
|
||||
).catch((e) => console.log("Keycloak Error: ", e));
|
||||
|
||||
if (!res) return;
|
||||
if (!res.ok) {
|
||||
return console.error("Keycloak Error Response: ", await res.json());
|
||||
}
|
||||
return ((await res.json()) as any[]).map((v: Record<string, string>) => ({
|
||||
id: v.id,
|
||||
username: v.username,
|
||||
firstName: v.firstName,
|
||||
lastName: v.lastName,
|
||||
email: v.email,
|
||||
attributes: v.attributes,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Count user in the system. Can be use for pagination purpose.
|
||||
*
|
||||
* @returns numer of user on success.
|
||||
*/
|
||||
export async function countUser(search = "") {
|
||||
const res = await fetch(
|
||||
`${KC_URL}/admin/realms/${KC_REALM}/users/count`.concat(!!search ? `?search=${search}` : ""),
|
||||
{
|
||||
headers: {
|
||||
authorization: `Bearer ${await getToken()}`,
|
||||
"content-type": `application/json`,
|
||||
},
|
||||
},
|
||||
).catch((e) => console.log("Keycloak Error: ", e));
|
||||
|
||||
if (!res) return;
|
||||
if (!res.ok) {
|
||||
return console.error("Keycloak Error Response: ", await res.json());
|
||||
}
|
||||
return (await res.json()) as number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create keycloak user by given username and password with roles
|
||||
*
|
||||
|
|
@ -151,36 +206,42 @@ export async function deleteUser(userId: string) {
|
|||
|
||||
/**
|
||||
* Get roles list or specific role data
|
||||
*
|
||||
* Client must have permission to get realms roles
|
||||
*
|
||||
* @returns role's info (array if not specify name) if success, null if not found, false otherwise.
|
||||
*/
|
||||
export async function getRoles(name?: string) {
|
||||
const res = await fetch(
|
||||
`${KC_URL}/admin/realms/${KC_REALM}/roles`.concat((name && `/${name}`) || ""),
|
||||
{
|
||||
// prettier-ignore
|
||||
headers: {
|
||||
"authorization": `Bearer ${await getToken()}`,
|
||||
export async function listRole() {
|
||||
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/roles`, {
|
||||
headers: {
|
||||
authorization: `Bearer ${await getToken()}`,
|
||||
},
|
||||
},
|
||||
).catch((e) => console.log(e));
|
||||
}).catch((e) => console.log(e));
|
||||
|
||||
if (!res) return false;
|
||||
if (!res) return;
|
||||
if (!res.ok && res.status !== 404) {
|
||||
return Boolean(console.error("Keycloak Error Response: ", await res.json()));
|
||||
return console.error("Keycloak Error Response: ", await res.json());
|
||||
}
|
||||
|
||||
if (res.status === 404) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
const data = (await res.json()) as any[];
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
return data.map((v: Record<string, string>) => ({ id: v.id, name: v.name }));
|
||||
return data.map((v: Record<string, string>) => ({ id: v.id, name: v.name }));
|
||||
}
|
||||
|
||||
export async function getRoleByName(name: string) {
|
||||
const res = await fetch(`${KC_URL}/admin/realms/${KC_REALM}/roles`.concat(`/${name}`), {
|
||||
headers: {
|
||||
authorization: `Bearer ${await getToken()}`,
|
||||
},
|
||||
}).catch((e) => console.log(e));
|
||||
|
||||
if (!res) return;
|
||||
if (!res.ok && res.status !== 404) {
|
||||
return console.error("Keycloak Error Response: ", await res.json());
|
||||
}
|
||||
if (res.status === 404) return null;
|
||||
|
||||
const data = (await res.json()) as any;
|
||||
|
||||
return {
|
||||
id: data.id,
|
||||
|
|
@ -285,7 +346,7 @@ export async function removeUserRoles(userId: string, roles: { id: string; name:
|
|||
|
||||
export default {
|
||||
createUser,
|
||||
getRoles,
|
||||
listRole,
|
||||
addUserRoles,
|
||||
removeUserRoles,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue