97 lines
2.9 KiB
JavaScript
97 lines
2.9 KiB
JavaScript
'use strict';
|
|
|
|
const UserTypes = require('../helpers/constants').UserTypes,
|
|
utils = require('../helpers/utils'),
|
|
polyUtil = require('../helpers/poly_util'),
|
|
jobUtil = require('../helpers/job_util'),
|
|
Job = require('../model/job'),
|
|
Areas = require('../model/area'),
|
|
ObjectId = require('mongodb').ObjectId,
|
|
{ Errors } = require('../helpers/constants'),
|
|
{ AppParamError, AppError } = require('../helpers/app_error');
|
|
|
|
async function search_post(req, res) {
|
|
const ops = req.body;
|
|
const uType = req.ut;
|
|
|
|
if (!ops || !utils.isNumber(ops.jobId) || !ObjectId.isValid(ops.byPuid)) AppParamError.throw();
|
|
|
|
// TODO: if possible, replace this with client population instead of lookup
|
|
const pipeline = [
|
|
{ $match: { _id: Number(ops.jobId) } },
|
|
{
|
|
$lookup: {
|
|
from: "users",
|
|
localField: "client",
|
|
foreignField: "_id",
|
|
as: "client"
|
|
}
|
|
},
|
|
{ $unwind: "$client" }
|
|
];
|
|
if (uType === UserTypes.CLIENT) {
|
|
pipeline.push({ $match: { "client": ObjectId(req.uid) } });
|
|
}
|
|
else if (uType > 0) {
|
|
pipeline.push({ $match: { "byPuid": ObjectId(ops.byPuid) } });
|
|
}
|
|
pipeline.push({ $project: { sprayAreas: 1, excludedAreas: 1, client: 1 } });
|
|
|
|
const jobs = await Job.aggregate(pipeline);
|
|
if (!jobs.length) AppError.throw(Errors.JOB_NOT_FOUND);
|
|
|
|
let items = [];
|
|
items = utils.appendArray(items, jobs[0].sprayAreas);
|
|
items = utils.appendArray(items, jobs[0].excludedAreas);
|
|
|
|
res.json(polyUtil.toGeoItems(items));
|
|
}
|
|
|
|
async function addToLibrary_post(req, res) {
|
|
const ops = req.body;
|
|
if (!ops || utils.isEmptyArray(ops.areas)) AppParamError.throw();
|
|
|
|
const data = await jobUtil.addAreasToLib(ops.areas, { returnAreas: false, clientId: ops.clientId, debug: !req.app.isProd });
|
|
res.json({ dup: data.dup });
|
|
}
|
|
|
|
async function update_post(req, res) {
|
|
const upOps = req.body;
|
|
if (!upOps || utils.isEmptyArray(upOps.c) && utils.isEmptyArray(upOps.u) && utils.isEmptyArray(upOps.r))
|
|
return res.end();
|
|
// console.log(upOps);
|
|
let bulkOps = [];
|
|
|
|
if (!utils.isEmptyArray(upOps.r)) {
|
|
bulkOps = bulkOps.concat(upOps.r.map(it => ({ deleteOne: { filter: { _id: ObjectId(it) } } })));
|
|
}
|
|
|
|
const mods = (upOps.c || []).concat(upOps.u || []);
|
|
if (mods.length) {
|
|
bulkOps = bulkOps.concat(mods.map(it => {
|
|
const id = it['_id'];
|
|
delete it['_id'];
|
|
return id ?
|
|
{ updateOne: { filter: { _id: ObjectId(id) }, update: it } }
|
|
: {
|
|
insertOne: { document: it }
|
|
}
|
|
}));
|
|
}
|
|
await Areas.bulkWrite(bulkOps);
|
|
res.json({ ok: true });
|
|
}
|
|
|
|
async function findAreas_post(req, res) {
|
|
const ops = req.body;
|
|
if (!ops || !ObjectId.isValid(ops.clientId)) AppParamError.throw();
|
|
|
|
const areas = await Areas.find({ client: ObjectId(ops.clientId) }, null, { lean: true }).populate('properties.crop', 'name color');
|
|
const items = polyUtil.toGeoItems(areas);
|
|
|
|
res.json(items);
|
|
}
|
|
|
|
module.exports = {
|
|
search_post, addToLibrary_post, update_post, findAreas_post
|
|
} |