Skip to content

Commit 1ab28c6

Browse files
committed
Fix status model in database
1 parent db0900b commit 1ab28c6

File tree

12 files changed

+181
-116
lines changed

12 files changed

+181
-116
lines changed

package-lock.json

Lines changed: 24 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@ant-design/icons": "^5.0.1",
14-
"@netsu/js-utils": "^1.0.9",
14+
"@netsu/js-utils": "^1.2.2",
1515
"@supabase/auth-helpers-nextjs": "^0.6.1",
1616
"@supabase/auth-helpers-react": "^0.3.1",
1717
"@supabase/auth-ui-react": "^0.3.6",
@@ -22,7 +22,7 @@
2222
"eslint-config-next": "13.3.2",
2323
"lodash": "^4.17.21",
2424
"moment": "^2.29.4",
25-
"mongodb": "^5.3.0",
25+
"mongodb": "^5.6.0",
2626
"next": "13.3.2",
2727
"react": "18.2.0",
2828
"react-beautiful-dnd": "^13.1.1",
@@ -40,4 +40,4 @@
4040
"sass": "^1.62.1",
4141
"supabase": "^1.52.2"
4242
}
43-
}
43+
}

src/components/ui/projectStatus/ProjectStatusContainer.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import ProjectModel, { ProjectStatusModel } from "@/models/project";
1+
import ProjectModel from "@/models/project";
2+
import ProjectStatusModel from "@/models/projectStatus";
23
import { AvailableRequestMethods } from "@/models/requests";
34
import TaskModel from "@/models/task";
45
import { SingleProjectStatusPutRequestBodyModel } from "@/pages/api/projects/[projectId]/status/[statusId]/_models";
@@ -119,8 +120,6 @@ const StatusContainer: React.FC<StatusContainerProps> = ({
119120
setUserProject(updatedProject);
120121
};
121122

122-
// if (loading) return <Loader />;
123-
124123
return (
125124
<div className={styles.statusContainer} style={{ height: statusHeight }}>
126125
<Typography.Title

src/models/projectStatus.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ObjectId } from "mongodb";
55
* multiple tasks
66
*/
77
interface ProjectStatusModel {
8-
_id: string;
8+
_id: ObjectId;
99
/**
1010
* ID of the project this status is linked to
1111
*/
@@ -19,6 +19,7 @@ interface ProjectStatusModel {
1919
* The order in which the statuses should be, aka its index in the array
2020
*/
2121
orderIndex: number;
22+
createdAt: Date;
2223
}
2324

2425
export default ProjectStatusModel;

src/models/task.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface TaskModel {
1515
* as TODO/IN PROGRESS/COMPLETED - these statuses
1616
* can be found on the project model
1717
*/
18-
statusId?: string;
18+
statusId?: ObjectId;
1919
title: string;
2020
description?: string;
2121
createdAt: Date;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import ProjectModel from "@/models/project";
22

33
// requests
4-
export interface SingleProjectGetResponseModel {
5-
data: ProjectModel;
4+
export interface SingleProjectPutRequestBodyModel {
5+
data: Partial<ProjectModel>;
66
}
77

88
// responses
99
export interface SingleProjectPutResponseModel {
1010
data: ProjectModel;
1111
}
1212

13-
export interface SingleProjectPutRequestBodyModel {
14-
data: Partial<ProjectModel>;
13+
export interface SingleProjectGetResponseModel {
14+
data: ProjectModel;
1515
}

src/pages/api/projects/[projectId]/status/[statusId]/index.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
2-
import { ProjectStatusCollection } from "@/db/collections";
2+
import { ProjectStatusCollection, TasksCollection } from "@/db/collections";
33
import ProjectStatusModel from "@/models/projectStatus";
44
import { AvailableRequestMethods, ErrorResponseModel, SimpleResponseModel } from "@/models/requests";
55
import { checkApiSupabaseAuth, notAuthResponse, simpleResponse } from "@/utils/requests";
@@ -11,9 +11,6 @@ const putHandler = async (req: NextApiRequest, res: NextApiResponse<SimpleRespon
1111
const { statusId } = req.query;
1212
const { data } = req.body as SingleProjectStatusPutRequestBodyModel;
1313

14-
// if only intellisense worked with this
15-
// if(!checkPathParametersValid(projectId, statusId)){}
16-
1714
if (!statusId || typeof statusId !== "string") {
1815
const resp: SimpleResponseModel = {
1916
reason: "ID is invalid",
@@ -48,11 +45,14 @@ const putHandler = async (req: NextApiRequest, res: NextApiResponse<SimpleRespon
4845

4946
const updateData: ProjectStatusModel = { ...oldStatus, ...data };
5047

51-
const updateStatus = await ProjectStatusCollection.findOneAndUpdate(new ObjectId(statusId), {
52-
$set: {
53-
...updateData,
48+
const updateStatus = await ProjectStatusCollection.findOneAndUpdate(
49+
{ _id: status._id },
50+
{
51+
$set: {
52+
...updateData,
53+
},
5454
},
55-
});
55+
);
5656

5757
if (!updateStatus.ok || !updateStatus.value) {
5858
const resp: SimpleResponseModel = {
@@ -84,7 +84,9 @@ const deleteHandler = async (req: NextApiRequest, res: NextApiResponse<SimpleRes
8484

8585
if (!user) return notAuthResponse(res);
8686

87-
const deletedStatus = await ProjectStatusCollection.deleteOne(new ObjectId(statusId));
87+
const deletedStatus = await ProjectStatusCollection.deleteOne({
88+
_id: new ObjectId(statusId),
89+
});
8890

8991
if (!deletedStatus.acknowledged) {
9092
const resp: SimpleResponseModel = {
@@ -94,6 +96,18 @@ const deleteHandler = async (req: NextApiRequest, res: NextApiResponse<SimpleRes
9496
return simpleResponse(res, resp, 500);
9597
}
9698

99+
const deletedTasks = await TasksCollection.deleteMany({
100+
statusId: new ObjectId(statusId),
101+
});
102+
103+
if (!deletedTasks.acknowledged) {
104+
const resp: SimpleResponseModel = {
105+
reason: "Could not delete status tasks - please report to an admin",
106+
};
107+
108+
return simpleResponse(res, resp, 500);
109+
}
110+
97111
return simpleResponse(res, {}, 200);
98112
};
99113

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
1-
import ProjectModel from "@/models/project";
21
import ProjectStatusModel from "@/models/projectStatus";
32

43
// requests
5-
export interface SingleProjectStatusPostRequestBodyModel {
6-
data?: ProjectStatusModel;
4+
export interface ProjectStatusesPostRequestBodyModel {
5+
/**
6+
* Title of the status that will be shown to
7+
* the user
8+
*/
9+
title: string;
10+
/**
11+
* The order in which the statuses should be, aka its index in the array
12+
*/
13+
// orderIndex: number;
714
}
815

916
// responses
1017
export interface SingleProjectStatusPostResponseModel {
11-
data: ProjectModel;
18+
data: ProjectStatusModel;
19+
}
20+
21+
export interface ProjectsStatusesGetResponseModel {
22+
data: ProjectStatusModel[];
1223
}

0 commit comments

Comments
 (0)