@@ -491,6 +491,7 @@ app.post("/api/receipts", requireAuth, async (req, res) => {
491491 discountValue,
492492 shippingCost,
493493 prodajnoMjestoNaziv : appSettings . prodajnoMjesto . name ,
494+ prodajnoMjestoId : appSettings . prodajnoMjesto . id ,
494495 items : {
495496 create : items . map ( ( item ) => ( {
496497 name : item . name ,
@@ -621,6 +622,8 @@ app.put("/api/receipts/:id/storno", requireAuth, async (req, res) => {
621622 internalNote : `STORNO of ${ originalReceipt . invoiceNumber } ` ,
622623 discountValue : originalReceipt . discountValue ,
623624 shippingCost : originalReceipt . shippingCost ,
625+ prodajnoMjestoNaziv : originalReceipt . prodajnoMjestoNaziv ,
626+ prodajnoMjestoId : originalReceipt . prodajnoMjestoId ,
624627 items : {
625628 create : originalReceipt . items . map ( ( item ) => ( {
626629 name : item . name ,
@@ -646,6 +649,21 @@ app.put("/api/receipts/:id/storno", requireAuth, async (req, res) => {
646649 } ,
647650 } ) ;
648651
652+ // Dohvati prodajno mjesto originalnog računa
653+ const originalProdajnoMjesto = await prisma . prodajnoMjesto . findUnique ( {
654+ where : { id : originalReceipt . prodajnoMjestoId } ,
655+ } ) ;
656+ if ( ! originalProdajnoMjesto ) {
657+ return res . status ( 400 ) . json ( { error : "Prodajno mjesto originalnog računa nije pronađeno." } ) ;
658+ }
659+ let firaApiKey ;
660+ try {
661+ firaApiKey = decrypt ( originalProdajnoMjesto . firaApiKey ) ;
662+ if ( ! firaApiKey ) throw new Error ( "Prazan ključ" ) ;
663+ } catch {
664+ return res . status ( 500 ) . json ( { error : "Greška pri dešifriranju API ključa prodajnog mjesta." } ) ;
665+ }
666+
649667 // Fiscalize storno receipt via FIRA
650668 const firaResult = await handleOrderFiscalization ( {
651669 id : stornoReceipt . id ,
@@ -655,7 +673,7 @@ app.put("/api/receipts/:id/storno", requireAuth, async (req, res) => {
655673 currency : stornoReceipt . currency ,
656674 paymentType : stornoReceipt . paymentType ,
657675 items : stornoReceipt . items ,
658- } ) ;
676+ } , { firaApiKey , prodajnoMjestoNaziv : originalProdajnoMjesto . name } ) ;
659677
660678 if ( firaResult && firaResult . invoiceNumber ) {
661679 await prisma . receipt . update ( {
@@ -931,20 +949,19 @@ app.get('/api/prodajna-mjesta', requireAuth, async (req, res) => {
931949// POST new prodajno mjesto
932950app . post ( '/api/prodajna-mjesta' , requireAuth , async ( req , res ) => {
933951 try {
934- const { name, businessSpace, paymentDevice, firaApiKey, active } = req . body ;
952+ const { name, businessSpace, paymentDevice, firaApiKey } = req . body ;
935953
936954 // Log for debugging
937955 console . log ( "Attempting to encrypt key..." ) ;
938956 const encryptedKey = encrypt ( firaApiKey ) ;
939-
957+
940958 console . log ( "Attempting to save to database..." ) ;
941959 const newLocation = await prisma . prodajnoMjesto . create ( {
942- data : {
943- name,
944- businessSpace,
945- paymentDevice,
946- firaApiKey : encryptedKey ,
947- active
960+ data : {
961+ name,
962+ businessSpace,
963+ paymentDevice,
964+ firaApiKey : encryptedKey
948965 }
949966 } ) ;
950967
@@ -963,9 +980,9 @@ app.post('/api/prodajna-mjesta', requireAuth, async (req, res) => {
963980// PUT (update) prodajno mjesto
964981app . put ( '/api/prodajna-mjesta/:id' , requireAuth , async ( req , res ) => {
965982 const { id } = req . params ;
966- const { name, businessSpace, paymentDevice, firaApiKey, active } = req . body ;
983+ const { name, businessSpace, paymentDevice, firaApiKey } = req . body ;
967984 try {
968- const data = { name, businessSpace, paymentDevice, active } ;
985+ const data = { name, businessSpace, paymentDevice } ;
969986 if ( firaApiKey && firaApiKey !== "********" ) {
970987 data . firaApiKey = encrypt ( firaApiKey ) ;
971988 }
0 commit comments