Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Feature/api in app router #237

Merged
merged 16 commits into from
Aug 11, 2023
Merged
22 changes: 22 additions & 0 deletions app/api/user/getAllPublicUserData/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { NextResponse } from "next/server";
import validateParams from "../../../../utils/api/validateParams";
import getAllPublicUserData from "../../../../utils/db/user/getAllPublicUserData";

export async function POST(request: Request) {
const req = await request.json();
const { missingParams } = validateParams(req, ["email"]);

if (missingParams.length > 0) {
return NextResponse.json({
error: `Missing parameters: ${missingParams.join(", ")}`,
});
}

try {
let dbResponse = await getAllPublicUserData({ email: req.email });
return NextResponse.json(dbResponse);
} catch (err) {
console.error("Error fetching db data:", err);
return NextResponse.json({ error: "Failed to fetch db data." });
}
}
22 changes: 22 additions & 0 deletions app/api/user/settings/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { NextResponse } from "next/server";
import getUserSettings from "../../../../utils/db/user/settings";
import validateParams from "../../../../utils/api/validateParams";

export async function POST(request: Request) {
const req = await request.json();
const { missingParams } = validateParams(req, ["email"]);

if (missingParams.length > 0) {
return NextResponse.json({
error: `Missing parameters: ${missingParams.join(", ")}`,
});
}

try {
let dbResponse = await getUserSettings({ email: req.email });
return NextResponse.json(dbResponse);
} catch (err) {
console.error("Error fetching db data:", err);
return NextResponse.json({ error: "Failed to fetch db data." });
}
}
25 changes: 25 additions & 0 deletions app/api/user/updateSettings/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { NextResponse } from "next/server";
import validateParams from "../../../../utils/api/validateParams";
import patchUserSettings from "../../../../utils/db/user/patchUserSettings";

export async function POST(request: Request) {
const req = await request.json();
const { missingParams } = validateParams(req, ["email", "userSettings"]);

if (missingParams.length > 0) {
return NextResponse.json({
error: `Missing parameters: ${missingParams.join(", ")}`,
});
}

try {
let dbResponse = await patchUserSettings({
email: req.email,
userSettings: req.userSettings,
});
return NextResponse.json(dbResponse);
} catch (err) {
console.error("Error fetching db data:", err);
return NextResponse.json({ error: "Failed to fetch db data." });
}
}
10 changes: 0 additions & 10 deletions pages/api/discord/getUser.ts

This file was deleted.

10 changes: 0 additions & 10 deletions pages/api/user/getAllPublicUserData.ts

This file was deleted.

10 changes: 0 additions & 10 deletions pages/api/user/settings.ts

This file was deleted.

14 changes: 0 additions & 14 deletions pages/api/user/updateSettings.ts

This file was deleted.

36 changes: 22 additions & 14 deletions public/logos/confluence.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions utils/api/validateParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
interface ValidationResult {
missingParams: string[];
requiredParams: string[];
}

export default function validateParams(
body: any,
requiredParams: string[]
): ValidationResult {
let missingParams: string[] = [];

for (let paramName of requiredParams) {
if (!body[paramName]) {
missingParams.push(paramName);
}
}

return { missingParams, requiredParams };
}
13 changes: 0 additions & 13 deletions utils/db/user/getAllData.ts

This file was deleted.

7 changes: 4 additions & 3 deletions utils/db/user/getAllPublicUserData.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import "server-only";
import executeRequest from "../azuredb";

export default async function getAllPublicUserData(user): Promise<any> {
export default async function getAllPublicUserData({ email }): Promise<any> {
try {
let data = await executeRequest(
`EXEC dbo.get_user_all_public_data @watermelon_user = '${user}'`
`EXEC dbo.get_user_all_public_data @watermelon_user = '${email}'`
);
return data;
} catch (err) {
console.error(err);
return err;
throw err;
}
}
12 changes: 6 additions & 6 deletions utils/db/user/patchUserSettings.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import executeRequest from "../azuredb";

export default async function updateUserSettings(
user,
userSettings
): Promise<any> {
export default async function updateUserSettings({
email,
userSettings,
}): Promise<any> {
try {
let data = await executeRequest(
`EXEC dbo.update_userSettings @watermelon_user = '${user}', @AISummary='${userSettings.AISummary}', @JiraTickets='${userSettings.JiraTickets}', @SlackMessages='${userSettings.SlackMessages}', @GitHubPRs='${userSettings.GitHubPRs}', @NotionPages= '${userSettings.NotionPages}', @LinearTickets= '${userSettings.LinearTickets}', @ConfluenceDocs= '${userSettings.ConfluenceDocs}'`
`EXEC dbo.update_userSettings @watermelon_user = '${email}', @AISummary='${userSettings.AISummary}', @JiraTickets='${userSettings.JiraTickets}', @SlackMessages='${userSettings.SlackMessages}', @GitHubPRs='${userSettings.GitHubPRs}', @NotionPages= '${userSettings.NotionPages}', @LinearTickets= '${userSettings.LinearTickets}', @ConfluenceDocs= '${userSettings.ConfluenceDocs}'`
);
return data;
} catch (err) {
console.error(err);
return err;
throw err;
}
}
6 changes: 3 additions & 3 deletions utils/db/user/settings.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import executeRequest from "../azuredb";

export default async function getUser(user): Promise<any> {
export default async function getUser({ email }): Promise<any> {
try {
let data = await executeRequest(
`EXEC dbo.get_user_settings @watermelon_user = '${user}'`
`EXEC dbo.get_user_settings @watermelon_user = '${email}'`
);
return data;
} catch (err) {
console.error(err);
return err;
throw err;
}
}
Loading