switch to mongo's id handling
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
|
import { ObjectId } from "mongodb";
|
||||||
import { Module } from ".";
|
import { Module } from ".";
|
||||||
import { Type, User, userTypeLabels, userTypeLabelsShort } from "./user";
|
import { Type, User, userTypeLabels, userTypeLabelsShort } from "./user";
|
||||||
|
|
||||||
export interface ApprovalWorkflow {
|
export interface ApprovalWorkflow {
|
||||||
id: string,
|
_id?: ObjectId,
|
||||||
name: string,
|
name: string,
|
||||||
entityId: string,
|
entityId: string,
|
||||||
requester: User["id"],
|
requester: User["id"],
|
||||||
@@ -12,14 +13,8 @@ export interface ApprovalWorkflow {
|
|||||||
steps: WorkflowStep[],
|
steps: WorkflowStep[],
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface EditableApprovalWorkflow {
|
export interface EditableApprovalWorkflow extends Omit<ApprovalWorkflow, "_id" | "steps"> {
|
||||||
id: string,
|
id: string,
|
||||||
name: string,
|
|
||||||
entityId: string,
|
|
||||||
requester: User["id"],
|
|
||||||
startDate: number,
|
|
||||||
modules: Module[],
|
|
||||||
status: ApprovalWorkflowStatus,
|
|
||||||
steps: EditableWorkflowStep[],
|
steps: EditableWorkflowStep[],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ export default function Home({ user, workflow, userEntitiesWithLabel, userEntiti
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
const editableWorkflow: EditableApprovalWorkflow = {
|
const editableWorkflow: EditableApprovalWorkflow = {
|
||||||
id: uuidv4(),
|
id: uuidv4(), // this id is only used in UI. it is ommited on submission to DB and lets mongo handle unique id.
|
||||||
name: workflow.name,
|
name: workflow.name,
|
||||||
entityId: workflow.entityId,
|
entityId: workflow.entityId,
|
||||||
requester: user.id,
|
requester: user.id,
|
||||||
@@ -141,7 +141,7 @@ export default function Home({ user, workflow, userEntitiesWithLabel, userEntiti
|
|||||||
};
|
};
|
||||||
|
|
||||||
axios
|
axios
|
||||||
.post(`/api/approval-workflows/${workflow.id}/clone`, filteredWorkflow)
|
.post(`/api/approval-workflows/${workflow._id}/clone`, filteredWorkflow)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
toast.success("Approval Workflow cloned successfully.");
|
toast.success("Approval Workflow cloned successfully.");
|
||||||
setIsRedirecting(true);
|
setIsRedirecting(true);
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ export default function Home({ user, workflow, workflowEntityTeachers, workflowE
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
const editableWorkflow: EditableApprovalWorkflow = {
|
const editableWorkflow: EditableApprovalWorkflow = {
|
||||||
id: workflow.id,
|
id: workflow._id?.toString() ?? "",
|
||||||
name: workflow.name,
|
name: workflow.name,
|
||||||
entityId: workflow.entityId,
|
entityId: workflow.entityId,
|
||||||
requester: user.id, // should it change to the editor?
|
requester: user.id, // should it change to the editor?
|
||||||
@@ -104,7 +104,7 @@ export default function Home({ user, workflow, workflowEntityTeachers, workflowE
|
|||||||
};
|
};
|
||||||
|
|
||||||
axios
|
axios
|
||||||
.put(`/api/approval-workflows/${workflow.id}/edit`, filteredWorkflow)
|
.put(`/api/approval-workflows/${updatedWorkflow.id}/edit`, filteredWorkflow)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
toast.success("Approval Workflow edited successfully.");
|
toast.success("Approval Workflow edited successfully.");
|
||||||
setIsRedirecting(true);
|
setIsRedirecting(true);
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ export default function Home({ user, userEntitiesWithLabel, userEntitiesTeachers
|
|||||||
if (isAdding) return;
|
if (isAdding) return;
|
||||||
setIsAdding(true);
|
setIsAdding(true);
|
||||||
|
|
||||||
const newId = uuidv4();
|
const newId = uuidv4(); // this id is only used in UI. it is ommited on submission to DB and lets mongo handle unique id.
|
||||||
const newWorkflow: EditableApprovalWorkflow = {
|
const newWorkflow: EditableApprovalWorkflow = {
|
||||||
id: newId,
|
id: newId,
|
||||||
name: "",
|
name: "",
|
||||||
|
|||||||
@@ -145,7 +145,8 @@ export default function ApprovalWorkflows({ user, workflows, workflowsAssignees,
|
|||||||
setNameFilter(name);
|
setNameFilter(name);
|
||||||
};
|
};
|
||||||
|
|
||||||
const deleteApprovalWorkflow = (id: string, name: string) => {
|
const deleteApprovalWorkflow = (id: string | undefined, name: string) => {
|
||||||
|
if (id === undefined) return;
|
||||||
if (!confirm(`Are you sure you want to delete this Approval Workflow?`)) return;
|
if (!confirm(`Are you sure you want to delete this Approval Workflow?`)) return;
|
||||||
|
|
||||||
axios
|
axios
|
||||||
@@ -267,7 +268,7 @@ export default function ApprovalWorkflows({ user, workflows, workflowsAssignees,
|
|||||||
className="cursor-pointer tooltip"
|
className="cursor-pointer tooltip"
|
||||||
onClick={(e) => {
|
onClick={(e) => {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
deleteApprovalWorkflow(row.original.id, row.original.name);
|
deleteApprovalWorkflow(row.original._id?.toString(), row.original.name);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<BsTrash className="hover:text-mti-purple-light transition ease-in-out duration-300" />
|
<BsTrash className="hover:text-mti-purple-light transition ease-in-out duration-300" />
|
||||||
@@ -276,7 +277,7 @@ export default function ApprovalWorkflows({ user, workflows, workflowsAssignees,
|
|||||||
<Link
|
<Link
|
||||||
onClick={(e) => e.stopPropagation()}
|
onClick={(e) => e.stopPropagation()}
|
||||||
data-tip="Clone"
|
data-tip="Clone"
|
||||||
href={`/approval-workflows/${row.original.id}/clone`}
|
href={`/approval-workflows/${row.original._id?.toString()}/clone`}
|
||||||
className="cursor-pointer tooltip"
|
className="cursor-pointer tooltip"
|
||||||
>
|
>
|
||||||
<FaRegClone className="hover:text-mti-purple-light transition ease-in-out duration-300" />
|
<FaRegClone className="hover:text-mti-purple-light transition ease-in-out duration-300" />
|
||||||
@@ -286,7 +287,7 @@ export default function ApprovalWorkflows({ user, workflows, workflowsAssignees,
|
|||||||
<Link
|
<Link
|
||||||
onClick={(e) => e.stopPropagation()}
|
onClick={(e) => e.stopPropagation()}
|
||||||
data-tip="Edit"
|
data-tip="Edit"
|
||||||
href={`/approval-workflows/${row.original.id}/edit`}
|
href={`/approval-workflows/${row.original._id?.toString()}/edit`}
|
||||||
className="cursor-pointer tooltip"
|
className="cursor-pointer tooltip"
|
||||||
>
|
>
|
||||||
<FaRegEdit className="hover:text-mti-purple-light transition ease-in-out duration-300" />
|
<FaRegEdit className="hover:text-mti-purple-light transition ease-in-out duration-300" />
|
||||||
@@ -391,7 +392,7 @@ export default function ApprovalWorkflows({ user, workflows, workflowsAssignees,
|
|||||||
{table.getRowModel().rows.map((row) => (
|
{table.getRowModel().rows.map((row) => (
|
||||||
<tr
|
<tr
|
||||||
key={row.id}
|
key={row.id}
|
||||||
onClick={() => window.location.href = `/approval-workflows/${row.original.id}`}
|
onClick={() => window.location.href = `/approval-workflows/${row.original._id?.toString()}`}
|
||||||
style={{ cursor: "pointer" }}
|
style={{ cursor: "pointer" }}
|
||||||
className="bg-purple-50"
|
className="bg-purple-50"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -1,32 +1,33 @@
|
|||||||
|
import { ObjectId } from "mongodb";
|
||||||
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
import { ApprovalWorkflow } from "@/interfaces/approval.workflow";
|
||||||
import client from "@/lib/mongodb";
|
import client from "@/lib/mongodb";
|
||||||
|
|
||||||
const db = client.db(process.env.MONGODB_DB);
|
const db = client.db(process.env.MONGODB_DB);
|
||||||
|
|
||||||
export const getApprovalWorkflows = async (ids?: string[]) => {
|
export const getApprovalWorkflows = async (ids?: string[]) => {
|
||||||
return await db
|
return await db
|
||||||
.collection("approval-workflows")
|
.collection<ApprovalWorkflow>("approval-workflows")
|
||||||
.find<ApprovalWorkflow>(ids ? { id: { $in: ids } } : {})
|
.find(ids ? { _id: { $in: ids.map((id) => new ObjectId(id)) } } : {})
|
||||||
.toArray();
|
.toArray();
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getApprovalWorkflow = async (id: string) => {
|
export const getApprovalWorkflow = async (id: string) => {
|
||||||
return await db.collection("approval-workflows").findOne<ApprovalWorkflow>({ id });
|
return await db.collection<ApprovalWorkflow>("approval-workflows").findOne({ _id: new ObjectId(id) });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createApprovalWorkflow = async (workflow: ApprovalWorkflow) => {
|
export const createApprovalWorkflow = async (workflow: Omit<ApprovalWorkflow, "_id">) => {
|
||||||
return await db.collection("approval-workflows").insertOne(workflow);
|
return await db.collection("approval-workflows").insertOne(workflow);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createApprovalWorkflows = async (workflows: ApprovalWorkflow[]) => {
|
export const createApprovalWorkflows = async (workflows: Omit<ApprovalWorkflow, "_id">[]) => {
|
||||||
if (workflows.length === 0) return;
|
if (workflows.length === 0) return;
|
||||||
return await db.collection<ApprovalWorkflow>("approval-workflows").insertMany(workflows);
|
return await db.collection("approval-workflows").insertMany(workflows);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const updateApprovalWorkflow = async (id: string, workflow: ApprovalWorkflow) => {
|
export const updateApprovalWorkflow = async (id: string, workflow: Omit<ApprovalWorkflow, "_id">) => {
|
||||||
return await db.collection("approval-workflows").replaceOne({ id }, workflow);
|
return await db.collection("approval-workflows").replaceOne({ _id: new ObjectId(id) }, workflow);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const deleteApprovalWorkflow = async (id: string) => {
|
export const deleteApprovalWorkflow = async (id: string) => {
|
||||||
return await db.collection("approval-workflows").deleteOne({ id });
|
return await db.collection("approval-workflows").deleteOne({ _id: new ObjectId(id) });
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user