-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete Professionals endpoint (#27)
- Loading branch information
Showing
23 changed files
with
557 additions
and
195 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ PROJECT_ID= | |
CLIENT_EMAIL= | ||
PRIVATE_KEY= | ||
BUCKET_NAME= | ||
|
||
# Prisma | ||
DISABLE_ERD=true |
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
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,79 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { z } from 'zod' | ||
|
||
import { prismaClient } from '@lib/prisma' | ||
import { getUserFromHeader } from '@lib/getUserFromHeader' | ||
|
||
export async function saveProfessional( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const { code } = req.body | ||
|
||
const codeSchema = z.string() | ||
|
||
const parsedCode = codeSchema.safeParse(code) | ||
|
||
if (!parsedCode.success) { | ||
return res.status(400).json({ | ||
message: 'Invalid professional code', | ||
error: parsedCode.error.issues, | ||
}) | ||
} | ||
|
||
const { email } = getUserFromHeader(req) | ||
|
||
try { | ||
const professional = await prismaClient.professional.findUnique({ | ||
where: { | ||
code: parsedCode.data, | ||
}, | ||
include: { | ||
saved_users: { | ||
select: { | ||
email: true, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
if (!professional) { | ||
return res.status(404).json({ message: 'Professional not found' }) | ||
} | ||
|
||
const isProfessionalSaved = professional.saved_users.includes({ email }) | ||
|
||
if (isProfessionalSaved) { | ||
await prismaClient.user.update({ | ||
where: { | ||
email, | ||
}, | ||
data: { | ||
saved_professionals: { | ||
disconnect: { | ||
code: parsedCode.data, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} else { | ||
await prismaClient.user.update({ | ||
where: { | ||
email, | ||
}, | ||
data: { | ||
saved_professionals: { | ||
connect: { | ||
code: parsedCode.data, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
return res.status(204).end() | ||
} catch (error) { | ||
console.log(error) | ||
return res.status(500).json({ message: 'Internal server error.' }) | ||
} | ||
} |
Oops, something went wrong.
adb554a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
formatch – ./
formatch.vercel.app
formatch-mateusfg7.vercel.app
formatch-git-main-mateusfg7.vercel.app