@@ -2,13 +2,14 @@ import { CardVisibility, type Prisma } from '@prisma/client';
22
33import { generateUniqueSlug } from '../utils/slug' ;
44
5- import type { FastifyInstance } from 'fastify' ;
65import type { CreateCardBody } from '../routes/cards' ;
6+ import type { FastifyInstance } from 'fastify' ;
77
88type CardLinkResponse = { platformLink : unknown } ;
99type RawCard = { id : string ; title : string ; isDefault : boolean ; cardLinks : CardLinkResponse [ ] } ;
1010export type CardResponse = { id : string ; title : string ; isDefault : boolean ; links : unknown [ ] } ;
1111
12+
1213function mapCard ( card : RawCard ) : CardResponse {
1314 return {
1415 id : card . id ,
@@ -18,6 +19,7 @@ function mapCard(card: RawCard): CardResponse {
1819 } ;
1920}
2021
22+ //List card service
2123export async function listCards ( app : FastifyInstance , userId : string ) : Promise < CardResponse [ ] > {
2224 const cards = ( await app . prisma . card . findMany ( {
2325 where : { userId } ,
@@ -29,6 +31,7 @@ export async function listCards(app: FastifyInstance, userId: string): Promise<C
2931 return cards . map ( mapCard ) ;
3032}
3133
34+ //Creates card service
3235export async function createCard ( app : FastifyInstance , userId : string , body : CreateCardBody ) : Promise < CardResponse > {
3336 const { title , description , linkIds , visibility} = body
3437
@@ -97,6 +100,7 @@ export async function createCard(app: FastifyInstance, userId: string, body: Cre
97100 throw new Error ( 'Failed to create card after retrying serialization conflicts' ) ;
98101}
99102
103+ //Update card service
100104export async function updateCard (
101105 app : FastifyInstance ,
102106 userId : string ,
@@ -147,6 +151,7 @@ export async function updateCard(
147151 return mapCard ( updated ) ;
148152}
149153
154+ //Delete card service
150155export async function deleteCard ( app : FastifyInstance , userId : string , id : string ) : Promise < null > {
151156 return await app . prisma . $transaction ( async ( tx : Prisma . TransactionClient ) => {
152157 const existing = await tx . card . findFirst ( { where : { id, userId } } ) ;
@@ -175,6 +180,7 @@ export async function deleteCard(app: FastifyInstance, userId: string, id: strin
175180 } ) ;
176181}
177182
183+ //Set default card service
178184export async function setDefaultCard ( app : FastifyInstance , userId : string , id : string ) : Promise < { message : string } | null > {
179185 const existing = await app . prisma . card . findFirst ( { where : { id, userId } } ) ;
180186 if ( ! existing ) {
@@ -188,3 +194,58 @@ export async function setDefaultCard(app: FastifyInstance, userId: string, id: s
188194
189195 return { message : 'Default card updated' } ;
190196}
197+
198+ //Adds platfrom link
199+ export async function addPlatFormLinks ( app : FastifyInstance , userId : string , id :string , platformLinkId : string ) {
200+ const ownedCard = await app . prisma . card . findFirst ( {
201+ where : {
202+ id,
203+ userId
204+ }
205+ } )
206+
207+ if ( ! ownedCard ) {
208+ throw Object . assign (
209+ new Error ( 'Card not found or you do not have permission to modify it' ) ,
210+ { code : 'CARD_NOT_FOUND' }
211+ ) ;
212+ }
213+ const [ existingLink , platformLink ] = await Promise . all ( [
214+ app . prisma . cardLink . findUnique ( {
215+ where : {
216+ cardId_platformLinkId : {
217+ cardId : id ,
218+ platformLinkId,
219+ } ,
220+ } ,
221+ } ) ,
222+
223+ app . prisma . platformLink . findFirst ( {
224+ where : {
225+ id : platformLinkId ,
226+ userId,
227+ } ,
228+ } ) ,
229+ ] ) ;
230+
231+ if ( ! platformLink ) {
232+ throw Object . assign (
233+ new Error ( 'Platform link not found or does not belong to your account' ) ,
234+ { code : 'PLATFORM_LINK_NOT_FOUND' }
235+ ) ;
236+ }
237+
238+ if ( existingLink ) {
239+ throw Object . assign (
240+ new Error ( 'This platform link has already been added to the card' ) ,
241+ { code : 'LINK_ALREADY_EXISTS' }
242+ ) ;
243+ }
244+
245+ await app . prisma . cardLink . create ( {
246+ data : {
247+ cardId : id ,
248+ platformLinkId
249+ }
250+ } )
251+ }
0 commit comments