feat: allow users to have access to multiple oorgs
This commit is contained in:
@@ -58,6 +58,7 @@ const createTaskInput = z.object({
|
||||
})
|
||||
.optional(),
|
||||
note: z.string().optional(),
|
||||
orgId: z.string().optional(),
|
||||
});
|
||||
|
||||
const updateTaskInput = z.object({
|
||||
@@ -80,6 +81,7 @@ const updateTaskInput = z.object({
|
||||
currentStage: z.number(),
|
||||
})
|
||||
.optional(),
|
||||
orgId: z.string().optional(),
|
||||
});
|
||||
|
||||
const uploadTaskInput = z.object({
|
||||
|
||||
@@ -10,6 +10,8 @@ import {
|
||||
UpdateTaskInput,
|
||||
} from "./ctask.schema";
|
||||
|
||||
const InvalidOrg = new Error("invalid orgId");
|
||||
|
||||
export async function createTask(
|
||||
input: CreateTaskInput,
|
||||
user: AuthenticatedUser
|
||||
@@ -21,10 +23,14 @@ export async function createTask(
|
||||
};
|
||||
}
|
||||
|
||||
if (input.orgId && !user.orgId.includes(input.orgId)) {
|
||||
throw InvalidOrg;
|
||||
}
|
||||
|
||||
const task = await taskModel.create({
|
||||
...input,
|
||||
tenantId: user.tenantId,
|
||||
orgId: user.orgId,
|
||||
orgId: input.orgId ?? user.orgId[0],
|
||||
pid: generateId(),
|
||||
createdAt: new Date(),
|
||||
createdBy: user.userId ?? null,
|
||||
@@ -52,9 +58,13 @@ export async function updateTask(
|
||||
input: UpdateTaskInput,
|
||||
user: AuthenticatedUser
|
||||
) {
|
||||
if (input.orgId && !user.orgId.includes(input.orgId)) {
|
||||
throw InvalidOrg;
|
||||
}
|
||||
|
||||
const updatedTask = await taskModel
|
||||
.findOneAndUpdate(
|
||||
{ tenantId: user.tenantId, orgId: user.orgId, pid: taskId },
|
||||
{ tenantId: user.tenantId, orgId: { $in: user.orgId }, pid: taskId },
|
||||
input,
|
||||
{ new: true }
|
||||
)
|
||||
@@ -66,7 +76,11 @@ export async function updateTask(
|
||||
|
||||
export async function getTask(taskId: string, user: AuthenticatedUser) {
|
||||
return await taskModel
|
||||
.findOne({ tenantId: user.tenantId, orgId: user.orgId, pid: taskId })
|
||||
.findOne({
|
||||
tenantId: user.tenantId,
|
||||
orgId: { $in: user.orgId },
|
||||
pid: taskId,
|
||||
})
|
||||
.populate({ path: "createdBy", select: "pid name avatar" })
|
||||
.populate({ path: "assignedTo", select: "pid name avatar" });
|
||||
}
|
||||
@@ -118,7 +132,7 @@ export async function listTasks(
|
||||
$match: {
|
||||
$and: [
|
||||
{ tenantId: user.tenantId },
|
||||
{ orgId: user.orgId },
|
||||
{ orgId: { $in: user.orgId } },
|
||||
...filterObj,
|
||||
],
|
||||
},
|
||||
@@ -204,7 +218,7 @@ export async function listTasks(
|
||||
export async function deleteTask(taskId: string, user: AuthenticatedUser) {
|
||||
return await taskModel.deleteOne({
|
||||
pid: taskId,
|
||||
orgId: user.orgId,
|
||||
orgId: { $in: user.orgId },
|
||||
tenantId: user.tenantId,
|
||||
});
|
||||
}
|
||||
@@ -225,7 +239,7 @@ export async function searchTasks(
|
||||
$match: {
|
||||
$and: [
|
||||
{ tenantId: user.tenantId },
|
||||
{ orgId: user.orgId },
|
||||
{ orgId: { $in: user.orgId } },
|
||||
...filterObj,
|
||||
],
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user