|
|
|
|
import BaseApi from "~/src/api/BaseApi";
|
|
|
|
|
import _ from "lodash";
|
|
|
|
|
import { Joi } from "celebrate";
|
|
|
|
|
import db from "../../database";
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
description,
|
|
|
|
|
request,
|
|
|
|
|
test,
|
|
|
|
|
response,
|
|
|
|
|
GET,
|
|
|
|
|
POST,
|
|
|
|
|
PUT,
|
|
|
|
|
DELETE,
|
|
|
|
|
SOCKET,
|
|
|
|
|
isPublic,
|
|
|
|
|
cache,
|
|
|
|
|
PRIVATE,
|
|
|
|
|
} = BaseApi;
|
|
|
|
|
|
|
|
|
|
export default class waterSample extends BaseApi {
|
|
|
|
|
description = "سرویس نمونه گیری آب";
|
|
|
|
|
///////////////////////////////
|
|
|
|
|
|
|
|
|
|
@PRIVATE
|
|
|
|
|
findWorkFlowId = (type) => {
|
|
|
|
|
const typePeaces = type.split(" ");
|
|
|
|
|
if (typePeaces.includes("آشامیدنی")) return 11;
|
|
|
|
|
else if (typePeaces.includes("سامانه")) return 14;
|
|
|
|
|
else if (typePeaces.includes("معدنی")) return 11;
|
|
|
|
|
else if (typePeaces.includes("بسته")) return 11;
|
|
|
|
|
else if (typePeaces.includes("استخر")) return 15;
|
|
|
|
|
else if (typePeaces.includes("شناگاه")) return 17;
|
|
|
|
|
else if (typePeaces.includes("یخ")) return 13;
|
|
|
|
|
else if (type.split("/").includes("فاضلاب")) return 16;
|
|
|
|
|
else return -1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
|
@description("دریافت لیست نمونه گیری آبها")
|
|
|
|
|
@request(
|
|
|
|
|
optional({
|
|
|
|
|
..._.pick(db.waterSample.validation, db.waterSample.getList),
|
|
|
|
|
pageIndex: Joi.number().min(1).description("شمارهی صفحه"),
|
|
|
|
|
pageSize: Joi.number().min(1).description("سایز صفحه"),
|
|
|
|
|
sort: Joi.string().description("پارامتر مرتب سازی"),
|
|
|
|
|
asc: Joi.bool().description("آیا خروجی به صورت صعودی مرتب شود؟"),
|
|
|
|
|
processId: Joi.number().description("شناسهی فرایند"),
|
|
|
|
|
checklistId: Joi.number().description("شناسهی چکلیست"),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
@response()
|
|
|
|
|
list = async (data) => {
|
|
|
|
|
data = omitNull(data);
|
|
|
|
|
|
|
|
|
|
let where = _.pick(data, db.waterSample.getList);
|
|
|
|
|
for (const key in where) {
|
|
|
|
|
if (db.waterSample.validation[key].type == "string")
|
|
|
|
|
where[key] = { [Op.substring]: where[key] };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const limit = +data.pageSize || 20;
|
|
|
|
|
const offset =
|
|
|
|
|
data.pageIndex && data.pageSize
|
|
|
|
|
? data.pageSize * (data.pageIndex - 1)
|
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
|
|
let order = ["id"];
|
|
|
|
|
if (data.sort && (data.asc == false || data.asc == "false")) {
|
|
|
|
|
order = [[data.sort, "DESC"]];
|
|
|
|
|
}
|
|
|
|
|
if (data.sort && (data.asc == true || data.asc == "true" || !data.asc)) {
|
|
|
|
|
order = [[data.sort, "ASC"]];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await db.waterSample.findAndCountAll({
|
|
|
|
|
where,
|
|
|
|
|
order,
|
|
|
|
|
offset,
|
|
|
|
|
limit,
|
|
|
|
|
raw: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@GET
|
|
|
|
|
@description("دریافت اطلاعات نمونه گیری آب")
|
|
|
|
|
@request(_.pick(db.waterSample.validation, ["id"]))
|
|
|
|
|
@response()
|
|
|
|
|
info = async (data) => {
|
|
|
|
|
const result = await db.waterSample.findByPk(data.id, {
|
|
|
|
|
raw: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
message: "شناسه نمونه گیری آب نامعتبر است.",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
@description("افزودن نمونه گیری آب")
|
|
|
|
|
@request(_.pick(db.waterSample.validation, db.waterSample.add))
|
|
|
|
|
@response(_.pick(db.waterSample.validation, ["id"]))
|
|
|
|
|
add = async (data) => {
|
|
|
|
|
const checkedData = _.pick(omitNull(data), db.waterSample.add);
|
|
|
|
|
|
|
|
|
|
checkedData.workflowId = this.findWorkFlowId(checkedData.type);
|
|
|
|
|
|
|
|
|
|
const result = await db.waterSample.create(checkedData);
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 400,
|
|
|
|
|
message: "بروز خطا. درخواست انجام نشد",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return { id: result.dataValues.id };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@PUT
|
|
|
|
|
@description("ویرایش نمونه گیری آب")
|
|
|
|
|
@request({
|
|
|
|
|
id: Joi.number().required().description("شناسه نمونه گیری آب"),
|
|
|
|
|
...optional(_.pick(db.waterSample.validation, db.waterSample.updateList)),
|
|
|
|
|
})
|
|
|
|
|
@response({})
|
|
|
|
|
update = async (data) => {
|
|
|
|
|
const checkedData = _.pick(omitNull(data), db.waterSample.updateList);
|
|
|
|
|
if (!Object.keys(checkedData).length)
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 400,
|
|
|
|
|
message: "بروز خطا در اطلاعات ورودی",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { id } = data;
|
|
|
|
|
|
|
|
|
|
if (checkedData.type)
|
|
|
|
|
checkedData.workflowId = this.findWorkFlowId(checkedData.type);
|
|
|
|
|
|
|
|
|
|
const result = await db.waterSample.update(checkedData, {
|
|
|
|
|
where: { id },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 400,
|
|
|
|
|
message: "شناسه واحد آب آشامیدنی نامعتبر است.",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@DELETE
|
|
|
|
|
@description("حذف نمونه گیری آب")
|
|
|
|
|
@request(_.pick(db.waterSample.validation, ["id"]))
|
|
|
|
|
@response({})
|
|
|
|
|
delete = async (data) => {
|
|
|
|
|
const { id } = data;
|
|
|
|
|
|
|
|
|
|
const result = await db.waterSample.destroy({ where: { id } });
|
|
|
|
|
|
|
|
|
|
if (!result)
|
|
|
|
|
return {
|
|
|
|
|
statusCode: 404,
|
|
|
|
|
message: "شناسه نمونه گیری آب نامعتبر است.",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
};
|
|
|
|
|
}
|