-
Notifications
You must be signed in to change notification settings - Fork 139
fix(experiences): persist interview submissions with review status #1375
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
Closed
nyxsky404
wants to merge
4
commits into
Canopus-Labs:main
from
nyxsky404:fix/935-interview-experience-persist
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
56ef153
fix(experiences): persist interview submissions with review status
nyxsky404 5579c8d
fix(experiences): address CodeRabbit review on interview submissions
nyxsky404 f8aca4d
docs(env): document MODERATOR_EMAILS for experience moderation
nyxsky404 7252b61
fix(experiences): require idempotencyKey on create
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,55 @@ | ||
| 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(), | ||
| idempotencyKey: z.string().trim().min(8).max(64).optional().nullable(), | ||
| }); | ||
|
|
||
| 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,192 @@ | ||
| const InterviewExperience = require("../models/InterviewExperience"); | ||
|
|
||
| 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" }; | ||
| 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%)`; | ||
| } | ||
|
|
||
| if (payload.idempotencyKey) { | ||
| const existing = await InterviewExperience.findOne({ | ||
| idempotencyKey: payload.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, | ||
| }); | ||
|
KaranUnique marked this conversation as resolved.
Dismissed
|
||
| 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 experience = await InterviewExperience.findByIdAndUpdate( | ||
| req.params.id, | ||
| { status: req.body.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.
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.