Skip to content

Commit d696255

Browse files
committed
refactor(deno-kv): Reorganize transaction begin and validation checks
1 parent ffd1205 commit d696255

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

config/deno-kv.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ export async function listenQueue(kv: Deno.Kv) {
120120
const client_1 = await dbPool.connect();
121121

122122
try {
123+
transaction = client_1.createTransaction("create-listing");
124+
125+
await transaction.begin();
126+
123127
if (!msg.data?.dataLayer) {
124128
throw new Error("DataLayer is missing or undefined");
125129
}
@@ -133,6 +137,27 @@ export async function listenQueue(kv: Deno.Kv) {
133137
);
134138
}
135139

140+
if (!msg.data.dataLayer.agent_name) {
141+
throw new Error("Agent name is missing or undefined");
142+
}
143+
144+
if (!msg.data.dataLayer.product_owner) {
145+
throw new Error("Product owner is missing or undefined");
146+
}
147+
148+
if (!msg.data.dataLayer.product_owner_name) {
149+
throw new Error("Product owner name is missing or undefined");
150+
}
151+
152+
if (
153+
!msg.data.dataLayer.location ||
154+
typeof msg.data.dataLayer.location !== "object"
155+
) {
156+
throw new Error(
157+
"Location is missing, undefined, or not an object"
158+
);
159+
}
160+
136161
let propertyId;
137162
let warehouseTypeId;
138163
const listingUrl = msg.data.listingUrl;
@@ -147,9 +172,6 @@ export async function listenQueue(kv: Deno.Kv) {
147172
const isLand =
148173
msg.data.dataLayer.attributes.subcategory === "Land";
149174

150-
transaction = client_1.createTransaction("create-listing");
151-
await transaction.begin();
152-
153175
const listingRecord = await transaction.queryObject(`
154176
SELECT id, property_id
155177
FROM Listing
@@ -203,27 +225,6 @@ export async function listenQueue(kv: Deno.Kv) {
203225
return;
204226
}
205227

206-
if (!msg.data.dataLayer.agent_name) {
207-
throw new Error("Agent name is missing or undefined");
208-
}
209-
210-
if (!msg.data.dataLayer.product_owner) {
211-
throw new Error("Product owner is missing or undefined");
212-
}
213-
214-
if (!msg.data.dataLayer.product_owner_name) {
215-
throw new Error("Product owner name is missing or undefined");
216-
}
217-
218-
if (
219-
!msg.data.dataLayer.location ||
220-
typeof msg.data.dataLayer.location !== "object"
221-
) {
222-
throw new Error(
223-
"Location is missing, undefined, or not an object"
224-
);
225-
}
226-
227228
if (isCondominium) {
228229
propertyId = 1;
229230
}

0 commit comments

Comments
 (0)