1- import { CardVisibility , Prisma } from '@prisma/client' ;
1+ import { Card , CardVisibility , Prisma } from '@prisma/client' ;
22
33import { generateUniqueSlug } from '../utils/slug' ;
44
@@ -11,6 +11,14 @@ type RawCard = { id: string; title: string; isDefault: boolean; cardLinks: CardL
1111export type CardResponse = { id : string ; title : string ; isDefault : boolean ; links : unknown [ ] } ;
1212
1313
14+ export interface UpdateCardBody {
15+ title ?:string ;
16+ description ?:string ;
17+ visibility ?: CardVisibility ;
18+ qrEnabled ?: boolean ;
19+ }
20+
21+
1422function mapCard ( card : RawCard ) : CardResponse {
1523 return {
1624 id : card . id ,
@@ -106,50 +114,28 @@ export async function updateCard(
106114 app : FastifyInstance ,
107115 userId : string ,
108116 id : string ,
109- body : { title ?: string ; linkIds ?: string [ ] } ,
110- ) : Promise < CardResponse | null > {
111- const existing = await app . prisma . card . findFirst ( { where : { id, userId } } ) ;
112- if ( ! existing ) {
113- return null ;
114- }
115-
116- if ( body . title ) {
117- await app . prisma . card . update ( { where : { id } , data : { title : body . title } } ) ;
118- }
117+ body : UpdateCardBody ,
118+ ) : Promise < Card > {
119+ const { title, description, visibility, qrEnabled} = body
119120
120- if ( body . linkIds ) {
121- if ( body . linkIds . length > 0 ) {
122- const ownedLinks = await app . prisma . platformLink . findMany ( {
123- where : { id : { in : body . linkIds } , userId } ,
124- select : { id : true } ,
125- } ) ;
126-
127- if ( ownedLinks . length !== body . linkIds . length ) {
128- throw Object . assign ( new Error ( 'Link ownership mismatch' ) , { code : 'OWNERSHIP' } ) ;
129- }
121+ const existing = await app . prisma . card . findFirst ( { where : { id, userId } } ) ;
122+ if ( ! existing ) {
123+ throw Object . assign ( new Error ( 'NotFound' ) , { code : 'NOT_FOUND' } ) ;
130124 }
131125
132- const linkIds = body . linkIds ;
133- await app . prisma . $transaction ( async ( tx : Prisma . TransactionClient ) => {
134- await tx . cardLink . deleteMany ( { where : { cardId : id } } ) ;
135- if ( linkIds . length > 0 ) {
136- await tx . cardLink . createMany ( {
137- data : linkIds . map ( ( linkId , index ) => ( { cardId : id , platformLinkId : linkId , displayOrder : index } ) ) ,
138- } ) ;
126+ const updated = await app . prisma . card . update ( {
127+ where : {
128+ id,
129+ } ,
130+ data :{
131+ title,
132+ description,
133+ visibility,
134+ qrEnabled
139135 }
140- } ) ;
141- }
142-
143- const updated = ( await app . prisma . card . findUnique ( {
144- where : { id } ,
145- include : { cardLinks : { include : { platformLink : true } , orderBy : { displayOrder : 'asc' } } } ,
146- } ) ) as unknown as RawCard | null ;
147-
148- if ( ! updated ) {
149- return null ;
150- }
136+ } )
151137
152- return mapCard ( updated ) ;
138+ return updated ;
153139}
154140
155141//Delete card service
@@ -184,9 +170,10 @@ export async function deleteCard(app: FastifyInstance, userId: string, id: strin
184170//Set default card service
185171export async function setDefaultCard ( app : FastifyInstance , userId : string , id : string ) : Promise < { message : string } | null > {
186172 const existing = await app . prisma . card . findFirst ( { where : { id, userId } } ) ;
187- if ( ! existing ) {
188- return null ;
189- }
173+
174+ if ( ! existing ) {
175+ throw Object . assign ( new Error ( 'NotFound' ) , { code : 'NOT_FOUND' } ) ;
176+ }
190177
191178 await app . prisma . $transaction ( async ( tx : Prisma . TransactionClient ) => {
192179 await tx . card . updateMany ( { where : { userId } , data : { isDefault : false } } ) ;
@@ -251,6 +238,7 @@ export async function addPlatFormLinks(app: FastifyInstance, userId: string, id:
251238 } )
252239}
253240
241+ //Shares card
254242export async function shareCard ( app : FastifyInstance , userId :string , id : string ) {
255243 const card = await app . prisma . card . findFirst ( {
256244 where :{
@@ -279,6 +267,7 @@ export async function shareCard(app: FastifyInstance, userId:string, id: string)
279267 } ;
280268}
281269
270+ //Gets share card
282271export async function getSharedCard ( app :FastifyInstance , slug :string ) {
283272 const card = await app . prisma . card . findUnique ( {
284273 where : {
@@ -303,6 +292,7 @@ export async function getSharedCard(app:FastifyInstance, slug:string){
303292 return card
304293}
305294
295+ //Genreate qr
306296export async function genrateQr ( app : FastifyInstance , userId :string , id : string ) {
307297 const card = await app . prisma . card . findFirst ( {
308298 where :{
0 commit comments