feat: check if user in in used
This commit is contained in:
parent
7f934454a9
commit
8336310e69
1 changed files with 25 additions and 3 deletions
|
|
@ -12,7 +12,7 @@ import {
|
||||||
Security,
|
Security,
|
||||||
Tags,
|
Tags,
|
||||||
} from "tsoa";
|
} from "tsoa";
|
||||||
import { Prisma } from "@prisma/client";
|
import { Prisma, Status } from "@prisma/client";
|
||||||
|
|
||||||
import prisma from "../../db";
|
import prisma from "../../db";
|
||||||
import minio from "../../services/minio";
|
import minio from "../../services/minio";
|
||||||
|
|
@ -308,8 +308,30 @@ export class UserController extends Controller {
|
||||||
|
|
||||||
@Delete("{userId}")
|
@Delete("{userId}")
|
||||||
async deleteUser(@Path() userId: string) {
|
async deleteUser(@Path() userId: string) {
|
||||||
const result = await prisma.user.deleteMany({ where: { id: userId } });
|
const record = await prisma.user.findFirst({
|
||||||
if (result.count <= 0)
|
include: {
|
||||||
|
province: true,
|
||||||
|
district: true,
|
||||||
|
subDistrict: true,
|
||||||
|
},
|
||||||
|
where: { id: userId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!record) {
|
||||||
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "data_not_found");
|
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "data_not_found");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (record.status === Status.USED) {
|
||||||
|
throw new HttpError(HttpStatus.FORBIDDEN, "User is in used.", "data_in_used");
|
||||||
|
}
|
||||||
|
|
||||||
|
return await prisma.user.delete({
|
||||||
|
include: {
|
||||||
|
province: true,
|
||||||
|
district: true,
|
||||||
|
subDistrict: true,
|
||||||
|
},
|
||||||
|
where: { id: userId },
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue