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