Back

Appwrite Functions

  • 0
  • Functions
rimesbeat
14 Feb, 2024, 09:15

I want to write an appwrite function that will update my isDue and isOverdue when repaymentDate is the current date and overdue when repayment date is overdue. export default async ({ req, res, log, error }) => {

if (req.method === 'PUT') { const updateDueInAppwrite = async (loanId) => { try{ const response = await db.updateDocument(FASTDB, LOANDATA, loanId, { isDue: true, });

TypeScript
    console.log('Document updated successfully', response);
  } catch (error) {
    console.error('Error updating document:', error);
  }
};
const updateDaysOverdueInAppwrite = async (loanId, days) => {

  try {
  
    const response = await db.updateDocument(
     FASTDB, LOANDATA,
      loanId,
      {
        isOverdue: true,
        daysOverdue: days,
      }
    );

    console.log('Document updated successfully', response);
    log();
  } catch (error) {
    console.error('Error updating document:', error);
    error()
  }
};
const checkLoanStatus = async () => {
  response.documents.forEach(async (loan) => {
    const currentDate = new Date();
    const repaymentDate = new Date(loan.repaymentDate) 
    if (repaymentDate < currentDate) {
      const differenceInTime =
        currentDate.getTime() - repaymentDate.getTime();
      const differenceInDays = Math.ceil(
        differenceInTime / (1000 * 3600 * 24)
      )      updateDaysOverdueInAppwrite(
        loan.$id.toString(),
        differenceInDays
      );
    } else if (repaymentDate.getTime() === currentDate.getTime()) {
      // If repayment date is equal to current date
      console.log(`Loan ID: ${loan.$id} is due today`);

log(Loan ID: ${loan.$id} is due today);

TypeScript
      await updateDueInAppwrite(loan.$id.toString());
    }
  });
}; 
checkLoanStatus();

} return res.empty(); };

TL;DR
Developers are having trouble getting their appwrite function to update the 'isDue' and 'isOverdue' values in their app. They have written a function that is supposed to update these values based on the repayment date, but it is not working as expected. It is recommended to check the following: - Ensure that the appwrite function is being called correctly and that the 'loanId' parameter is being passed correctly to the update functions. - Double-check the 'updateDocument' method in the appwrite SDK to make sure it is being used correctly. - Verify that the 'repaymentDate' and 'currentDate'
rimesbeat
14 Feb, 2024, 09:16

But this function is not updating my isDue and isOverdue in my appwrite

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more