Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#141 task template stages visibility successfully toggles. #144

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api/src/models/battery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const batteryStageSchema = new Schema<IBatteryStage>({
type: { type: String, required: true },
stageLabel: String,
options: optionGroupSchema,
isVisibleToNonAdmins: { type: Boolean, default: true },
});

const optArray = optionGroupSchema.path<Schema.Types.DocumentArray>("options");
Expand Down
12 changes: 12 additions & 0 deletions packages/api/src/routes/admin.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,16 @@ router.delete(
route((req) => adminService.removeUserAsAdmin(req.params.id))
);

router.put(
"/battery/:id/stage/:stageId/visibility/:status",
isAdmin,
route((req) =>
adminService.updateStageVisibility(
req.params.id,
req.params.stageId,
req.params.status
)
)
);

export default router;
24 changes: 24 additions & 0 deletions packages/api/src/services/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
IBattery,
IUser,
} from "@seitz/shared";
import { parseVisibility } from "@/util/validation.utils";

/* eslint-disable @typescript-eslint/no-explicit-any */

Expand Down Expand Up @@ -87,6 +88,29 @@ export const removeUserAsAdmin = async (userId: string): APIResponse<void> => {
return [200];
};

export const updateStageVisibility = async (
batteryId: string,
stageId: string,
visibility: string
): APIResponse<IBattery> => {
const isVisibleToNonAdmins = parseVisibility(visibility);

const updatedBattery = await Battery.findOneAndUpdate(
{ _id: batteryId, "stages._id": stageId },
{ $set: { "stages.$.isVisibleToNonAdmins": isVisibleToNonAdmins } },
{ new: true }
);

if (!updatedBattery) {
throw new HttpError(
404,
`Battery ${batteryId} or Stage ${stageId} not found.`
);
}

return [200, updatedBattery];
};

function parseOptions(s: any): CreateOption[] {
return Object.entries(s).reduce((acc: CreateOption[], item: any) => {
const optionName = item[0];
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ export default class HttpError extends Error {
this.status = status;
}
}

export class VisibilityError extends HttpError {
constructor(message: string) {
super(403, message);
}
}
23 changes: 23 additions & 0 deletions packages/api/src/util/validation.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { VisibilityError } from "@/types/errors";

export enum Visibility {
ON = "on",
OFF = "off",
}

export const parseVisibility = (visibility: string): boolean => {
try {
if (!Object.values(Visibility).includes(visibility as Visibility)) {
throw new VisibilityError(
`Invalid visibility value: '${visibility}'. Must be either '${Visibility.ON}' or '${Visibility.OFF}'`
);
}
return visibility === Visibility.ON;
} catch (e) {
// Log the error for monitoring but default to OFF for safety - debugging statement :/
console.warn(
`Invalid visibility value received: ${visibility}. Defaulting to OFF`
);
return false;
}
};
1 change: 1 addition & 0 deletions packages/shared/types/models/battery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface CreateBatteryStage {
type: string;
stageLabel: string;
options: CreateOptionGroup;
isVisibleToNonAdmins: boolean;
}

export interface IBatteryStage extends Required<CreateBatteryStage> {
Expand Down