add rts routes
This commit is contained in:
154
src/rts/rts.service.ts
Normal file
154
src/rts/rts.service.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
import {
|
||||
CreateRtsInput,
|
||||
rtsFields,
|
||||
rtsModel,
|
||||
UpdateRtsInput,
|
||||
UploadRtsInput,
|
||||
} from "./rts.schema";
|
||||
import { AuthenticatedUser } from "../auth";
|
||||
import { generateId } from "../utils/id";
|
||||
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
|
||||
|
||||
export async function createRts(
|
||||
input: CreateRtsInput,
|
||||
user: AuthenticatedUser
|
||||
) {
|
||||
if (!input.files) {
|
||||
return await rtsModel.create({
|
||||
...input,
|
||||
tenantId: user.tenantId,
|
||||
pid: generateId(),
|
||||
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: "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, rtsFields);
|
||||
|
||||
const rtsList = await rtsModel.aggregate([
|
||||
{
|
||||
$match: { $and: [{ tenantId: tenantId }, ...filterObj] },
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "organizations",
|
||||
localField: "county",
|
||||
foreignField: "_id",
|
||||
as: "countyRec",
|
||||
pipeline: [{ $project: { _id: 1, pid: 1, name: 1, type: 1 } }],
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "organizations",
|
||||
localField: "client",
|
||||
foreignField: "_id",
|
||||
as: "clientRec",
|
||||
pipeline: [{ $project: { _id: 1, pid: 1, name: 1, type: 1 } }],
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "users",
|
||||
localField: "createdBy",
|
||||
foreignField: "_id",
|
||||
as: "createdRec",
|
||||
pipeline: [{ $project: { _id: 1, pid: 1, name: 1, avatar: 1 } }],
|
||||
},
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
_id: 1,
|
||||
pid: 1,
|
||||
county: { $arrayElemAt: ["$countyRec", 0] },
|
||||
client: { $arrayElemAt: ["$clientRec", 0] },
|
||||
statusPipeline: 1,
|
||||
createdAt: 1,
|
||||
createdBy: { $arrayElemAt: ["$createdRec", 0] },
|
||||
},
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
metadata: [{ $count: "count" }],
|
||||
data: [
|
||||
{ $skip: (page - 1) * pageSize },
|
||||
{ $limit: pageSize },
|
||||
{ $sort: sortObj },
|
||||
],
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
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
|
||||
) {
|
||||
return await rtsModel.findOneAndUpdate(
|
||||
{ pid: id, tenantId: tenantId },
|
||||
input
|
||||
);
|
||||
}
|
||||
|
||||
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,
|
||||
},
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user