|
1 | | -import type { FastifyInstance } from 'fastify' |
2 | | -import type { Prisma } from '@prisma/client' |
| 1 | +import type { Prisma } from '@prisma/client'; |
| 2 | +import type { FastifyInstance } from 'fastify'; |
| 3 | + |
| 4 | +type CardLinkResponse = { platformLink: unknown }; |
| 5 | +type RawCard = { id: string; title: string; isDefault: boolean; cardLinks: CardLinkResponse[] }; |
| 6 | +type CardResponse = { id: string; title: string; isDefault: boolean; links: unknown[] }; |
| 7 | + |
| 8 | +function mapCard(card: RawCard): CardResponse { |
| 9 | + return { |
| 10 | + id: card.id, |
| 11 | + title: card.title, |
| 12 | + isDefault: card.isDefault, |
| 13 | + links: card.cardLinks.map((cardLink) => cardLink.platformLink), |
| 14 | + }; |
| 15 | +} |
3 | 16 |
|
4 | | -export async function listCards(app: FastifyInstance, userId: string) { |
5 | | - const cards = await app.prisma.card.findMany({ |
| 17 | +export async function listCards(app: FastifyInstance, userId: string): Promise<CardResponse[]> { |
| 18 | + const cards = (await app.prisma.card.findMany({ |
6 | 19 | where: { userId }, |
7 | 20 | take: 50, |
8 | 21 | include: { cardLinks: { include: { platformLink: true }, orderBy: { displayOrder: 'asc' } } }, |
9 | 22 | orderBy: { createdAt: 'asc' }, |
10 | | - }) |
| 23 | + })) as unknown as RawCard[]; |
11 | 24 |
|
12 | | - return cards.map((card: any) => ({ id: card.id, title: card.title, isDefault: card.isDefault, links: card.cardLinks.map((cl: any) => cl.platformLink) })) |
| 25 | + return cards.map(mapCard); |
13 | 26 | } |
14 | 27 |
|
15 | | -export async function createCard(app: FastifyInstance, userId: string, body: { title: string; linkIds: string[] }) { |
| 28 | +export async function createCard(app: FastifyInstance, userId: string, body: { title: string; linkIds: string[] }): Promise<CardResponse> { |
16 | 29 | if (body.linkIds.length > 0) { |
17 | | - const ownedLinks = await app.prisma.platformLink.findMany({ where: { id: { in: body.linkIds }, userId }, select: { id: true } }) |
18 | | - if (ownedLinks.length !== body.linkIds.length) throw Object.assign(new Error('Link ownership mismatch'), { code: 'OWNERSHIP' }) |
| 30 | + const ownedLinks = await app.prisma.platformLink.findMany({ |
| 31 | + where: { id: { in: body.linkIds }, userId }, |
| 32 | + select: { id: true }, |
| 33 | + }); |
| 34 | + |
| 35 | + if (ownedLinks.length !== body.linkIds.length) { |
| 36 | + throw Object.assign(new Error('Link ownership mismatch'), { code: 'OWNERSHIP' }); |
| 37 | + } |
19 | 38 | } |
20 | 39 |
|
21 | | - const cardCount = await app.prisma.card.count({ where: { userId } }) |
| 40 | + const maxRetries = 3; |
| 41 | + for (let attempt = 1; attempt <= maxRetries; attempt++) { |
| 42 | + try { |
| 43 | + const card = (await app.prisma.$transaction( |
| 44 | + async (tx: Prisma.TransactionClient) => { |
| 45 | + const cardCount = await tx.card.count({ where: { userId } }); |
| 46 | + |
| 47 | + return tx.card.create({ |
| 48 | + data: { |
| 49 | + userId, |
| 50 | + title: body.title, |
| 51 | + isDefault: cardCount === 0, |
| 52 | + cardLinks: { |
| 53 | + create: body.linkIds.map((linkId, index) => ({ platformLinkId: linkId, displayOrder: index })), |
| 54 | + }, |
| 55 | + }, |
| 56 | + include: { cardLinks: { include: { platformLink: true }, orderBy: { displayOrder: 'asc' } } }, |
| 57 | + }); |
| 58 | + }, |
| 59 | + { |
| 60 | + isolationLevel: 'Serializable', |
| 61 | + }, |
| 62 | + )) as unknown as RawCard; |
| 63 | + |
| 64 | + return mapCard(card); |
| 65 | + } catch (error: unknown) { |
| 66 | + if ( |
| 67 | + typeof error === 'object' && |
| 68 | + error !== null && |
| 69 | + 'code' in error && |
| 70 | + (error as { code: string }).code === 'P2034' && |
| 71 | + attempt < maxRetries |
| 72 | + ) { |
| 73 | + continue; |
| 74 | + } |
22 | 75 |
|
23 | | - const card = await app.prisma.card.create({ |
24 | | - data: { |
25 | | - userId, |
26 | | - title: body.title, |
27 | | - isDefault: cardCount === 0, |
28 | | - cardLinks: { create: body.linkIds.map((linkId, index) => ({ platformLinkId: linkId, displayOrder: index })) }, |
29 | | - }, |
30 | | - include: { cardLinks: { include: { platformLink: true }, orderBy: { displayOrder: 'asc' } } }, |
31 | | - }) |
| 76 | + app.log.error(error); |
| 77 | + throw error; |
| 78 | + } |
| 79 | + } |
32 | 80 |
|
33 | | - return { id: card.id, title: card.title, isDefault: card.isDefault, links: card.cardLinks.map((cl: any) => cl.platformLink) } |
| 81 | + throw new Error('Failed to create card after retrying serialization conflicts'); |
34 | 82 | } |
35 | 83 |
|
36 | | -export async function updateCard(app: FastifyInstance, userId: string, id: string, body: { title?: string; linkIds?: string[] }) { |
37 | | - const existing = await app.prisma.card.findFirst({ where: { id, userId } }) |
38 | | - if (!existing) return null |
| 84 | +export async function updateCard( |
| 85 | + app: FastifyInstance, |
| 86 | + userId: string, |
| 87 | + id: string, |
| 88 | + body: { title?: string; linkIds?: string[] }, |
| 89 | +): Promise<CardResponse | null> { |
| 90 | + const existing = await app.prisma.card.findFirst({ where: { id, userId } }); |
| 91 | + if (!existing) { |
| 92 | + return null; |
| 93 | + } |
39 | 94 |
|
40 | 95 | if (body.title) { |
41 | | - await app.prisma.card.update({ where: { id }, data: { title: body.title } }) |
| 96 | + await app.prisma.card.update({ where: { id }, data: { title: body.title } }); |
42 | 97 | } |
43 | 98 |
|
44 | 99 | if (body.linkIds) { |
45 | 100 | if (body.linkIds.length > 0) { |
46 | | - const ownedLinks = await app.prisma.platformLink.findMany({ where: { id: { in: body.linkIds }, userId }, select: { id: true } }) |
47 | | - if (ownedLinks.length !== body.linkIds.length) throw Object.assign(new Error('Link ownership mismatch'), { code: 'OWNERSHIP' }) |
| 101 | + const ownedLinks = await app.prisma.platformLink.findMany({ |
| 102 | + where: { id: { in: body.linkIds }, userId }, |
| 103 | + select: { id: true }, |
| 104 | + }); |
| 105 | + |
| 106 | + if (ownedLinks.length !== body.linkIds.length) { |
| 107 | + throw Object.assign(new Error('Link ownership mismatch'), { code: 'OWNERSHIP' }); |
| 108 | + } |
48 | 109 | } |
49 | 110 |
|
50 | | - const linkIds = body.linkIds |
| 111 | + const linkIds = body.linkIds; |
51 | 112 | await app.prisma.$transaction(async (tx: Prisma.TransactionClient) => { |
52 | | - await tx.cardLink.deleteMany({ where: { cardId: id } }) |
| 113 | + await tx.cardLink.deleteMany({ where: { cardId: id } }); |
53 | 114 | if (linkIds.length > 0) { |
54 | | - await tx.cardLink.createMany({ data: linkIds.map((linkId, index) => ({ cardId: id, platformLinkId: linkId, displayOrder: index })) }) |
| 115 | + await tx.cardLink.createMany({ |
| 116 | + data: linkIds.map((linkId, index) => ({ cardId: id, platformLinkId: linkId, displayOrder: index })), |
| 117 | + }); |
55 | 118 | } |
56 | | - }) |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + const updated = (await app.prisma.card.findUnique({ |
| 123 | + where: { id }, |
| 124 | + include: { cardLinks: { include: { platformLink: true }, orderBy: { displayOrder: 'asc' } } }, |
| 125 | + })) as unknown as RawCard | null; |
| 126 | + |
| 127 | + if (!updated) { |
| 128 | + return null; |
57 | 129 | } |
58 | 130 |
|
59 | | - const updated = await app.prisma.card.findUnique({ where: { id }, include: { cardLinks: { include: { platformLink: true }, orderBy: { displayOrder: 'asc' } } } }) |
60 | | - return { id: updated!.id, title: updated!.title, isDefault: updated!.isDefault, links: updated!.cardLinks.map((cl: any) => cl.platformLink) } |
| 131 | + return mapCard(updated); |
61 | 132 | } |
62 | 133 |
|
63 | | -export async function deleteCard(app: FastifyInstance, userId: string, id: string) { |
| 134 | +export async function deleteCard(app: FastifyInstance, userId: string, id: string): Promise<null> { |
64 | 135 | return await app.prisma.$transaction(async (tx: Prisma.TransactionClient) => { |
65 | | - const existing = await tx.card.findFirst({ where: { id, userId } }) |
66 | | - if (!existing) return Object.assign(new Error('NotFound'), { code: 'NOT_FOUND' }) |
| 136 | + const existing = await tx.card.findFirst({ where: { id, userId } }); |
| 137 | + if (!existing) { |
| 138 | + throw Object.assign(new Error('NotFound'), { code: 'NOT_FOUND' }); |
| 139 | + } |
67 | 140 |
|
68 | | - const userCardCount = await tx.card.count({ where: { userId } }) |
69 | | - if (userCardCount <= 1) return Object.assign(new Error('Cannot delete last card'), { code: 'LAST_CARD' }) |
| 141 | + const userCardCount = await tx.card.count({ where: { userId } }); |
| 142 | + if (userCardCount <= 1) { |
| 143 | + throw Object.assign(new Error('Cannot delete last card'), { code: 'LAST_CARD' }); |
| 144 | + } |
70 | 145 |
|
71 | 146 | if (existing.isDefault) { |
72 | | - const oldestRemainingCard = await tx.card.findFirst({ where: { userId, id: { not: id } }, orderBy: { createdAt: 'asc' } }) |
| 147 | + const oldestRemainingCard = await tx.card.findFirst({ |
| 148 | + where: { userId, id: { not: id } }, |
| 149 | + orderBy: { createdAt: 'asc' }, |
| 150 | + }); |
| 151 | + |
73 | 152 | if (oldestRemainingCard) { |
74 | | - await tx.card.update({ where: { id: oldestRemainingCard.id }, data: { isDefault: true } }) |
| 153 | + await tx.card.update({ where: { id: oldestRemainingCard.id }, data: { isDefault: true } }); |
75 | 154 | } |
76 | 155 | } |
77 | 156 |
|
78 | | - await tx.card.delete({ where: { id } }) |
79 | | - return null |
80 | | - }) |
| 157 | + await tx.card.delete({ where: { id } }); |
| 158 | + return null; |
| 159 | + }); |
81 | 160 | } |
82 | 161 |
|
83 | | -export async function setDefaultCard(app: FastifyInstance, userId: string, id: string) { |
84 | | - const existing = await app.prisma.card.findFirst({ where: { id, userId } }) |
85 | | - if (!existing) return null |
| 162 | +export async function setDefaultCard(app: FastifyInstance, userId: string, id: string): Promise<{ message: string } | null> { |
| 163 | + const existing = await app.prisma.card.findFirst({ where: { id, userId } }); |
| 164 | + if (!existing) { |
| 165 | + return null; |
| 166 | + } |
86 | 167 |
|
87 | 168 | await app.prisma.$transaction(async (tx: Prisma.TransactionClient) => { |
88 | | - await tx.card.updateMany({ where: { userId }, data: { isDefault: false } }) |
89 | | - await tx.card.update({ where: { id }, data: { isDefault: true } }) |
90 | | - }) |
| 169 | + await tx.card.updateMany({ where: { userId }, data: { isDefault: false } }); |
| 170 | + await tx.card.update({ where: { id }, data: { isDefault: true } }); |
| 171 | + }); |
91 | 172 |
|
92 | | - return { message: 'Default card updated' } |
| 173 | + return { message: 'Default card updated' }; |
93 | 174 | } |
0 commit comments