up
This commit is contained in:
@ -22,7 +22,7 @@
|
|||||||
"database": {
|
"database": {
|
||||||
"engine": "postgres",
|
"engine": "postgres",
|
||||||
"host": "127.0.0.1",
|
"host": "127.0.0.1",
|
||||||
"port": "15432",
|
"port": "1520",
|
||||||
"username": "saude_stag",
|
"username": "saude_stag",
|
||||||
"password": "gM*#o>3W4&5X",
|
"password": "gM*#o>3W4&5X",
|
||||||
"database": "saude_stag",
|
"database": "saude_stag",
|
||||||
|
|||||||
@ -3,6 +3,8 @@ import { NextFunction, Response } from "express";
|
|||||||
import { Request } from "express-jwt";
|
import { Request } from "express-jwt";
|
||||||
import Joi from "joi";
|
import Joi from "joi";
|
||||||
import { ILogObj, Logger } from "tslog";
|
import { ILogObj, Logger } from "tslog";
|
||||||
|
import { Brackets } from "typeorm";
|
||||||
|
import { validate as validateUuid } from "uuid";
|
||||||
import CommonHelper from "../helpers/common";
|
import CommonHelper from "../helpers/common";
|
||||||
import { ReturnHelper } from "../helpers/express/return";
|
import { ReturnHelper } from "../helpers/express/return";
|
||||||
import { Language } from "../langs/lang";
|
import { Language } from "../langs/lang";
|
||||||
@ -23,7 +25,7 @@ export class DiagnosisController {
|
|||||||
"bearerAuth": []
|
"bearerAuth": []
|
||||||
}]
|
}]
|
||||||
#swagger.parameters['filter'] = {
|
#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',
|
in: 'query',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
}
|
}
|
||||||
@ -68,9 +70,49 @@ export class DiagnosisController {
|
|||||||
|
|
||||||
let param: Paging = await schema.validateAsync(req.query);
|
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;
|
const offset = (param.page - 1) * param.limit;
|
||||||
let limit = param.limit;
|
let limit = param.limit;
|
||||||
|
|||||||
@ -6,15 +6,14 @@ import { OrmHelper } from "../helpers/orm";
|
|||||||
export class DiagnosisModel {
|
export class DiagnosisModel {
|
||||||
static async list(filter = {}): Promise<SelectQueryBuilder<any>> {
|
static async list(filter = {}): Promise<SelectQueryBuilder<any>> {
|
||||||
const repo = OrmHelper.DB.getRepository(Diagnosis);
|
const repo = OrmHelper.DB.getRepository(Diagnosis);
|
||||||
let whereAttr = [];
|
let whereAttr: string[] = [];
|
||||||
let whereVal: any = {};
|
let whereVal: any = {};
|
||||||
if (filter) {
|
if (filter && Object.keys(filter).length > 0) {
|
||||||
whereAttr = [...whereAttr, ...CommonHelper.handleQueryFilter(filter).whereAttr];
|
whereAttr = [...whereAttr, ...CommonHelper.handleQueryFilter(filter).whereAttr];
|
||||||
whereVal = { ...whereVal, ...CommonHelper.handleQueryFilter(filter).whereVal };
|
whereVal = { ...whereVal, ...CommonHelper.handleQueryFilter(filter).whereVal };
|
||||||
}
|
}
|
||||||
|
|
||||||
var query = null;
|
var query = repo.createQueryBuilder("Diagnosis");
|
||||||
query = repo.createQueryBuilder("Diagnosis");
|
|
||||||
if (whereAttr.length != 0) {
|
if (whereAttr.length != 0) {
|
||||||
query = query.where(whereAttr.join(" and "), whereVal);
|
query = query.where(whereAttr.join(" and "), whereVal);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user