update: list in room to pharmacy
This commit is contained in:
@ -36,31 +36,99 @@ export class RoomToPharmacyController {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let param: Paging = await schema.validateAsync(req.query);
|
let param: Paging = await schema.validateAsync(req.query);
|
||||||
|
|
||||||
let filter = param.filter && param.filter !== "" ? JSON.parse(param.filter) : {};
|
let filter = param.filter && param.filter !== "" ? JSON.parse(param.filter) : {};
|
||||||
|
|
||||||
var query = await RoomToPharmacyModel.list(filter);
|
|
||||||
|
|
||||||
const offset = (param.page - 1) * param.limit;
|
const offset = (param.page - 1) * param.limit;
|
||||||
let limit = param.limit;
|
const limit = param.limit;
|
||||||
|
|
||||||
const res_count = query;
|
const roomQuery = OrmHelper.DB.getRepository(Room)
|
||||||
const res_list = query
|
.createQueryBuilder("room")
|
||||||
.orderBy("room_to_pharmacy." + param.order_field, param.order_direction)
|
.leftJoinAndSelect("room.department", "department")
|
||||||
.offset(offset)
|
.leftJoinAndMapMany(
|
||||||
.limit(limit);
|
"room.roomToPharmacies",
|
||||||
|
RoomToPharmacy,
|
||||||
|
"room_to_pharmacy",
|
||||||
|
"room_to_pharmacy.room_id = room.id"
|
||||||
|
)
|
||||||
|
.leftJoinAndMapOne(
|
||||||
|
"room_to_pharmacy.pharmacy_room",
|
||||||
|
Room,
|
||||||
|
"pharmacy_room",
|
||||||
|
"pharmacy_room.id = room_to_pharmacy.pharmacy_room_id"
|
||||||
|
)
|
||||||
|
.where("department.id IN (:...departmentIds)", {
|
||||||
|
departmentIds: [
|
||||||
|
'42bc42a7-e2a7-4f95-a788-9d9e51a51a20',
|
||||||
|
'3fc39ff1-b4bc-4276-92ef-def7ca9432fb',
|
||||||
|
'a0d2ef11-6927-4e5c-8628-6acf26351071',
|
||||||
|
]
|
||||||
|
})
|
||||||
|
.orderBy("room." + param.order_field, param.order_direction as "ASC" | "DESC");
|
||||||
|
|
||||||
if (param.with_deleted) {
|
if (param.with_deleted) {
|
||||||
res_count.withDeleted();
|
roomQuery.withDeleted();
|
||||||
res_list.withDeleted();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const current_page = param.page;
|
// Filter tambahan jika ada
|
||||||
const total_count_data = await res_count.getCount();
|
if (filter.room) {
|
||||||
const list_data = await res_list.getMany();
|
roomQuery.andWhere("room.room ILIKE :room", { room: `%${filter.room}%` });
|
||||||
const count_data = CommonHelper.countObject(list_data);
|
}
|
||||||
|
if (filter.status) {
|
||||||
|
roomQuery.andWhere("room.status = :status", { status: filter.status });
|
||||||
|
}
|
||||||
|
|
||||||
return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, list_data);
|
const total_count_data = await roomQuery.clone().getCount();
|
||||||
|
|
||||||
|
const rooms = await roomQuery
|
||||||
|
.offset(offset)
|
||||||
|
.limit(limit)
|
||||||
|
.getMany();
|
||||||
|
|
||||||
|
// Transform: { room_name: { pharmacy_room_1: {}, pharmacy_room_2: {} } }
|
||||||
|
const list_data_return = rooms.map((room: any) => {
|
||||||
|
const roomToPharmacies: any[] = room.roomToPharmacies ?? [];
|
||||||
|
|
||||||
|
const pharmacyRooms: Record<string, any> = {};
|
||||||
|
roomToPharmacies.forEach((rtp: any, index: number) => {
|
||||||
|
const pharmacy: Room | null = rtp.pharmacy_room ?? null;
|
||||||
|
pharmacyRooms[`pharmacy_room_${index + 1}`] = pharmacy ? {
|
||||||
|
id: pharmacy.id,
|
||||||
|
room: pharmacy.room,
|
||||||
|
code: pharmacy.code,
|
||||||
|
description: pharmacy.description ?? null,
|
||||||
|
status: pharmacy.status,
|
||||||
|
} : null;
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
rooms: {
|
||||||
|
id: room.id,
|
||||||
|
room: room.room,
|
||||||
|
code: room.code,
|
||||||
|
description: room.description ?? null,
|
||||||
|
status: room.status,
|
||||||
|
department: room.department ? {
|
||||||
|
id: room.department.id,
|
||||||
|
name: room.department.name,
|
||||||
|
} : null,
|
||||||
|
pharmacy_rooms: Object.keys(pharmacyRooms).length > 0
|
||||||
|
? pharmacyRooms
|
||||||
|
: null,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const count_data = CommonHelper.countObject(list_data_return);
|
||||||
|
|
||||||
|
return ReturnHelper.successResponselist(
|
||||||
|
res,
|
||||||
|
200,
|
||||||
|
Language.lang.success_view,
|
||||||
|
count_data,
|
||||||
|
param.page,
|
||||||
|
total_count_data,
|
||||||
|
list_data_return
|
||||||
|
);
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
log.error(e);
|
log.error(e);
|
||||||
const err = e as Error;
|
const err = e as Error;
|
||||||
@ -110,7 +178,7 @@ export class RoomToPharmacyController {
|
|||||||
|
|
||||||
var query = await RoomToPharmacyModel.list(filter);
|
var query = await RoomToPharmacyModel.list(filter);
|
||||||
const data = await query.getMany();
|
const data = await query.getMany();
|
||||||
|
|
||||||
const filename = "room_to_pharmacy.csv";
|
const filename = "room_to_pharmacy.csv";
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'text/csv');
|
res.setHeader('Content-Type', 'text/csv');
|
||||||
@ -173,15 +241,15 @@ export class RoomToPharmacyController {
|
|||||||
try {
|
try {
|
||||||
const schema = Joi.object().keys({
|
const schema = Joi.object().keys({
|
||||||
pharmacy_room_id: Joi.string()
|
pharmacy_room_id: Joi.string()
|
||||||
.uuid()
|
.uuid()
|
||||||
.required()
|
.required()
|
||||||
.label("Pharmacy Room ID"),
|
.label("Pharmacy Room ID"),
|
||||||
|
|
||||||
room_id: Joi.string()
|
room_id: Joi.string()
|
||||||
.uuid()
|
.uuid()
|
||||||
.required()
|
.required()
|
||||||
.label("Room ID"),
|
.label("Room ID"),
|
||||||
|
|
||||||
// room_ids: Joi.array()
|
// room_ids: Joi.array()
|
||||||
// .items(Joi.string().uuid().required())
|
// .items(Joi.string().uuid().required())
|
||||||
// .min(1)
|
// .min(1)
|
||||||
@ -229,7 +297,7 @@ export class RoomToPharmacyController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async update(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
static async update(req: Request, res: Response, next: NextFunction): Promise<Response> {
|
||||||
/*
|
/*
|
||||||
#swagger.tags = ['RoomToPharmacy']
|
#swagger.tags = ['RoomToPharmacy']
|
||||||
#swagger.security = [{
|
#swagger.security = [{
|
||||||
@ -253,15 +321,15 @@ export class RoomToPharmacyController {
|
|||||||
id: Joi.string().uuid().required().label('ID'),
|
id: Joi.string().uuid().required().label('ID'),
|
||||||
|
|
||||||
pharmacy_room_id: Joi.string()
|
pharmacy_room_id: Joi.string()
|
||||||
.uuid()
|
.uuid()
|
||||||
.required()
|
.required()
|
||||||
.label("Pharmacy Room ID"),
|
.label("Pharmacy Room ID"),
|
||||||
|
|
||||||
room_id: Joi.string()
|
room_id: Joi.string()
|
||||||
.uuid()
|
.uuid()
|
||||||
.required()
|
.required()
|
||||||
.label("Room ID"),
|
.label("Room ID"),
|
||||||
|
|
||||||
// room_ids: Joi.array()
|
// room_ids: Joi.array()
|
||||||
// .items(Joi.string().uuid().required())
|
// .items(Joi.string().uuid().required())
|
||||||
// .min(1)
|
// .min(1)
|
||||||
@ -274,7 +342,7 @@ export class RoomToPharmacyController {
|
|||||||
const param: any = await schema.validateAsync(req.body);
|
const param: any = await schema.validateAsync(req.body);
|
||||||
|
|
||||||
const repo = OrmHelper.DB.getRepository(RoomToPharmacy);
|
const repo = OrmHelper.DB.getRepository(RoomToPharmacy);
|
||||||
|
|
||||||
const data = await repo.findOneBy({ id: param.id });
|
const data = await repo.findOneBy({ id: param.id });
|
||||||
|
|
||||||
if (data != null) {
|
if (data != null) {
|
||||||
@ -295,26 +363,26 @@ export class RoomToPharmacyController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async detail(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
static async detail(req: Request, res: Response, next: NextFunction): Promise<void | Response> {
|
||||||
/*
|
/*
|
||||||
#swagger.tags = ['RoomToPharmacy']
|
#swagger.tags = ['RoomToPharmacy']
|
||||||
#swagger.security = [{ "bearerAuth": [] }]
|
#swagger.security = [{ "bearerAuth": [] }]
|
||||||
#swagger.parameters['id'] = { description: '', in: 'path', type: 'string' }
|
#swagger.parameters['id'] = { description: '', in: 'path', type: 'string' }
|
||||||
*/
|
*/
|
||||||
try {
|
try {
|
||||||
const schema = Joi.object().keys({
|
const schema = Joi.object().keys({
|
||||||
id: Joi.string().uuid().required().label("RoomToPharmacy Id"),
|
id: Joi.string().uuid().required().label("RoomToPharmacy Id"),
|
||||||
});
|
});
|
||||||
|
|
||||||
let param: any = await schema.validateAsync(req.params);
|
let param: any = await schema.validateAsync(req.params);
|
||||||
|
|
||||||
let data = await RoomToPharmacyModel.list({ "id": param.id }).then((q) => q.getOne());
|
let data = await RoomToPharmacyModel.list({ "id": param.id }).then((q) => q.getOne());
|
||||||
if (!data) throw { message: "RoomToPharmacy Not Found" };
|
if (!data) throw { message: "RoomToPharmacy Not Found" };
|
||||||
|
|
||||||
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, data);
|
return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, data);
|
||||||
} catch (e: unknown) {
|
} catch (e: unknown) {
|
||||||
log.error(e);
|
log.error(e);
|
||||||
const err = e as Error;
|
const err = e as Error;
|
||||||
return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_not_found, err.message);
|
return ReturnHelper.errorResponse(res, 400, 401, Language.lang.failed_not_found, err.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,24 +1,34 @@
|
|||||||
import { SelectQueryBuilder } from "typeorm";
|
import { SelectQueryBuilder } from "typeorm";
|
||||||
import CommonHelper from "../helpers/common";
|
import CommonHelper from "../helpers/common";
|
||||||
import { OrmHelper } from "../helpers/orm";
|
import { OrmHelper } from "../helpers/orm";
|
||||||
import { RoomToPharmacy } from "entity";
|
import { RoomToPharmacy, Room } from "entity";
|
||||||
|
|
||||||
export class RoomToPharmacyModel {
|
export class RoomToPharmacyModel {
|
||||||
static async list(filter = {}): Promise<SelectQueryBuilder<any>> {
|
static async list(filter = {}): Promise<SelectQueryBuilder<any>> {
|
||||||
const repo = OrmHelper.DB.getRepository(RoomToPharmacy);
|
const repo = OrmHelper.DB.getRepository(Room);
|
||||||
let whereAttr: string[] = [];
|
|
||||||
let whereVal: any = {};
|
|
||||||
if (filter && Object.keys(filter).length > 0) {
|
|
||||||
whereAttr = [...whereAttr, ...CommonHelper.handleQueryFilter(filter, "room_to_pharmacy").whereAttr];
|
|
||||||
whereVal = { ...whereVal, ...CommonHelper.handleQueryFilter(filter, "room_to_pharmacy").whereVal };
|
|
||||||
}
|
|
||||||
|
|
||||||
var query = repo.createQueryBuilder("room_to_pharmacy")
|
const query = repo
|
||||||
.leftJoinAndSelect("room_to_pharmacy.room", "room")
|
.createQueryBuilder("room")
|
||||||
.leftJoinAndSelect("room_to_pharmacy.pharmacy_room", "pharmacy_room");
|
.leftJoinAndSelect("room.department", "department")
|
||||||
if (whereAttr.length != 0) {
|
.leftJoinAndMapMany(
|
||||||
query = query.where(whereAttr.join(" and "), whereVal);
|
"room.roomToPharmacies",
|
||||||
}
|
RoomToPharmacy,
|
||||||
|
"room_to_pharmacy",
|
||||||
|
"room_to_pharmacy.room_id = room.id"
|
||||||
|
)
|
||||||
|
.leftJoinAndMapOne(
|
||||||
|
"room_to_pharmacy.pharmacy_room",
|
||||||
|
Room,
|
||||||
|
"pharmacy_room",
|
||||||
|
"pharmacy_room.id = room_to_pharmacy.pharmacy_room_id"
|
||||||
|
)
|
||||||
|
.where("department.id IN (:...departmentIds)", {
|
||||||
|
departmentIds: [
|
||||||
|
'42bc42a7-e2a7-4f95-a788-9d9e51a51a20',
|
||||||
|
'3fc39ff1-b4bc-4276-92ef-def7ca9432fb',
|
||||||
|
'a0d2ef11-6927-4e5c-8628-6acf26351071',
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user