<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Obinna Justice</title>
    <description>The latest articles on DEV Community by Obinna Justice (@obinna_justice).</description>
    <link>https://dev.to/obinna_justice</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4078339%2F26ddcd06-799e-4a6b-8c08-d0e28ce3a260.jpg</url>
      <title>DEV Community: Obinna Justice</title>
      <link>https://dev.to/obinna_justice</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/obinna_justice"/>
    <language>en</language>
    <item>
      <title>Node.js function that handles a wallet funding. Notice the atomic transaction</title>
      <dc:creator>Obinna Justice</dc:creator>
      <pubDate>Sun, 16 Aug 2026 11:54:18 +0000</pubDate>
      <link>https://dev.to/obinna_justice/nodejs-function-that-handles-a-wallet-funding-notice-the-atomic-transaction-4mm4</link>
      <guid>https://dev.to/obinna_justice/nodejs-function-that-handles-a-wallet-funding-notice-the-atomic-transaction-4mm4</guid>
      <description>&lt;p&gt;`&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;`async function fundWallet(userId: string, amount: Decimal, reference: string) {&lt;/p&gt;

&lt;p&gt;return await prisma.$transaction(async (tx) =&amp;gt; {&lt;/p&gt;

&lt;p&gt;// 1. Check for duplicate&lt;/p&gt;

&lt;p&gt;const existing = await tx.transaction.findUnique({ where: { reference } });&lt;/p&gt;

&lt;p&gt;if (existing) return existing;&lt;/p&gt;

&lt;p&gt;// 2. Lock wallet row (prevents race conditions)&lt;/p&gt;

&lt;p&gt;const wallet = await tx.wallet.findUnique({&lt;/p&gt;

&lt;p&gt;where: { userId },&lt;/p&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;if (!wallet || wallet.status !== 'ACTIVE') {&lt;/p&gt;

&lt;p&gt;throw new Error('Wallet not available');&lt;/p&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;// 3. Create transaction record first&lt;/p&gt;

&lt;p&gt;const newBalance = wallet.balance.plus(amount);&lt;/p&gt;

&lt;p&gt;const transaction = await tx.transaction.create({&lt;/p&gt;

&lt;p&gt;data: {&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;walletId: wallet.id,

type: 'CREDIT',

amount,

balanceAfter: newBalance,

reference,

status: 'COMPLETED',

description: 'Wallet funding via Paystack',
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;},&lt;/p&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;// 4. Update wallet balance&lt;/p&gt;

&lt;p&gt;await tx.wallet.update({&lt;/p&gt;

&lt;p&gt;where: { id: wallet.id },&lt;/p&gt;

&lt;p&gt;data: { balance: newBalance },&lt;/p&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;return transaction;&lt;/p&gt;

&lt;p&gt;});&lt;/p&gt;

&lt;p&gt;}&lt;code&gt;&lt;/code&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
