Skip to content

Commit

Permalink
fix req body error
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromehardaway committed Feb 9, 2024
1 parent 17a24d8 commit 61c01d1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
23 changes: 10 additions & 13 deletions src/pages/api/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from "axios";
import { Request, Response } from "express";
import { checkParams } from "./api-helpers";

// Define the ParsedBody interface to type-check the request body
interface ParsedBody {
firstName?: string;
lastName?: string;
Expand All @@ -21,7 +22,7 @@ interface ParsedBody {

export default async function handler(req: Request, res: Response) {
try {
const parsedBody: ParsedBody = req.body;
const parsedBody = req.body as ParsedBody; // Ensure body matches ParsedBody interface
const requiredParams: (keyof ParsedBody)[] = [
"firstName",
"lastName",
Expand All @@ -39,15 +40,14 @@ export default async function handler(req: Request, res: Response) {
"preworkRepo",
];

// Note: Type argument removed since TypeScript infers <ParsedBody> from the argument types
// Validate required fields in the parsed body
const hasErrors = checkParams(parsedBody, requiredParams);

if (hasErrors) {
return res.status(422).json({
error: "Missing or incorrect required property",
});
return res.status(422).json({ error: "Missing or incorrect required property" });
}

// Construct the text message to be sent
const text = [
`First Name: \`${parsedBody.firstName ?? ""}\``,
`Last Name: \`${parsedBody.lastName ?? ""}\``,
Expand All @@ -65,17 +65,14 @@ export default async function handler(req: Request, res: Response) {
`Prework Repository: \`${parsedBody.preworkRepo ?? ""}\``,
].join("\n");

const payload = JSON.stringify({ text });

await axios({
method: "POST",
baseURL: "https://hooks.slack.com",
url: `/services/${process.env.APPLY_WEBHOOK_ID ?? ""}`,
data: payload,
});
// Send the payload to the configured Slack webhook URL
await axios.post(`https://hooks.slack.com/services/${process.env.APPLY_WEBHOOK_ID ?? ""}`, JSON.stringify({ text }));

// Respond with success message
return res.status(200).json({ message: "SUCCESS" });
} catch (err) {
// Log the error for debugging and respond with an error message
console.error("Failed to post to #mentor channel:", err);
return res.status(500).json({ message: "Failed to post to #mentor channel" });
}
}
43 changes: 19 additions & 24 deletions src/pages/api/mentor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ interface ParsedBody {

export default async function handler(req: Request, res: Response) {
try {
const parsedBody: ParsedBody = req.body;
// Correctly typed requiredParams to leverage TypeScript's type checking
const parsedBody = req.body as ParsedBody; // Cast to ensure body matches ParsedBody interface
const requiredParams: (keyof ParsedBody)[] = [
"name",
"email",
Expand All @@ -27,41 +26,37 @@ export default async function handler(req: Request, res: Response) {
"employer-restrictions",
];

// Assuming checkParams is now properly generic
const hasErrors = checkParams<ParsedBody>(parsedBody, requiredParams);
// Leverage TypeScript's type inference for generic parameters
const hasErrors = checkParams(parsedBody, requiredParams);

if (hasErrors) {
return res.status(422).json({
error: "Missing or incorrect required property",
});
}

// Construct the message text
const text = [
`Name: \`${parsedBody.name}\``,
`\nEmail: \`${parsedBody.email}\``,
`\nBranch of Service: \`${parsedBody["branch-of-service"]}\``,
`\nTechnical Expertise: \`${parsedBody["technical-expertise"]}\``,
`\nGithub, Portfolio or LinkedIn: \`${parsedBody["github-portfolio-or-linkedin"]}\``,
`\nLocation: \`${parsedBody.location}\``,
`\nEmployer Restrictions: \`${parsedBody["employer-restrictions"]}\``,
].join("");
`Email: \`${parsedBody.email}\``,
`Branch of Service: \`${parsedBody["branch-of-service"]}\``,
`Technical Expertise: \`${parsedBody["technical-expertise"]}\``,
`Github, Portfolio or LinkedIn: \`${parsedBody["github-portfolio-or-linkedin"]}\``,
`Location: \`${parsedBody.location}\``,
`Employer Restrictions: \`${parsedBody["employer-restrictions"]}\``,
].join("\n");

const payload = JSON.stringify({ text });

await axios({
method: "POST",
baseURL: "https://hooks.slack.com",
url: `/services/${process.env.MENTOR_WEBHOOK_ID ?? ""}`,
data: payload,
}).catch((err: unknown) => {
// More specific error handling could be applied here
console.error("Error posting to Slack:", err);
throw new Error("Failed to post to Slack");
});
// Send the constructed message to Slack
await axios.post(`https://hooks.slack.com/services/${process.env.MENTOR_WEBHOOK_ID ?? ""}`, JSON.stringify({ text }))
.catch((err) => {
console.error("Error posting to Slack:", err);
throw new Error("Failed to post to Slack");
});

// Respond with a success message
return res.status(200).json({ message: "SUCCESS" });
} catch (err) {
console.error("Handler error:", err);
return res.status(500).json({ message: "Failed post to #mentor channel" });
return res.status(500).json({ message: "Failed to post to #mentor channel" });
}
}

0 comments on commit 61c01d1

Please sign in to comment.