-
Notifications
You must be signed in to change notification settings - Fork 78
Feat/nfc payload endpoint #189
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
Open
Midoriya-w
wants to merge
8
commits into
Dev-Card:main
Choose a base branch
from
Midoriya-w:feat/nfc-payload-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9e9470
feat: add context-card diffing utility and validation layer
Midoriya-w b10c39f
feat: add NFC tag payload generation endpoint with card ownership val…
Midoriya-w c416a3a
fix: add Zod query validation and improve error handling in NFC route
Midoriya-w a2ed2f6
fix: resolve merge conflicts
Midoriya-w f24bd45
fix: resolve merge conflicts in app.ts
Midoriya-w 5c84994
fix: add typed response schema NfcPayloadResponse
Midoriya-w 1fc961c
fix: remove typo in import statement in cards.ts
Midoriya-w 41140d6
refactor: narrow try catch scope in NFC payload route
Midoriya-w 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 |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; | ||
| import { z } from 'zod'; | ||
|
|
||
| type NfcPayloadResponse = { | ||
| type: 'URI'; | ||
| payload: string; | ||
| }; | ||
|
|
||
| const nfcQuerySchema = z.object({ | ||
| card: z.string().uuid('Invalid card ID format').optional(), | ||
| }); | ||
|
|
||
| export async function nfcRoutes(app: FastifyInstance) { | ||
| app.addHook('preHandler', app.authenticate); | ||
|
|
||
| // GET /api/nfc/payload — returns NDEF URI payload for user's default DevCard URL | ||
| // GET /api/nfc/payload?card=<cardId> — returns payload for a specific card | ||
| app.get( | ||
| '/payload', | ||
| async ( | ||
| request: FastifyRequest<{ Querystring: { card?: string } }>, | ||
| reply: FastifyReply | ||
| ) => { | ||
| const userId = (request.user as any).id; | ||
|
|
||
| // Validate query params with Zod | ||
| const parseResult = nfcQuerySchema.safeParse(request.query); | ||
| if (!parseResult.success) { | ||
| return reply.status(400).send({ | ||
| error: 'Invalid query parameters', | ||
| details: parseResult.error.flatten(), | ||
| }); | ||
| } | ||
|
|
||
| const { card: cardId } = parseResult.data; | ||
|
|
||
| let username: string; | ||
|
|
||
| // Fetch username | ||
| try { | ||
| const user = await app.prisma.user.findUnique({ | ||
| where: { id: userId }, | ||
| select: { username: true }, | ||
| }); | ||
|
|
||
| if (!user) { | ||
| return reply.status(404).send({ | ||
| error: 'User not found', | ||
| }); | ||
| } | ||
|
|
||
| username = user.username; | ||
| } catch (err) { | ||
| request.log.error( | ||
| { err }, | ||
| 'Failed to fetch user for NFC payload' | ||
| ); | ||
| return reply.status(500).send({ | ||
| error: 'Failed to fetch user profile', | ||
| }); | ||
| } | ||
|
|
||
| // If a specific card is requested, verify ownership | ||
| if (cardId) { | ||
| try { | ||
| const card = await app.prisma.card.findUnique({ | ||
| where: { id: cardId }, | ||
| select: { userId: true }, | ||
| }); | ||
|
|
||
| if (!card || card.userId !== userId) { | ||
| return reply.status(404).send({ | ||
| error: 'Card not found', | ||
| }); | ||
| } | ||
| } catch (err) { | ||
| request.log.error( | ||
| { err }, | ||
| 'Failed to fetch card for NFC payload' | ||
| ); | ||
| return reply.status(500).send({ | ||
| error: 'Failed to fetch card', | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const payloadUrl = `https://dev-card.vercel.app/${username}${ | ||
| cardId ? `?card=${cardId}` : '' | ||
| }`; | ||
|
|
||
| const response: NfcPayloadResponse = { | ||
| type: 'URI', | ||
| payload: payloadUrl, | ||
| }; | ||
|
|
||
| return reply.send(response); | ||
| } | ||
| ); | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { validateCardPlatforms, diffCardPlatforms } from '../cards'; | ||
|
|
||
| describe('validateCardPlatforms', () => { | ||
| it('passes with valid platforms', () => { | ||
| const result = validateCardPlatforms(['github', 'linkedin']); | ||
| expect(result.valid).toBe(true); | ||
| expect(result.errors).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('fails with empty array', () => { | ||
| const result = validateCardPlatforms([]); | ||
| expect(result.valid).toBe(false); | ||
| expect(result.errors).toContain('At least one platform is required.'); | ||
| }); | ||
|
|
||
| it('fails with unknown platform', () => { | ||
| const result = validateCardPlatforms(['github', 'myspace']); | ||
| expect(result.valid).toBe(false); | ||
| expect(result.errors.some(e => e.includes('myspace'))).toBe(true); | ||
| }); | ||
|
|
||
| it('fails with duplicate platforms', () => { | ||
| const result = validateCardPlatforms(['github', 'github']); | ||
| expect(result.valid).toBe(false); | ||
| expect(result.errors.some(e => e.includes('Duplicate'))).toBe(true); | ||
| }); | ||
|
|
||
| it('passes with exactly 10 platforms', () => { | ||
| const platforms = ['github','linkedin','twitter','youtube','twitch', | ||
| 'discord','devto','medium','dribbble','leetcode']; | ||
| const result = validateCardPlatforms(platforms); | ||
| expect(result.valid).toBe(true); | ||
| }); | ||
|
|
||
| it('fails with more than 10 platforms', () => { | ||
| const platforms = ['github','linkedin','twitter','youtube','twitch', | ||
| 'discord','devto','medium','dribbble','leetcode','npm']; | ||
| const result = validateCardPlatforms(platforms); | ||
| expect(result.valid).toBe(false); | ||
| expect(result.errors.some(e => e.includes('Maximum 10'))).toBe(true); | ||
| }); | ||
|
|
||
| it('fails with all invalid platforms', () => { | ||
| const result = validateCardPlatforms(['myspace', 'bebo']); | ||
| expect(result.valid).toBe(false); | ||
| expect(result.errors.length).toBeGreaterThanOrEqual(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe('diffCardPlatforms', () => { | ||
| it('correctly identifies added, removed, unchanged', () => { | ||
| const diff = diffCardPlatforms(['github', 'linkedin'], ['github', 'twitter']); | ||
| expect(diff.added).toEqual(['twitter']); | ||
| expect(diff.removed).toEqual(['linkedin']); | ||
| expect(diff.unchanged).toEqual(['github']); | ||
| }); | ||
|
|
||
| it('handles empty old card', () => { | ||
| const diff = diffCardPlatforms([], ['github']); | ||
| expect(diff.added).toEqual(['github']); | ||
| expect(diff.removed).toEqual([]); | ||
| expect(diff.unchanged).toEqual([]); | ||
| }); | ||
|
|
||
| it('handles identical cards', () => { | ||
| const diff = diffCardPlatforms(['github'], ['github']); | ||
| expect(diff.added).toEqual([]); | ||
| expect(diff.removed).toEqual([]); | ||
| expect(diff.unchanged).toEqual(['github']); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| export type CardValidationResult = { | ||
| valid: boolean; | ||
| errors: string[]; | ||
| }; | ||
|
|
||
| const PLATFORMS = new Set([ | ||
| 'github', 'linkedin', 'twitter', 'instagram', 'youtube', | ||
| 'twitch', 'discord', 'devto', 'hashnode', 'medium', | ||
| 'dribbble', 'behance', 'figma', 'stackoverflow', 'leetcode', | ||
| 'codepen', 'replit', 'npm', 'producthunt', 'website', | ||
| ]); | ||
|
|
||
| export function validateCardPlatforms(platforms: string[]): CardValidationResult { | ||
| const errors: string[] = []; | ||
|
|
||
| if (platforms.length === 0) { | ||
| errors.push('At least one platform is required.'); | ||
| } | ||
|
|
||
| if (platforms.length > 10) { | ||
| errors.push(`Maximum 10 platforms allowed, got ${platforms.length}.`); | ||
| } | ||
|
|
||
| const seen = new Set<string>(); | ||
| for (const p of platforms) { | ||
| if (!PLATFORMS.has(p)) { | ||
| errors.push(`Unknown platform: "${p}".`); | ||
| } | ||
| if (seen.has(p)) { | ||
| errors.push(`Duplicate platform: "${p}".`); | ||
| } | ||
| seen.add(p); | ||
| } | ||
|
|
||
| return { valid: errors.length === 0, errors }; | ||
| } | ||
|
|
||
| export function diffCardPlatforms( | ||
| oldCard: string[], | ||
| newCard: string[] | ||
| ): { added: string[]; removed: string[]; unchanged: string[] } { | ||
| const oldSet = new Set(oldCard); | ||
| const newSet = new Set(newCard); | ||
|
|
||
| return { | ||
| added: newCard.filter(p => !oldSet.has(p)), | ||
| removed: oldCard.filter(p => !newSet.has(p)), | ||
| unchanged: oldCard.filter(p => newSet.has(p)), | ||
| }; | ||
| } |
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,2 +1,3 @@ | ||
| export * from './platforms'; | ||
| export * from './types'; | ||
| export * from './cards'; |
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.
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.
Catch position should be here.
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.
Updated this section as well to keep the try/catch scoped only around the database call. Thanks for catching that.