-
Notifications
You must be signed in to change notification settings - Fork 0
Fix CORS Issues and Add Contact Message Management #1
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
| @@ -1,18 +1,55 @@ | ||
| import { PrismaClient } from "../../generated/prisma"; | ||
| import { logger } from "./logger"; | ||
|
|
||
|
|
||
| interface CustomNodeJsGlobal extends Global { | ||
| prisma: PrismaClient; | ||
| } | ||
|
|
||
| declare const global: CustomNodeJsGlobal; | ||
|
|
||
| export const db = global.prisma || new PrismaClient(); | ||
|
|
||
| db.$connect() | ||
| .then(() => { | ||
| logger.info("[PRISMA] : connected to database"); | ||
| }) | ||
| .catch((error: string) => { | ||
| logger.error("[PRISMA] : failed to connect database : ", error); | ||
| // Create PrismaClient with logging enabled to help diagnose connection issues | ||
| if (!global.prisma) { | ||
| global.prisma = new PrismaClient({ | ||
| log: ["info", "warn", "error"], | ||
| }); | ||
| } | ||
| export const db = global.prisma; | ||
|
|
||
| // Track whether Prisma has successfully connected so other parts of the app | ||
| // can report DB health without attempting queries that would fail. | ||
| let _isDbConnected = false; | ||
| export function isDbConnected() { | ||
| return _isDbConnected; | ||
| } | ||
|
|
||
| // Attempt to connect with a small retry/backoff loop to handle transient network issues. | ||
| async function connectWithRetry(maxRetries = 5, initialDelayMs = 1000) { | ||
| let attempt = 0; | ||
| let delay = initialDelayMs; | ||
| while (attempt < maxRetries) { | ||
| try { | ||
| await db.$connect(); | ||
| _isDbConnected = true; | ||
| logger.info("[PRISMA] : connected to database"); | ||
| return; | ||
| } catch (error: any) { | ||
| attempt += 1; | ||
| logger.warn( | ||
| `[PRISMA] : failed to connect (attempt ${attempt}/${maxRetries}):`, | ||
| error?.message ?? error, | ||
| ); | ||
| if (attempt >= maxRetries) { | ||
| logger.error("[PRISMA] : exhausted all connection retries.", error); | ||
| // leave _isDbConnected as false so health checks report DB down | ||
| return; | ||
| } | ||
| // exponential backoff | ||
| await new Promise((res) => setTimeout(res, delay)); | ||
| delay *= 2; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Start background connection attempts (do not block module initialization). | ||
| connectWithRetry().catch((err) => logger.error("[PRISMA] : connectWithRetry unexpected error:", err)); |
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
52 changes: 51 additions & 1 deletion
52
src/controllers/v1/admin/contactForm/contactForms.controller.ts
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 |
|---|---|---|
| @@ -1,15 +1,65 @@ | ||
| import { db } from "@/config/database"; | ||
| import catchAsync from "@/handlers/async.handler"; | ||
| import { APIError } from "@/utils/APIError"; | ||
| import { Request, Response } from "express"; | ||
| import { ContactStatus } from "generated/prisma"; | ||
|
|
||
| const getAllContactForms = catchAsync(async (req: Request, res: Response) => { | ||
| const forms = await db.contactUs.findMany(); | ||
| const forms = await db.contactUs.findMany({ | ||
| orderBy: { | ||
| createdAt: 'desc' | ||
| } | ||
| }); | ||
| res.status(200).json({ | ||
| status: "success", | ||
| data: forms, | ||
| }); | ||
| }); | ||
|
|
||
| const updateContactStatus = catchAsync(async (req: Request, res: Response) => { | ||
| const { id } = req.params; | ||
| const { status } = req.body; | ||
|
|
||
| if (!id || id.trim() === "") { | ||
| throw new APIError(400, "Contact ID is required"); | ||
| } | ||
|
|
||
| if (!status) { | ||
| throw new APIError(400, "Status is required"); | ||
samarth3301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const validStatuses = Object.values(ContactStatus); | ||
| if (!validStatuses.includes(status.toUpperCase())) { | ||
| throw new APIError(400, `Invalid status. Must be one of: ${validStatuses.join(', ')}`); | ||
| } | ||
|
|
||
| const existingContact = await db.contactUs.findUnique({ | ||
| where: { | ||
| id: id | ||
| } | ||
| }); | ||
|
|
||
| if (!existingContact) { | ||
| throw new APIError(404, "Contact not found"); | ||
| } | ||
samarth3301 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const updatedContact = await db.contactUs.update({ | ||
| where: { | ||
| id: id | ||
| }, | ||
| data: { | ||
| status: status.toUpperCase() as ContactStatus | ||
| } | ||
| }); | ||
|
|
||
| res.status(200).json({ | ||
| status: "success", | ||
| message: "Contact status updated successfully", | ||
| data: updatedContact | ||
| }); | ||
| }); | ||
|
|
||
| export default { | ||
| getAllContactForms, | ||
| updateContactStatus, | ||
| }; | ||
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
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.