populate fields on task creation
This commit is contained in:
@@ -11,6 +11,7 @@ export const configModel = mongoose.model(
|
||||
},
|
||||
emailIds: Array,
|
||||
statusMap: Object,
|
||||
folders: Object,
|
||||
updatedAt: Date,
|
||||
updatedBy: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
|
||||
@@ -8,16 +8,24 @@ import {
|
||||
import { AuthenticatedUser } from "../auth";
|
||||
import { generateId } from "../utils/id";
|
||||
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
|
||||
import { getUser } from "../user/user.service";
|
||||
|
||||
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,
|
||||
});
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { AuthenticatedUser } from "../auth";
|
||||
import { getFilterObject, getSortObject, PageQueryParams } from "../pagination";
|
||||
import { generateId } from "../utils/id";
|
||||
import { taskPipeline } from "../utils/pipeline";
|
||||
import { AuthenticatedUser } from '../auth';
|
||||
import { getFilterObject, getSortObject, PageQueryParams } from '../pagination';
|
||||
import { generateId } from '../utils/id';
|
||||
import { taskPipeline } from '../utils/pipeline';
|
||||
import {
|
||||
CreateTaskInput,
|
||||
taskFields,
|
||||
taskModel,
|
||||
UpdateTaskInput,
|
||||
UploadTaskInput,
|
||||
} from "./task.schema";
|
||||
} from './task.schema';
|
||||
|
||||
export async function createTask(
|
||||
input: CreateTaskInput,
|
||||
@@ -21,29 +21,18 @@ export async function createTask(
|
||||
};
|
||||
}
|
||||
|
||||
if (!input.files) {
|
||||
return await taskModel.create({
|
||||
...input,
|
||||
tenantId: user.tenantId,
|
||||
pid: generateId(),
|
||||
createdAt: new Date(),
|
||||
createdBy: user.userId ?? null,
|
||||
});
|
||||
} else {
|
||||
return await taskModel.create({
|
||||
tenantId: user.tenantId,
|
||||
pid: generateId(),
|
||||
documents: [
|
||||
{
|
||||
files: input.files,
|
||||
createdAt: new Date(),
|
||||
createdBy: user.userId ?? null,
|
||||
},
|
||||
],
|
||||
createdAt: new Date(),
|
||||
createdBy: user.userId ?? null,
|
||||
});
|
||||
}
|
||||
const task = await taskModel.create({
|
||||
...input,
|
||||
tenantId: user.tenantId,
|
||||
pid: generateId(),
|
||||
createdAt: new Date(),
|
||||
createdBy: user.userId ?? null,
|
||||
});
|
||||
|
||||
return await taskModel
|
||||
.find({ pid: task.pid })
|
||||
.populate({ path: 'createdBy', select: 'pid name avatar' })
|
||||
.populate({ path: 'assignedTo', select: 'pid name avatar' });
|
||||
}
|
||||
|
||||
export async function updateTask(
|
||||
@@ -53,8 +42,8 @@ export async function updateTask(
|
||||
) {
|
||||
const updatedTask = await taskModel
|
||||
.findOneAndUpdate({ tenantId: tenantId, pid: taskId }, input, { new: true })
|
||||
.populate({ path: "createdBy", select: "pid name avatar" })
|
||||
.populate({ path: "assignedTo", select: "pid name avatar" });
|
||||
.populate({ path: 'createdBy', select: 'pid name avatar' })
|
||||
.populate({ path: 'assignedTo', select: 'pid name avatar' });
|
||||
|
||||
return updatedTask;
|
||||
}
|
||||
@@ -62,8 +51,8 @@ export async function updateTask(
|
||||
export async function getTask(taskId: string, tenantId: string) {
|
||||
return await taskModel
|
||||
.findOne({ tenantId: tenantId, pid: taskId })
|
||||
.populate({ path: "createdBy", select: "pid name avatar" })
|
||||
.populate({ path: "assignedTo", select: "pid name avatar" });
|
||||
.populate({ path: 'createdBy', select: 'pid name avatar' })
|
||||
.populate({ path: 'assignedTo', select: 'pid name avatar' });
|
||||
}
|
||||
|
||||
export async function listTasks(params: PageQueryParams, tenantId: string) {
|
||||
@@ -78,18 +67,18 @@ export async function listTasks(params: PageQueryParams, tenantId: string) {
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "users",
|
||||
localField: "createdBy",
|
||||
foreignField: "_id",
|
||||
as: "createdBy",
|
||||
from: 'users',
|
||||
localField: 'createdBy',
|
||||
foreignField: '_id',
|
||||
as: 'createdBy',
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "users",
|
||||
localField: "assignedTo",
|
||||
foreignField: "_id",
|
||||
as: "assignedTo",
|
||||
from: 'users',
|
||||
localField: 'assignedTo',
|
||||
foreignField: '_id',
|
||||
as: 'assignedTo',
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -103,21 +92,21 @@ export async function listTasks(params: PageQueryParams, tenantId: string) {
|
||||
createdAt: 1,
|
||||
createdBy: {
|
||||
$let: {
|
||||
vars: { createdBy: { $arrayElemAt: ["$createdBy", 0] } },
|
||||
vars: { createdBy: { $arrayElemAt: ['$createdBy', 0] } },
|
||||
in: {
|
||||
_id: "$$createdBy._id",
|
||||
pid: "$$createdBy.pid",
|
||||
name: "$$createdBy.name",
|
||||
_id: '$$createdBy._id',
|
||||
pid: '$$createdBy.pid',
|
||||
name: '$$createdBy.name',
|
||||
},
|
||||
},
|
||||
},
|
||||
assignedTo: {
|
||||
$let: {
|
||||
vars: { assignedTo: { $arrayElemAt: ["$assignedTo", 0] } },
|
||||
vars: { assignedTo: { $arrayElemAt: ['$assignedTo', 0] } },
|
||||
in: {
|
||||
_id: "$$assignedTo._id",
|
||||
pid: "$$assignedTo.pid",
|
||||
name: "$$assignedTo.name",
|
||||
_id: '$$assignedTo._id',
|
||||
pid: '$$assignedTo.pid',
|
||||
name: '$$assignedTo.name',
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -125,7 +114,7 @@ export async function listTasks(params: PageQueryParams, tenantId: string) {
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
metadata: [{ $count: "count" }],
|
||||
metadata: [{ $count: 'count' }],
|
||||
data: [
|
||||
{ $sort: sortObj },
|
||||
{ $skip: (page - 1) * pageSize },
|
||||
@@ -158,7 +147,7 @@ export async function searchTasks(params: PageQueryParams, tenantId: string) {
|
||||
const sortObj = getSortObject(params, taskFields);
|
||||
const filterObj = getFilterObject(params, taskFields);
|
||||
|
||||
const regex = new RegExp(params.searchToken, "i");
|
||||
const regex = new RegExp(params.searchToken, 'i');
|
||||
|
||||
const taskList = await taskModel.aggregate([
|
||||
{
|
||||
@@ -171,18 +160,18 @@ export async function searchTasks(params: PageQueryParams, tenantId: string) {
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "users",
|
||||
localField: "createdBy",
|
||||
foreignField: "_id",
|
||||
as: "createdBy",
|
||||
from: 'users',
|
||||
localField: 'createdBy',
|
||||
foreignField: '_id',
|
||||
as: 'createdBy',
|
||||
},
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "users",
|
||||
localField: "assignedTo",
|
||||
foreignField: "_id",
|
||||
as: "assignedTo",
|
||||
from: 'users',
|
||||
localField: 'assignedTo',
|
||||
foreignField: '_id',
|
||||
as: 'assignedTo',
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -196,21 +185,21 @@ export async function searchTasks(params: PageQueryParams, tenantId: string) {
|
||||
createdAt: 1,
|
||||
createdBy: {
|
||||
$let: {
|
||||
vars: { createdBy: { $arrayElemAt: ["$createdBy", 0] } },
|
||||
vars: { createdBy: { $arrayElemAt: ['$createdBy', 0] } },
|
||||
in: {
|
||||
_id: "$$createdBy._id",
|
||||
pid: "$$createdBy.pid",
|
||||
name: "$$createdBy.name",
|
||||
_id: '$$createdBy._id',
|
||||
pid: '$$createdBy.pid',
|
||||
name: '$$createdBy.name',
|
||||
},
|
||||
},
|
||||
},
|
||||
assignedTo: {
|
||||
$let: {
|
||||
vars: { assignedTo: { $arrayElemAt: ["$assignedTo", 0] } },
|
||||
vars: { assignedTo: { $arrayElemAt: ['$assignedTo', 0] } },
|
||||
in: {
|
||||
_id: "$$assignedTo._id",
|
||||
pid: "$$assignedTo.pid",
|
||||
name: "$$assignedTo.name",
|
||||
_id: '$$assignedTo._id',
|
||||
pid: '$$assignedTo.pid',
|
||||
name: '$$assignedTo.name',
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -218,7 +207,7 @@ export async function searchTasks(params: PageQueryParams, tenantId: string) {
|
||||
},
|
||||
{
|
||||
$facet: {
|
||||
metadata: [{ $count: "count" }],
|
||||
metadata: [{ $count: 'count' }],
|
||||
data: [
|
||||
{ $sort: sortObj },
|
||||
{ $skip: (page - 1) * pageSize },
|
||||
|
||||
@@ -28,6 +28,10 @@ const userSchema = new mongoose.Schema({
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
defaultClient: {
|
||||
type: mongoose.Types.ObjectId,
|
||||
ref: "organization",
|
||||
},
|
||||
passKeys: [new mongoose.Schema({}, { _id: false, strict: false })],
|
||||
challenge: new mongoose.Schema(
|
||||
{
|
||||
@@ -92,6 +96,7 @@ const updateUserInput = z.object({
|
||||
.optional(),
|
||||
avatar: z.string().url().optional(),
|
||||
role: z.enum(roles).optional(),
|
||||
defaultClient: z.string().optional(),
|
||||
});
|
||||
|
||||
const userResponse = z.object({
|
||||
|
||||
Reference in New Issue
Block a user