223 lines
5.6 KiB
TypeScript
223 lines
5.6 KiB
TypeScript
import {
|
|
CreateRtsInput,
|
|
rtsFields,
|
|
rtsModel,
|
|
UpdateRtsInput,
|
|
UploadRtsInput,
|
|
} from './rts.schema';
|
|
import { AuthenticatedUser } from '../auth';
|
|
import { generateId } from '../utils/id';
|
|
import { getFilterObject, getSortObject, PageQueryParams } from '../pagination';
|
|
import { getUser } from '../user/user.service';
|
|
import { orgModel } from '../organization/organization.schema';
|
|
import { userModel } from '../user/user.schema';
|
|
|
|
export async function createRts(
|
|
input: CreateRtsInput,
|
|
user: AuthenticatedUser
|
|
) {
|
|
let defaultClient = null;
|
|
const userInDb = await getUser(user.userId);
|
|
if (userInDb && userInDb.defaultClient) {
|
|
defaultClient = userInDb.defaultClient;
|
|
}
|
|
|
|
if (!input.files) {
|
|
return await rtsModel.create({
|
|
...input,
|
|
tenantId: user.tenantId,
|
|
pid: generateId(),
|
|
client: defaultClient,
|
|
createdAt: new Date(),
|
|
createdBy: user.userId ?? null,
|
|
});
|
|
} else {
|
|
return await rtsModel.create({
|
|
tenantId: user.tenantId,
|
|
pid: generateId(),
|
|
county: input.county,
|
|
client: input.client,
|
|
documents: [
|
|
{
|
|
files: input.files,
|
|
createdAt: new Date(),
|
|
createdBy: user.userId ?? null,
|
|
},
|
|
],
|
|
createdAt: new Date(),
|
|
createdBy: user.userId ?? null,
|
|
});
|
|
}
|
|
}
|
|
|
|
export async function getRts(id: string, tenantId: string) {
|
|
return await rtsModel
|
|
.findOne({ pid: id, tenantId: tenantId })
|
|
.populate({ path: 'county', select: 'pid name avatar' })
|
|
.populate({ path: 'client', select: 'pid name avatar' })
|
|
.populate({ path: 'createdBy', select: 'pid name avatar' });
|
|
}
|
|
|
|
export async function listRts(params: PageQueryParams, tenantId: string) {
|
|
const page = params.page || 1;
|
|
const pageSize = params.pageSize || 10;
|
|
const sortObj = getSortObject(params, rtsFields);
|
|
const filterObj = getFilterObject(params);
|
|
|
|
const rtsList = await rtsModel.aggregate([
|
|
{
|
|
$match: { $and: [{ tenantId: tenantId }, { ...filterObj }] },
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: 'organizations',
|
|
localField: 'county',
|
|
foreignField: '_id',
|
|
as: 'countyRec',
|
|
},
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: 'organizations',
|
|
localField: 'client',
|
|
foreignField: '_id',
|
|
as: 'clientRec',
|
|
},
|
|
},
|
|
{
|
|
$lookup: {
|
|
from: 'users',
|
|
localField: 'createdBy',
|
|
foreignField: '_id',
|
|
as: 'createdRec',
|
|
},
|
|
},
|
|
{
|
|
$project: {
|
|
_id: 1,
|
|
pid: 1,
|
|
documents: 1,
|
|
statusPipeline: 1,
|
|
createdAt: 1,
|
|
county: {
|
|
$let: {
|
|
vars: { county: { $arrayElemAt: ['$countyRec', 0] } },
|
|
in: {
|
|
_id: '$$county._id',
|
|
pid: '$$county.pid',
|
|
name: '$$county.name',
|
|
type: '$$county.type',
|
|
avatar: '$$county.avatar',
|
|
},
|
|
},
|
|
},
|
|
client: {
|
|
$let: {
|
|
vars: { client: { $arrayElemAt: ['$clientRec', 0] } },
|
|
in: {
|
|
_id: '$$client._id',
|
|
pid: '$$client.pid',
|
|
name: '$$client.name',
|
|
type: '$$client.type',
|
|
avatar: '$$client.avatar',
|
|
},
|
|
},
|
|
},
|
|
createdBy: {
|
|
$let: {
|
|
vars: { created: { $arrayElemAt: ['$createdRec', 0] } },
|
|
in: {
|
|
_id: '$$created._id',
|
|
pid: '$$created.pid',
|
|
name: '$$created.name',
|
|
avatar: '$$created.avatar',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
$facet: {
|
|
metadata: [{ $count: 'count' }],
|
|
data: [
|
|
{ $sort: sortObj },
|
|
{ $skip: (page - 1) * pageSize },
|
|
{ $limit: pageSize },
|
|
],
|
|
},
|
|
},
|
|
]);
|
|
|
|
if (rtsList[0].data.length === 0)
|
|
return { rts: [], metadata: { count: 0, page, pageSize } };
|
|
|
|
return {
|
|
rts: rtsList[0]?.data,
|
|
metadata: {
|
|
count: rtsList[0].metadata[0].count,
|
|
page,
|
|
pageSize,
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function updateRts(
|
|
id: string,
|
|
input: UpdateRtsInput,
|
|
tenantId: string
|
|
) {
|
|
const updatedRts = await rtsModel
|
|
.findOneAndUpdate({ pid: id, tenantId: tenantId }, input, { new: true })
|
|
.populate({ path: 'createdBy', select: 'pid name avatar' })
|
|
.populate({ path: 'county', select: 'pid name avatar' })
|
|
.populate({ path: 'client', select: 'pid name avatar' });
|
|
|
|
return updatedRts;
|
|
}
|
|
|
|
export async function deleteRts(id: string, tenantId: string) {
|
|
return await rtsModel.deleteOne({ pid: id, tenantId: tenantId });
|
|
}
|
|
|
|
export async function newUpload(
|
|
id: string,
|
|
newUpload: UploadRtsInput,
|
|
user: AuthenticatedUser
|
|
) {
|
|
return await rtsModel.findOneAndUpdate(
|
|
{ pid: id, tenantId: user.tenantId },
|
|
{
|
|
$push: {
|
|
documents: {
|
|
files: newUpload.files,
|
|
createdAt: new Date(),
|
|
createdBy: user.userId,
|
|
},
|
|
},
|
|
}
|
|
);
|
|
}
|
|
|
|
export async function getUniqueValuesRTS(field: string, tenenatId: string) {
|
|
let values = await rtsModel.distinct(field, { tenantId: tenenatId });
|
|
|
|
let matchedValues = [];
|
|
if (field === 'county') {
|
|
matchedValues = await orgModel.find().where('_id').in(values).exec();
|
|
} else if (field === 'client') {
|
|
matchedValues = await orgModel.find().where('_id').in(values).exec();
|
|
} else if (field === 'createdBy') {
|
|
matchedValues = await userModel.find().where('_id').in(values).exec();
|
|
}
|
|
|
|
if (matchedValues.length > 0) {
|
|
const newValues = {};
|
|
for (const item of matchedValues) {
|
|
newValues[item.id] = item.name;
|
|
}
|
|
return newValues;
|
|
}
|
|
|
|
return values;
|
|
}
|