Ahmadullah Ahmadi
Ahmadullah Ahmadi

Reputation: 1

Prisma Transaction sometime not update all properties

A function is declare in the next.js 14 server action when I call this function with the verified="approve" ,id=1, data={payload:"bankAccount",id:1} it does not update the bank account while all thing working and updating other tables, while it is in transaction why this happening, what is the problem may answer it.

export const verifiedPartnerDepositionPayment = createServerAction(

  async ({ id, verified, data }) => {

    let VerifiedPartnerDeposition;

    const session = await auth();

    let actions = {};

    if (verified === "approved") {

      actions = {

        ppwd_approval: { connect: { u_id: Number(session?.user?.id) } },
      };
    }

    if (verified === "verified") {

      actions = {

        ppwd_verifier: { connect: { u_id: Number(session?.user?.id) } },
      };
    }

    if (verified !== "approved" && !!!data) {

      VerifiedPartnerDeposition =
        await prisma.payment_partner_withdrawals_deposition.update({
          where: { ppwd_id: id * 1 },
          data: { ppwd_status: verified, ...actions },
          include: { ppwd_partner_with: { select: { pwd_gnt_id: true } } },
        });

      if (verified === "verified" && session?.user?.role !== "admin") {

        const adnotfiy = await AddNotification({
          resourceTitle: "Partner Deposition Payment",
          action: verified,
          user: session,
          resource: VerifiedPartnerDeposition.ppwd_partner_with?.pwd_gnt_id,
          department: "fi",
          link: "fi/partner_fund",
        });
      }
    } else {

      const payment = await prisma.$transaction(async (tx) => {

        // Find Partner Deposition
        const findPartnerDepositionPayment =
          await tx.payment_partner_withdrawals_deposition.findUnique({
            where: { ppwd_id: id * 1 },
            include: {
              ppwd_partner_with: {
                select: {
                  pwd_gnt_id: true,
                  pwd_id: true,
                },
              },
              // sm_project: { select: { pj_id: true, pj_gnt_id: true } },
            },
          });

        if (!findPartnerDepositionPayment)

          throw new ServerActionError("Partner Deposition Payment not found");

        const partnerDeposit =
          await tx.partners_withdrawals_deposition.findUnique({
            where: {
              pwd_id: findPartnerDepositionPayment.ppwd_partner_with?.pwd_id,
            },
            select: {
              pwd_payment: { select: { ppwd_amount: true, ppwd_status: true } },
              pwd_gnt_id: true,
            },
          });

        const totalDeposit = partnerDeposit?.pwd_payment
          .filter((el) => el.ppwd_status === "approved")
          .reduce((prev, cur) => prev + cur.ppwd_amount, 0);

        if (totalDeposit === undefined)
          throw new ServerActionError(
            "Partner Deposition Amount Left not found"
          );

        // PAYMENT FOR HQ OR MAIN OFFICE

        if (data.payload === "bankAccount") {
          // Find Bank account Type
          const findAccount = await tx.bank_accounts.findUnique({
            where: { ba_id: data.id },
          });

          if (!findAccount)
            throw new ServerActionError("Bank Account not found");

          // Check for ‌Budget Type amount
          if (findAccount.ba_amount < data.pcl_amount) {
            throw new ServerActionError(
              Bank Account No:${findAccount.ba_account_number?.toUpperCase()} doesn't have enough amount
          }

          const updateBankAccount = await tx.bank_accounts.update({
            where: { ba_id: findAccount.ba_id },
            data: {
              ba_amount: {
                increment: findPartnerDepositionPayment.ppwd_amount,
              },
            },
          });

          // Create withdraw from bank withdrawal

          const lastRow = await prisma.bank_infos.findFirst({


            select: {


              bi_id: true,


            },


            orderBy: {


              bi_id: "desc",


            },


          });





          const gnt_id = generateModernId(


            ${systemId}-D-,


            lastRow ? lastRow.bi_id + 1 : 1


          );


          const addWithdrawal = await tx.bank_infos.create({


            data: {


              bi_gnt_id: gnt_id,


              bi_amount: findPartnerDepositionPayment.ppwd_amount,


              bi_type: "deposit",


              bi_description: Partner Deposition payment (${partnerDeposit?.pwd_gnt_id?.toUpperCase()}),


              bi_bank: { connect: { ba_id: findAccount.ba_id } },


              bi_open_balance: updateBankAccount.ba_amount,


              bi_user: { connect: { u_id: Number(session?.user?.id) } },


            },


          });


          // Create Partner Deposition Logs


  const PartnerDepositionLogs =


            await tx.payment_partner_withdrawals_deposition.update({


              data: {


                ppwd_status: verified,


                ppwd_bank_acount: { connect: { ba_id: findAccount.ba_id } },


                ...actions,


              },


              where: { ppwd_id: findPartnerDepositionPayment.ppwd_id },


              include: {


                ppwd_partner_with: { select: { pwd_gnt_id: true } },


              },


            });


          if (verified === "approved" && session?.user?.role !== "admin") {


            const adnotfiy = await AddNotification({


              resourceTitle: "Partner Deposition Payment",


              action: verified,


              user: session,


              resource: PartnerDepositionLogs.ppwd_partner_with?.pwd_gnt_id,


              department: "fi",


              link: "fi/partner_fund",


            });


          }


          return { addWithdrawal };


        }


      });


      return { payment };


    }


    revalidatePath("/dashboard/fi/loan_fund");


    return { VerifiedPartnerDeposition };


  }


);

Upvotes: 0

Views: 18

Answers (0)

Related Questions