-
Notifications
You must be signed in to change notification settings - Fork 140
fix(experiences): persist interview submissions with review status #1641
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
Merged
KaranUnique
merged 7 commits into
Canopus-Labs:main
from
nyxsky404:fix/935-interview-experience-persist
Aug 9, 2026
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b5731d2
fix(experiences): persist interview submissions with review status
nyxsky404 9ce2d7a
fix(experiences): address CodeRabbit review on interview submissions
nyxsky404 b515d65
docs(env): document MODERATOR_EMAILS for experience moderation
nyxsky404 fe75716
fix(experiences): require idempotencyKey on create
nyxsky404 5f31229
fix(experiences): satisfy CodeQL nosql and CSRF checks
nyxsky404 41d9784
fix(experiences): harden anonymous client identity
nyxsky404 c223e71
fix(experiences): reject legacy weak client keys
nyxsky404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| const { z } = require("zod"); | ||
| const { handleValidationError } = require("./ValidateQuestions"); | ||
|
|
||
| const createInterviewExperienceSchema = z.object({ | ||
| company: z.string().trim().min(1, "Company is required").max(120), | ||
| role: z.string().trim().min(1, "Role is required").max(120), | ||
| experience: z.string().trim().max(60).optional().default("N/A"), | ||
| difficulty: z.enum(["Easy", "Medium", "Hard"]).optional().default("Medium"), | ||
| offerReceived: z.boolean().optional().default(false), | ||
| date: z.string().trim().max(40).optional().default(""), | ||
| rounds: z | ||
| .array( | ||
| z.object({ | ||
| name: z.string().trim().max(120).optional().default("Round"), | ||
| type: z.string().trim().max(60).optional().default("Coding"), | ||
| description: z.string().trim().max(2000).optional().default(""), | ||
| }), | ||
| ) | ||
| .max(20) | ||
| .optional() | ||
| .default([]), | ||
| summary: z.string().trim().min(1, "Summary is required").max(5000), | ||
| tips: z.array(z.string().trim().max(500)).max(20).optional().default([]), | ||
| tags: z.array(z.string().trim().max(40)).max(10).optional().default([]), | ||
| color: z.string().trim().max(40).optional(), | ||
| clientKey: z.string().trim().min(8).max(64).optional().nullable(), | ||
| // Required so keyless retries cannot bypass the unique partial index. | ||
| idempotencyKey: z | ||
| .string() | ||
| .trim() | ||
| .min(8, "idempotencyKey is required") | ||
| .max(64), | ||
| }); | ||
|
|
||
| const updateStatusSchema = z.object({ | ||
| status: z.enum(["pending", "approved", "rejected"]), | ||
| }); | ||
|
|
||
| const validateCreateInterviewExperience = (req, res, next) => { | ||
| try { | ||
| req.body = createInterviewExperienceSchema.parse(req.body); | ||
| next(); | ||
| } catch (error) { | ||
| return handleValidationError(res, error); | ||
| } | ||
| }; | ||
|
|
||
| const validateUpdateInterviewExperienceStatus = (req, res, next) => { | ||
| try { | ||
| req.body = updateStatusSchema.parse(req.body); | ||
| next(); | ||
| } catch (error) { | ||
| return handleValidationError(res, error); | ||
| } | ||
| }; | ||
|
|
||
| module.exports = { | ||
| validateCreateInterviewExperience, | ||
| validateUpdateInterviewExperienceStatus, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| const mongoose = require("mongoose"); | ||
| const InterviewExperience = require("../models/InterviewExperience"); | ||
|
|
||
| const ALLOWED_STATUSES = ["pending", "approved", "rejected"]; | ||
|
|
||
| const toClientShape = (doc) => { | ||
| const obj = typeof doc.toObject === "function" ? doc.toObject() : doc; | ||
| return { | ||
| id: String(obj._id), | ||
| company: obj.company, | ||
| role: obj.role, | ||
| experience: obj.experience, | ||
| difficulty: obj.difficulty, | ||
| offerReceived: obj.offerReceived, | ||
| date: obj.date, | ||
| rounds: obj.rounds || [], | ||
| summary: obj.summary, | ||
| tips: obj.tips || [], | ||
| tags: obj.tags || [], | ||
| color: obj.color, | ||
| status: obj.status, | ||
| createdAt: obj.createdAt, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * @desc Submit an interview experience for moderation | ||
| * @route POST /api/interview-experiences | ||
| * @access Public (optional auth) | ||
| */ | ||
| const createInterviewExperience = async (req, res) => { | ||
| try { | ||
| const payload = { ...req.body, status: "pending" }; | ||
| const idempotencyKey = | ||
| typeof payload.idempotencyKey === "string" | ||
| ? payload.idempotencyKey.trim() | ||
| : ""; | ||
|
|
||
| if (!idempotencyKey) { | ||
| return res.status(400).json({ | ||
| success: false, | ||
| message: "idempotencyKey is required", | ||
| }); | ||
| } | ||
|
|
||
| payload.idempotencyKey = idempotencyKey; | ||
|
|
||
| if (req.user?._id) { | ||
| payload.userId = req.user._id; | ||
| } | ||
|
|
||
| if (!payload.color && payload.company) { | ||
| payload.color = `hsl(${(payload.company.charCodeAt(0) * 37) % 360}, 55%, 50%)`; | ||
| } | ||
|
|
||
| const existing = await InterviewExperience.findOne({ idempotencyKey }); | ||
| if (existing) { | ||
| return res.status(200).json({ | ||
| success: true, | ||
| message: "Interview experience already submitted", | ||
| experience: toClientShape(existing), | ||
| }); | ||
| } | ||
|
|
||
| const experience = await InterviewExperience.create(payload); | ||
|
|
||
| return res.status(201).json({ | ||
| success: true, | ||
| message: "Interview experience submitted for review", | ||
| experience: toClientShape(experience), | ||
| }); | ||
| } catch (error) { | ||
| // Concurrent retry won the unique index race — return the first write. | ||
| if (error?.code === 11000 && req.body?.idempotencyKey) { | ||
| try { | ||
| const existing = await InterviewExperience.findOne({ | ||
| idempotencyKey: req.body.idempotencyKey, | ||
| }); | ||
| if (existing) { | ||
| return res.status(200).json({ | ||
| success: true, | ||
| message: "Interview experience already submitted", | ||
| experience: toClientShape(existing), | ||
| }); | ||
| } | ||
| } catch { | ||
| // fall through to generic 500 | ||
| } | ||
| } | ||
|
|
||
| return res.status(500).json({ | ||
| success: false, | ||
| message: "Failed to submit interview experience", | ||
| error: "A server error occurred", | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @desc List approved community experiences | ||
| * @route GET /api/interview-experiences/approved | ||
| * @access Public | ||
| */ | ||
| const getApprovedInterviewExperiences = async (req, res) => { | ||
| try { | ||
| const experiences = await InterviewExperience.find({ status: "approved" }) | ||
| .sort({ createdAt: -1 }) | ||
| .lean(); | ||
|
|
||
| return res.status(200).json({ | ||
| success: true, | ||
| experiences: experiences.map(toClientShape), | ||
| }); | ||
| } catch (error) { | ||
| return res.status(500).json({ | ||
| success: false, | ||
| message: "Failed to load approved experiences", | ||
| error: "A server error occurred", | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @desc Load a visitor's own submissions by client key and/or auth | ||
| * @route GET /api/interview-experiences/mine | ||
| * @access Public (clientKey query) / Private | ||
| */ | ||
| const getMyInterviewExperiences = async (req, res) => { | ||
| try { | ||
| const clientKey = | ||
| typeof req.query.clientKey === "string" ? req.query.clientKey.trim() : ""; | ||
| const filters = []; | ||
|
|
||
| if (req.user?._id) { | ||
| filters.push({ userId: req.user._id }); | ||
| } | ||
| if (clientKey) { | ||
| filters.push({ clientKey }); | ||
| } | ||
|
|
||
| if (filters.length === 0) { | ||
| return res.status(400).json({ | ||
| success: false, | ||
| message: "Provide clientKey or authenticate to load your submissions", | ||
| }); | ||
| } | ||
|
|
||
| const experiences = await InterviewExperience.find({ $or: filters }) | ||
| .sort({ createdAt: -1 }) | ||
| .lean(); | ||
|
|
||
| return res.status(200).json({ | ||
| success: true, | ||
| experiences: experiences.map(toClientShape), | ||
| }); | ||
| } catch (error) { | ||
| return res.status(500).json({ | ||
| success: false, | ||
| message: "Failed to load your submissions", | ||
| error: "A server error occurred", | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @desc Update moderation status | ||
| * @route PATCH /api/interview-experiences/:id/status | ||
| * @access Private | ||
| */ | ||
| const updateInterviewExperienceStatus = async (req, res) => { | ||
| try { | ||
| const { id } = req.params; | ||
| if (!mongoose.isValidObjectId(id)) { | ||
| return res.status(400).json({ | ||
| success: false, | ||
| message: "Invalid experience id", | ||
| }); | ||
| } | ||
|
|
||
| // Resolve against a constant allowlist so the update document is never | ||
| // built directly from the request body (CodeQL js/nosql-injection). | ||
| const status = ALLOWED_STATUSES.find((value) => value === req.body?.status); | ||
| if (!status) { | ||
| return res.status(400).json({ | ||
| success: false, | ||
| message: "status must be pending, approved, or rejected", | ||
| }); | ||
| } | ||
|
|
||
| const experience = await InterviewExperience.findByIdAndUpdate( | ||
| id, | ||
| { status }, | ||
| { new: true }, | ||
| ); | ||
|
|
||
| if (!experience) { | ||
| return res.status(404).json({ | ||
| success: false, | ||
| message: "Interview experience not found", | ||
| }); | ||
| } | ||
|
|
||
| return res.status(200).json({ | ||
| success: true, | ||
| message: `Experience marked as ${experience.status}`, | ||
| experience: toClientShape(experience), | ||
| }); | ||
| } catch (error) { | ||
| return res.status(500).json({ | ||
| success: false, | ||
| message: "Failed to update experience status", | ||
| error: "A server error occurred", | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| module.exports = { | ||
| createInterviewExperience, | ||
| getApprovedInterviewExperiences, | ||
| getMyInterviewExperiences, | ||
| updateInterviewExperienceStatus, | ||
| toClientShape, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.