Skip to content

Commit

Permalink
refactor(eb-app-api): transaction in room-partner-candidacy
Browse files Browse the repository at this point in the history
  • Loading branch information
emrahcom committed Mar 25, 2024
1 parent 857b578 commit 0c93f31
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fetch, query } from "./common.ts";
import { fetch, pool } from "./common.ts";
import type { Id, RoomPartnerCandidacy } from "./types.ts";

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -65,6 +65,10 @@ export async function acceptRoomPartnerCandidacy(
identityId: string,
candidacyId: string,
) {
using client = await pool.connect();
const trans = client.createTransaction("transaction");
await trans.begin();

const sql = {
text: `
INSERT INTO room_partner (identity_id, room_id)
Expand All @@ -82,7 +86,7 @@ export async function acceptRoomPartnerCandidacy(
candidacyId,
],
};
const rows = await fetch(sql) as Id[];
const { rows: rows } = await trans.queryObject(sql);

// add partner to the contact list if not exists
const sql1 = {
Expand Down Expand Up @@ -110,7 +114,7 @@ export async function acceptRoomPartnerCandidacy(
candidacyId,
],
};
if (rows[0] !== undefined) await query(sql1);
await trans.queryObject(sql1);

// add room owner to the partner's contact list if not exists
const sql2 = {
Expand Down Expand Up @@ -145,7 +149,7 @@ export async function acceptRoomPartnerCandidacy(
candidacyId,
],
};
if (rows[0] !== undefined) await query(sql2);
await trans.queryObject(sql2);

// remove the room-partner candidancy if the add action is successful
const sql3 = {
Expand All @@ -158,9 +162,11 @@ export async function acceptRoomPartnerCandidacy(
candidacyId,
],
};
if (rows[0] !== undefined) await query(sql3);
await trans.queryObject(sql3);

await trans.commit();

return rows;
return rows as Id[];
}

// -----------------------------------------------------------------------------
Expand Down

0 comments on commit 0c93f31

Please sign in to comment.