Skip to content

Commit 8f64377

Browse files
author
Rajat
committed
Codex drip code review fix
1 parent 381fd92 commit 8f64377

2 files changed

Lines changed: 59 additions & 2 deletions

File tree

apps/web/graphql/courses/__tests__/update-group-drip.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import DomainModel from "@models/Domain";
22
import UserModel from "@models/User";
33
import CourseModel from "@models/Course";
44
import constants from "@/config/constants";
5+
import { responses } from "@/config/strings";
56
import { Constants } from "@courselit/common-models";
67
import { updateGroup } from "../logic";
78

@@ -94,4 +95,51 @@ describe("updateGroup drip status updates", () => {
9495
2 * constants.relativeDripUnitInMillis,
9596
);
9697
});
98+
99+
it("rejects status-only drip updates when the group has no drip type yet", async () => {
100+
const groupId = id("group-without-drip");
101+
const course = await CourseModel.create({
102+
domain: testDomain._id,
103+
courseId: id("course-without-drip"),
104+
title: id("course-title-without-drip"),
105+
creatorId: adminUser.userId,
106+
groups: [
107+
{
108+
_id: groupId,
109+
name: "Group 1",
110+
rank: 1000,
111+
collapsed: true,
112+
lessonsOrder: [],
113+
},
114+
],
115+
lessons: [],
116+
type: "course",
117+
privacy: "unlisted",
118+
costType: "free",
119+
cost: 0,
120+
slug: id("course-slug-without-drip"),
121+
});
122+
123+
await expect(
124+
updateGroup({
125+
id: groupId,
126+
courseId: course.courseId,
127+
drip: {
128+
status: false,
129+
},
130+
ctx: {
131+
subdomain: testDomain,
132+
user: adminUser,
133+
address: "",
134+
},
135+
}),
136+
).rejects.toThrow(responses.invalid_input);
137+
138+
const updatedCourse = await CourseModel.findOne({
139+
domain: testDomain._id,
140+
courseId: course.courseId,
141+
}).lean();
142+
143+
expect(updatedCourse?.groups?.[0]?.drip).toBeUndefined();
144+
});
97145
});

apps/web/graphql/courses/logic.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,9 @@ export const updateGroup = async ({
815815
ctx: GQLContext;
816816
}) => {
817817
const course = await getCourseOrThrow(undefined, ctx, courseId);
818+
const currentGroup = course.groups?.find((group) => group.id === id);
819+
const existingDripType = currentGroup?.drip?.type;
820+
const effectiveDripType = drip?.type || existingDripType;
818821

819822
const $set = {};
820823
if (name) {
@@ -849,20 +852,26 @@ export const updateGroup = async ({
849852
}
850853

851854
if (drip) {
855+
const hasDripUpdates = Object.keys(drip).some((key) => key !== "type");
856+
857+
if (!effectiveDripType && hasDripUpdates) {
858+
throw new Error(responses.invalid_input);
859+
}
860+
852861
if (typeof drip.status === "boolean") {
853862
$set["groups.$.drip.status"] = drip.status;
854863
}
855864
if (drip.type) {
856865
$set["groups.$.drip.type"] = drip.type;
857866
}
858-
if (drip.type === Constants.dripType[0]) {
867+
if (effectiveDripType === Constants.dripType[0]) {
859868
if (typeof drip.delayInMillis === "number") {
860869
$set["groups.$.drip.delayInMillis"] =
861870
drip.delayInMillis * constants.relativeDripUnitInMillis;
862871
}
863872
$set["groups.$.drip.dateInUTC"] = drip.dateInUTC;
864873
}
865-
if (drip.type === Constants.dripType[1]) {
874+
if (effectiveDripType === Constants.dripType[1]) {
866875
$set["groups.$.drip.delayInMillis"] = null;
867876
if (drip.dateInUTC) {
868877
$set["groups.$.drip.dateInUTC"] = drip.dateInUTC;

0 commit comments

Comments
 (0)