up
This commit is contained in:
@ -22,7 +22,7 @@
|
||||
"database": {
|
||||
"engine": "postgres",
|
||||
"host": "127.0.0.1",
|
||||
"port": "15432",
|
||||
"port": "1520",
|
||||
"username": "saude_stag",
|
||||
"password": "gM*#o>3W4&5X",
|
||||
"database": "saude_stag",
|
||||
|
||||
@ -3,6 +3,8 @@ import { NextFunction, Response } from "express";
|
||||
import { Request } from "express-jwt";
|
||||
import Joi from "joi";
|
||||
import { ILogObj, Logger } from "tslog";
|
||||
import { Brackets } from "typeorm";
|
||||
import { validate as validateUuid } from "uuid";
|
||||
import CommonHelper from "../helpers/common";
|
||||
import { ReturnHelper } from "../helpers/express/return";
|
||||
import { Language } from "../langs/lang";
|
||||
@ -23,7 +25,7 @@ export class DiagnosisController {
|
||||
"bearerAuth": []
|
||||
}]
|
||||
#swagger.parameters['filter'] = {
|
||||
description: 'Filter: plain text (like %name%) or JSON object with status, code, etc.',
|
||||
description: 'Filter: JSON object; Diagnosis.code and Diagnosis.diagnosis are combined with OR; other fields use AND.',
|
||||
in: 'query',
|
||||
type: 'string',
|
||||
}
|
||||
@ -68,9 +70,49 @@ export class DiagnosisController {
|
||||
|
||||
let param: Paging = await schema.validateAsync(req.query);
|
||||
|
||||
let filter = JSON.parse(param.filter);
|
||||
let filter = param.filter && param.filter !== "" ? JSON.parse(param.filter) : {};
|
||||
|
||||
var query = await DiagnosisModel.list(filter);
|
||||
const codeKey = "Diagnosis.code";
|
||||
const diagnosisKey = "Diagnosis.diagnosis";
|
||||
const orCode = filter[codeKey];
|
||||
const orDiagnosis = filter[diagnosisKey];
|
||||
const restFilter = { ...filter };
|
||||
delete restFilter[codeKey];
|
||||
delete restFilter[diagnosisKey];
|
||||
|
||||
var query = await DiagnosisModel.list(restFilter);
|
||||
|
||||
if (orCode !== undefined || orDiagnosis !== undefined) {
|
||||
query.andWhere(
|
||||
new Brackets((qb) => {
|
||||
let isFirst = true;
|
||||
const addOr = (column: string, paramKey: string, raw: any) => {
|
||||
const op = isFirst ? "where" : "orWhere";
|
||||
isFirst = false;
|
||||
if (Array.isArray(raw)) {
|
||||
(qb as any)[op](`${column} IN (:...${paramKey})`, { [paramKey]: raw });
|
||||
return;
|
||||
}
|
||||
const f = String(raw);
|
||||
if (validateUuid(f)) {
|
||||
(qb as any)[op](`${column} = :${paramKey}`, { [paramKey]: f });
|
||||
return;
|
||||
}
|
||||
if (f === "null") {
|
||||
(qb as any)[op](`${column} IS NULL`);
|
||||
return;
|
||||
}
|
||||
if (f.includes("%")) {
|
||||
(qb as any)[op](`${column} ILIKE :${paramKey}`, { [paramKey]: f });
|
||||
return;
|
||||
}
|
||||
(qb as any)[op](`${column} = :${paramKey}`, { [paramKey]: f });
|
||||
};
|
||||
if (orCode !== undefined) addOr("Diagnosis.code", "diag_or_code", orCode);
|
||||
if (orDiagnosis !== undefined) addOr("Diagnosis.diagnosis", "diag_or_diagnosis", orDiagnosis);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const offset = (param.page - 1) * param.limit;
|
||||
let limit = param.limit;
|
||||
|
||||
@ -6,15 +6,14 @@ import { OrmHelper } from "../helpers/orm";
|
||||
export class DiagnosisModel {
|
||||
static async list(filter = {}): Promise<SelectQueryBuilder<any>> {
|
||||
const repo = OrmHelper.DB.getRepository(Diagnosis);
|
||||
let whereAttr = [];
|
||||
let whereAttr: string[] = [];
|
||||
let whereVal: any = {};
|
||||
if (filter) {
|
||||
if (filter && Object.keys(filter).length > 0) {
|
||||
whereAttr = [...whereAttr, ...CommonHelper.handleQueryFilter(filter).whereAttr];
|
||||
whereVal = { ...whereVal, ...CommonHelper.handleQueryFilter(filter).whereVal };
|
||||
}
|
||||
|
||||
var query = null;
|
||||
query = repo.createQueryBuilder("Diagnosis");
|
||||
var query = repo.createQueryBuilder("Diagnosis");
|
||||
if (whereAttr.length != 0) {
|
||||
query = query.where(whereAttr.join(" and "), whereVal);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user