@@ -427,6 +427,22 @@ app.post("/api/receipts", requireAuth, async (req, res) => {
427427 return res . status ( 400 ) . json ( { error : "Invalid paymentType. Must be GOTOVINA, KARTICA, or TRANSAKCIJSKI" } ) ;
428428 }
429429
430+ // Validate active prodajno mjesto exists BEFORE creating anything in DB
431+ const appSettings = await prisma . appSettings . findUnique ( {
432+ where : { id : 1 } ,
433+ include : { prodajnoMjesto : true } ,
434+ } ) ;
435+ if ( ! appSettings ?. prodajnoMjesto ) {
436+ return res . status ( 400 ) . json ( { error : "Nije odabrano prodajno mjesto. Odaberite prodajno mjesto u admin postavkama." } ) ;
437+ }
438+ let firaApiKey ;
439+ try {
440+ firaApiKey = decrypt ( appSettings . prodajnoMjesto . firaApiKey ) ;
441+ if ( ! firaApiKey ) throw new Error ( "Prazan ključ" ) ;
442+ } catch {
443+ return res . status ( 500 ) . json ( { error : "Greška pri dešifriranju API ključa prodajnog mjesta." } ) ;
444+ }
445+
430446 // Create billing address if provided
431447 let billingAddressId = null ;
432448 if ( billingAddress ) {
@@ -473,6 +489,7 @@ app.post("/api/receipts", requireAuth, async (req, res) => {
473489 internalNote,
474490 discountValue,
475491 shippingCost,
492+ prodajnoMjestoNaziv : appSettings . prodajnoMjesto . name ,
476493 items : {
477494 create : items . map ( ( item ) => ( {
478495 name : item . name ,
@@ -509,7 +526,7 @@ app.post("/api/receipts", requireAuth, async (req, res) => {
509526 currency : receipt . currency ,
510527 paymentType : receipt . paymentType ,
511528 items : receipt . items ,
512- } ) ;
529+ } , { firaApiKey , prodajnoMjestoNaziv : appSettings . prodajnoMjesto . name } ) ;
513530
514531 if ( firaResult && firaResult . invoiceNumber ) {
515532 try {
@@ -547,7 +564,7 @@ app.post("/api/receipts", requireAuth, async (req, res) => {
547564 if ( Math . abs ( calculatedBrutto - brutto ) > 0.01 ) {
548565 return res . status ( 400 ) . json ( { error : "Total amount mismatch." } ) ;
549566 }
550- res . status ( 201 ) . json ( receipt ) ;
567+ res . status ( 201 ) . json ( { ... receipt , prodajnoMjestoNaziv : appSettings . prodajnoMjesto . name } ) ;
551568 } catch ( error ) {
552569 res . status ( 400 ) . json ( { error : error . message } ) ;
553570 }
@@ -848,14 +865,62 @@ app.post("/api/reports", requireAuth, async (req, res) => {
848865} ) ;
849866
850867
868+ // ========== APP SETTINGS API ==========
869+
870+ // GET trenutno odabrano prodajno mjesto
871+ app . get ( '/api/settings/active-location' , requireAuth , async ( req , res ) => {
872+ if ( req . user . role !== "ADMIN" ) return res . status ( 403 ) . json ( { error : "Unauthorized" } ) ;
873+ try {
874+ const settings = await prisma . appSettings . findUnique ( {
875+ where : { id : 1 } ,
876+ include : { prodajnoMjesto : true } ,
877+ } ) ;
878+ if ( ! settings || ! settings . prodajnoMjesto ) {
879+ return res . json ( { selectedProdajnoMjestoId : null , prodajnoMjesto : null } ) ;
880+ }
881+ res . json ( {
882+ selectedProdajnoMjestoId : settings . selectedProdajnoMjestoId ,
883+ prodajnoMjesto : { ...settings . prodajnoMjesto , firaApiKey : "********" } ,
884+ } ) ;
885+ } catch ( error ) {
886+ res . status ( 500 ) . json ( { error : "Greška" } ) ;
887+ }
888+ } ) ;
889+
890+ // PUT odabir aktivnog prodajnog mjesta
891+ app . put ( '/api/settings/active-location' , requireAuth , async ( req , res ) => {
892+ if ( req . user . role !== "ADMIN" ) return res . status ( 403 ) . json ( { error : "Unauthorized" } ) ;
893+ const { prodajnoMjestoId } = req . body ;
894+ try {
895+ const settings = await prisma . appSettings . upsert ( {
896+ where : { id : 1 } ,
897+ update : { selectedProdajnoMjestoId : prodajnoMjestoId ?? null } ,
898+ create : { id : 1 , selectedProdajnoMjestoId : prodajnoMjestoId ?? null } ,
899+ include : { prodajnoMjesto : true } ,
900+ } ) ;
901+ res . json ( {
902+ selectedProdajnoMjestoId : settings . selectedProdajnoMjestoId ,
903+ prodajnoMjesto : settings . prodajnoMjesto
904+ ? { ...settings . prodajnoMjesto , firaApiKey : "********" }
905+ : null ,
906+ } ) ;
907+ } catch ( error ) {
908+ res . status ( 500 ) . json ( { error : "Greška pri ažuriranju" } ) ;
909+ }
910+ } ) ;
911+
851912// GET all prodajna mjesta
852913app . get ( '/api/prodajna-mjesta' , async ( req , res ) => {
853914 try {
854915 const locations = await prisma . prodajnoMjesto . findMany ( ) ;
855- const safeLocations = locations . map ( loc => ( {
856- ...loc ,
857- firaApiKey : "********" // Don't send the real key back to the UI!
858- } ) ) ;
916+ const safeLocations = locations . map ( loc => {
917+ let maskedKey = "********" ;
918+ try {
919+ const real = decrypt ( loc . firaApiKey ) ;
920+ maskedKey = `****${ real . slice ( - 4 ) } ` ;
921+ } catch { /* leave as ******** if decryption fails */ }
922+ return { ...loc , firaApiKey : maskedKey } ;
923+ } ) ;
859924 res . json ( safeLocations ) ;
860925 } catch ( error ) {
861926 res . status ( 500 ) . json ( { error : "Greška" } ) ;
@@ -882,7 +947,7 @@ app.post('/api/prodajna-mjesta', async (req, res) => {
882947 }
883948 } ) ;
884949
885- res . json ( newLocation ) ;
950+ res . json ( { ... newLocation , firaApiKey : "********" } ) ;
886951 } catch ( error ) {
887952 // THIS LOG IS CRUCIAL: Check your terminal for this output!
888953 console . error ( "CRITICAL BACKEND ERROR:" , error . message ) ;
@@ -899,11 +964,15 @@ app.put('/api/prodajna-mjesta/:id', async (req, res) => {
899964 const { id } = req . params ;
900965 const { name, businessSpace, paymentDevice, firaApiKey, active } = req . body ;
901966 try {
967+ const data = { name, businessSpace, paymentDevice, active } ;
968+ if ( firaApiKey && firaApiKey !== "********" ) {
969+ data . firaApiKey = encrypt ( firaApiKey ) ;
970+ }
902971 const updated = await prisma . prodajnoMjesto . update ( {
903972 where : { id : parseInt ( id ) } ,
904- data : { name , businessSpace , paymentDevice , firaApiKey , active }
973+ data,
905974 } ) ;
906- res . json ( updated ) ;
975+ res . json ( { ... updated , firaApiKey : "********" } ) ;
907976 } catch ( error ) {
908977 res . status ( 500 ) . json ( { error : "Greška pri ažuriranju" } ) ;
909978 }
0 commit comments