feat: auto gen user code
This commit is contained in:
parent
3abff0594a
commit
3f0ed2c8d6
4 changed files with 113 additions and 32 deletions
5
prisma/migrations/20240409040419_update/migration.sql
Normal file
5
prisma/migrations/20240409040419_update/migration.sql
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
-- CreateEnum
|
||||||
|
CREATE TYPE "UserType" AS ENUM ('USER', 'MESSENGER', 'DELEGATE', 'AGENCY');
|
||||||
|
|
||||||
|
-- AlterTable
|
||||||
|
ALTER TABLE "User" ALTER COLUMN "code" DROP NOT NULL;
|
||||||
|
|
@ -73,7 +73,7 @@ enum Status {
|
||||||
|
|
||||||
model Branch {
|
model Branch {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
code String @default(uuid())
|
code String
|
||||||
taxNo String
|
taxNo String
|
||||||
name String
|
name String
|
||||||
nameEN String
|
nameEN String
|
||||||
|
|
@ -143,12 +143,19 @@ model BranchUser {
|
||||||
updatedAt DateTime @updatedAt
|
updatedAt DateTime @updatedAt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum UserType {
|
||||||
|
USER
|
||||||
|
MESSENGER
|
||||||
|
DELEGATE
|
||||||
|
AGENCY
|
||||||
|
}
|
||||||
|
|
||||||
model User {
|
model User {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
|
|
||||||
keycloakId String
|
keycloakId String
|
||||||
|
|
||||||
code String @default(uuid())
|
code String?
|
||||||
firstName String
|
firstName String
|
||||||
firstNameEN String
|
firstNameEN String
|
||||||
lastName String
|
lastName String
|
||||||
|
|
@ -177,7 +184,7 @@ model User {
|
||||||
startDate DateTime?
|
startDate DateTime?
|
||||||
retireDate DateTime?
|
retireDate DateTime?
|
||||||
|
|
||||||
userType String
|
userType UserType
|
||||||
userRole String
|
userRole String
|
||||||
|
|
||||||
discountCondition String?
|
discountCondition String?
|
||||||
|
|
@ -203,7 +210,7 @@ model User {
|
||||||
|
|
||||||
model Customer {
|
model Customer {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
code String @default(uuid())
|
code String
|
||||||
customerType String
|
customerType String
|
||||||
customerName String
|
customerName String
|
||||||
customerNameEN String
|
customerNameEN String
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { Prisma, Status } from "@prisma/client";
|
import { Prisma, Status, UserType } from "@prisma/client";
|
||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
|
|
@ -71,10 +71,23 @@ export class BranchUserController extends Controller {
|
||||||
@Path() branchId: string,
|
@Path() branchId: string,
|
||||||
@Body() body: BranchUserBody,
|
@Body() body: BranchUserBody,
|
||||||
) {
|
) {
|
||||||
const user = await prisma.user.findMany({
|
const [branch, user] = await prisma.$transaction([
|
||||||
include: { branch: true },
|
prisma.branch.findUnique({
|
||||||
where: { id: { in: body.user } },
|
where: { id: branchId },
|
||||||
});
|
}),
|
||||||
|
prisma.user.findMany({
|
||||||
|
include: { branch: true },
|
||||||
|
where: { id: { in: body.user } },
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!branch) {
|
||||||
|
throw new HttpError(
|
||||||
|
HttpStatus.BAD_REQUEST,
|
||||||
|
"Branch cannot be found.",
|
||||||
|
"missing_or_invalid_parameter",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (user.length !== body.user.length) {
|
if (user.length !== body.user.length) {
|
||||||
throw new HttpError(
|
throw new HttpError(
|
||||||
|
|
@ -84,21 +97,55 @@ export class BranchUserController extends Controller {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
await prisma.user.updateMany({
|
await prisma.$transaction([
|
||||||
where: { id: { in: body.user }, status: Status.CREATED },
|
prisma.user.updateMany({
|
||||||
data: { status: Status.ACTIVE },
|
where: { id: { in: body.user }, status: Status.CREATED },
|
||||||
});
|
data: { status: Status.ACTIVE },
|
||||||
|
}),
|
||||||
|
prisma.branchUser.createMany({
|
||||||
|
data: user
|
||||||
|
.filter((a) => !a.branch.some((b) => b.branchId === branchId))
|
||||||
|
.map((v) => ({
|
||||||
|
branchId,
|
||||||
|
userId: v.id,
|
||||||
|
createdBy: req.user.name,
|
||||||
|
updateBy: req.user.name,
|
||||||
|
})),
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
await prisma.branchUser.createMany({
|
const group: Record<UserType, string[]> = {
|
||||||
data: user
|
USER: [],
|
||||||
.filter((a) => !a.branch.some((b) => b.branchId === branchId))
|
AGENCY: [],
|
||||||
.map((v) => ({
|
DELEGATE: [],
|
||||||
branchId,
|
MESSENGER: [],
|
||||||
userId: v.id,
|
};
|
||||||
createdBy: req.user.name,
|
|
||||||
updateBy: req.user.name,
|
for (const u of user) group[u.userType].push(u.id);
|
||||||
})),
|
|
||||||
});
|
for (const g of Object.values(UserType)) {
|
||||||
|
if (group[g].length === 0) continue;
|
||||||
|
|
||||||
|
const last = await prisma.user.findFirst({
|
||||||
|
orderBy: { createdAt: "desc" },
|
||||||
|
where: {
|
||||||
|
userType: g,
|
||||||
|
code: { startsWith: `${branch.code.slice(2, 5).padEnd(3, "0")}` },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const code = (idx: number) =>
|
||||||
|
`${branch.code.slice(4).padEnd(3, "0")}${g !== "USER" ? g.charAt(0) : ""}${(+(last?.code?.slice(-4) || 0) + idx + 1).toString().padStart(4, "0")}`;
|
||||||
|
|
||||||
|
await prisma.$transaction(
|
||||||
|
group[g].map((v, i) =>
|
||||||
|
prisma.user.updateMany({
|
||||||
|
where: { id: v, code: null },
|
||||||
|
data: { code: code(i) },
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete()
|
@Delete()
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ import {
|
||||||
Security,
|
Security,
|
||||||
Tags,
|
Tags,
|
||||||
} from "tsoa";
|
} from "tsoa";
|
||||||
import { Prisma, Status } from "@prisma/client";
|
import { Prisma, Status, UserType } from "@prisma/client";
|
||||||
|
|
||||||
import prisma from "../db";
|
import prisma from "../db";
|
||||||
import minio from "../services/minio";
|
import minio from "../services/minio";
|
||||||
|
|
@ -31,7 +31,7 @@ type UserCreate = {
|
||||||
|
|
||||||
keycloakId: string;
|
keycloakId: string;
|
||||||
|
|
||||||
userType: string;
|
userType: UserType;
|
||||||
userRole: string;
|
userRole: string;
|
||||||
|
|
||||||
firstName: string;
|
firstName: string;
|
||||||
|
|
@ -40,7 +40,6 @@ type UserCreate = {
|
||||||
lastNameEN: string;
|
lastNameEN: string;
|
||||||
gender: string;
|
gender: string;
|
||||||
|
|
||||||
code?: string;
|
|
||||||
registrationNo?: string;
|
registrationNo?: string;
|
||||||
startDate?: Date;
|
startDate?: Date;
|
||||||
retireDate?: Date;
|
retireDate?: Date;
|
||||||
|
|
@ -66,7 +65,7 @@ type UserCreate = {
|
||||||
type UserUpdate = {
|
type UserUpdate = {
|
||||||
status?: "ACTIVE" | "INACTIVE";
|
status?: "ACTIVE" | "INACTIVE";
|
||||||
|
|
||||||
userType?: string;
|
userType?: UserType;
|
||||||
userRole?: string;
|
userRole?: string;
|
||||||
|
|
||||||
firstName?: string;
|
firstName?: string;
|
||||||
|
|
@ -75,7 +74,6 @@ type UserUpdate = {
|
||||||
lastNameEN?: string;
|
lastNameEN?: string;
|
||||||
gender?: string;
|
gender?: string;
|
||||||
|
|
||||||
code?: string;
|
|
||||||
registrationNo?: string;
|
registrationNo?: string;
|
||||||
startDate?: Date;
|
startDate?: Date;
|
||||||
retireDate?: Date;
|
retireDate?: Date;
|
||||||
|
|
@ -108,7 +106,7 @@ function imageLocation(id: string) {
|
||||||
export class UserController extends Controller {
|
export class UserController extends Controller {
|
||||||
@Get()
|
@Get()
|
||||||
async getUser(
|
async getUser(
|
||||||
@Query() userType?: string,
|
@Query() userType?: UserType,
|
||||||
@Query() zipCode?: string,
|
@Query() zipCode?: string,
|
||||||
@Query() query: string = "",
|
@Query() query: string = "",
|
||||||
@Query() page: number = 1,
|
@Query() page: number = 1,
|
||||||
|
|
@ -276,10 +274,36 @@ export class UserController extends Controller {
|
||||||
|
|
||||||
const { provinceId, districtId, subDistrictId, ...rest } = body;
|
const { provinceId, districtId, subDistrictId, ...rest } = body;
|
||||||
|
|
||||||
|
const user = await prisma.user.findFirst({
|
||||||
|
where: { id: userId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "Branch cannot be found.", "data_not_found");
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastUserOfType =
|
||||||
|
body.userType &&
|
||||||
|
body.userType !== user.userType &&
|
||||||
|
user.code &&
|
||||||
|
(await prisma.user.findFirst({
|
||||||
|
orderBy: { createdAt: "desc" },
|
||||||
|
where: {
|
||||||
|
userType: body.userType,
|
||||||
|
code: { startsWith: `${user.code?.slice(0, 3)}` },
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
console.log(lastUserOfType);
|
||||||
|
|
||||||
const record = await prisma.user.update({
|
const record = await prisma.user.update({
|
||||||
include: { province: true, district: true, subDistrict: true },
|
include: { province: true, district: true, subDistrict: true },
|
||||||
data: {
|
data: {
|
||||||
...rest,
|
...rest,
|
||||||
|
code:
|
||||||
|
(lastUserOfType &&
|
||||||
|
`${user.code?.slice(0, 3)}${body.userType !== "USER" ? body.userType?.charAt(0) : ""}${(+(lastUserOfType?.code?.slice(-4) || 0) + 1).toString().padStart(4, "0")}`) ||
|
||||||
|
undefined,
|
||||||
province: {
|
province: {
|
||||||
connect: provinceId ? { id: provinceId } : undefined,
|
connect: provinceId ? { id: provinceId } : undefined,
|
||||||
disconnect: provinceId === null || undefined,
|
disconnect: provinceId === null || undefined,
|
||||||
|
|
@ -296,9 +320,7 @@ export class UserController extends Controller {
|
||||||
},
|
},
|
||||||
where: { id: userId },
|
where: { id: userId },
|
||||||
});
|
});
|
||||||
if (!record) {
|
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.");
|
|
||||||
}
|
|
||||||
return Object.assign(record, {
|
return Object.assign(record, {
|
||||||
profileImageUrl: await minio.presignedGetObject(
|
profileImageUrl: await minio.presignedGetObject(
|
||||||
MINIO_BUCKET,
|
MINIO_BUCKET,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue