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 1 commit
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;
25 changes: 25 additions & 0 deletions packages/api/src/services/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,31 @@ export const removeUserAsAdmin = async (userId: string): APIResponse<void> => {
return [200];
};

export const updateStageVisibility = async (
batteryId: string,
stageId: string,
visibility: string
): APIResponse<IBattery> => {
if (visibility !== "on" && visibility !== "off") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my only thought with this is that the visibility must be on or off, so perhaps if there is some more graceful error handling or we just default to off that might be better than throwing an error

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we simply remove the check? That way, our logic would dictate that visibilityBool is true if visibility is "on", and if it's anything else, it simply defaults to false. This would include the case where the caller of the API states the visibility to be "off".

throw new HttpError(400, "Invalid visibility must be 'on' or 'off'");
}
const visibilityBool = visibility === "on";
const updatedBattery = await Battery.findOneAndUpdate(
{ _id: batteryId, "stages._id": stageId },
{ $set: { "stages.$.isVisibleToNonAdmins": visibilityBool } },
{ 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
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