sap-tutorials / Tutorials

Tutorials on sap.com
https://developers.sap.com/tutorial-navigator.html
Creative Commons Attribution 4.0 International
720 stars 773 forks source link

Create a Full-Stack SAP Fiori Application with Joule in SAP Build Code #23535

Closed aryanpanc111 closed 5 months ago

aryanpanc111 commented 5 months ago

Tutorials: https://developers.sap.com/tutorials/build-code-test-drive.html

Thanks for a very good Tutorial. Sometime Redemption code which is auto generated by Joule is not working. Below is the working code, I have written, tested and working.

-----Application logic modified to correction module.exports = async function(req) { const { redeemedAmount, customer_ID } = req.data; const tx = cds.transaction(req);

const [{ totalRewardPoints, totalRedeemedRewardPoints } ] = await tx.run(
   SELECT.from('loyaltyProgramSrv.Customers')
      .where({ID: customer_ID } )
      .columns('totalRewardPoints', 'totalRedeemedRewardPoints')
      );

// const customerData = await SELECT.one(['totalRewardPoints', 'totalRedeemedRewardPoints']).from('loyaltyProgramSrv.Customers').where({ ID: req.data.customer_ID });

if (totalRewardPoints < redeemedAmount) {
    req.error(400, 'Insufficient reward points');
    return;
}

const updatedRewardPoints = totalRewardPoints - redeemedAmount;
const updatedRedeemedPoints = totalRedeemedRewardPoints + redeemedAmount;

await UPDATE('loyaltyProgramSrv.Customers')
    .set({
        totalRewardPoints: updatedRewardPoints,
        totalRedeemedRewardPoints: updatedRedeemedPoints
    })
    .where({ ID: customer_ID });

}

aryanpanc111 commented 5 months ago

Sometime Redemption code which is auto generated by Joule is not working. Below is the working code, I have written, tested and working.

-----Application logic modified to correction module.exports = async function(req) { const { redeemedAmount, customer_ID } = req.data; const tx = cds.transaction(req);

const [{ totalRewardPoints, totalRedeemedRewardPoints } ] = await tx.run( SELECT.from('loyaltyProgramSrv.Customers') .where({ID: customer_ID } ) .columns('totalRewardPoints', 'totalRedeemedRewardPoints') ); // const customerData = await SELECT.one(['totalRewardPoints', 'totalRedeemedRewardPoints']).from('loyaltyProgramSrv.Customers').where({ ID: req.data.customer_ID });

if (totalRewardPoints < redeemedAmount) { req.error(400, 'Insufficient reward points'); return; }

const updatedRewardPoints = totalRewardPoints - redeemedAmount; const updatedRedeemedPoints = totalRedeemedRewardPoints + redeemedAmount;

await UPDATE('loyaltyProgramSrv.Customers') .set({ totalRewardPoints: updatedRewardPoints, totalRedeemedRewardPoints: updatedRedeemedPoints }) .where({ ID: customer_ID }); }