-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update link routes and add shared_access post api to get the user det…
…ails from shared linkl
- Loading branch information
1 parent
d050394
commit d564702
Showing
5 changed files
with
102 additions
and
70 deletions.
There are no files selected for viewing
This file contains 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 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 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,61 @@ | ||
import bcryptjs from 'bcryptjs'; | ||
import prisma from '@lib/prisma'; | ||
import LinkService from '@/services/linkService'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
export async function POST(req: NextRequest): Promise<NextResponse> { | ||
try { | ||
const { linkId, first_name, last_name, email, password } = await req.json(); | ||
|
||
if (!linkId || !first_name || !last_name || !email || !password) { | ||
return createErrorResponse('Link ID, firstName, lastName, email and password are required.', 400); | ||
} | ||
|
||
const link = await LinkService.getLink(linkId); | ||
if (!link) { | ||
return createErrorResponse('Link not found.', 404); | ||
} | ||
|
||
if (!link.hasSharingOptions) { | ||
return createErrorResponse('This link does not require sharing options.', 400); | ||
} | ||
|
||
const isPasswordValid = await validatePassword(password, link.password as string); | ||
if (!isPasswordValid) { | ||
return createErrorResponse('Invalid password.', 403); | ||
} | ||
|
||
await logLinkRecipient(linkId, first_name, last_name, email); | ||
|
||
const signedUrl = await LinkService.getFileFromLink(linkId); | ||
return NextResponse.json({ message: 'Link URL generated', data: { signedUrl } }, { status: 200 }); | ||
} catch (error) { | ||
return createErrorResponse('Server error.', 500, error); | ||
} | ||
} | ||
|
||
function createErrorResponse(message: string, status: number, details?: any) { | ||
if (Array.isArray(details) && details.length === 2) { | ||
const [errorCode, errorMessage] = details; | ||
console.error(`[${new Date().toISOString()}] ${errorMessage}`, details); | ||
return NextResponse.json({ error: errorMessage, details }, { status: errorCode }); | ||
} else { | ||
console.error(`[${new Date().toISOString()}] ${message}`, details); | ||
return NextResponse.json({ error: message, details }, { status }); | ||
} | ||
} | ||
|
||
async function validatePassword(providedPassword: string, storedPassword: string) { | ||
return bcryptjs.compare(providedPassword, storedPassword); | ||
} | ||
|
||
async function logLinkRecipient(linkId: string, first_name: string, last_name: string, email: string) { | ||
return prisma.linkRecipients.create({ | ||
data: { | ||
linkId, | ||
first_name, | ||
last_name, | ||
email, | ||
}, | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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