commit e625c0526fbbf71dbe7c9ea999017a1fb5e03f4f
parent 7a3cbf6e5272d8a97a2112528df0291bbd7f6ae2
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Sat, 7 Sep 2024 10:02:08 +0000
client: update iclient db, add `trade_offer` model
Diffstat:
1 file changed, 132 insertions(+), 1 deletion(-)
diff --git a/client/src/capacitor/sql.ts b/client/src/capacitor/sql.ts
@@ -1,6 +1,6 @@
import { Capacitor } from '@capacitor/core';
import { CapacitorSQLite, SQLiteConnection, SQLiteDBConnection, type DBSQLiteValues, type capSQLiteChanges, type capSQLiteUpgradeOptions, type capSQLiteVersionUpgrade } from '@radroots/capacitor-sqlite';
-import { LocationGcsSchema, NostrNoteSchema, TradeProductSchema, location_gcs_sort, models_initial_upgrade, nostr_note_sort, parse_location_gcs_form_field_types, parse_location_gcss, parse_nostr_note_form_field_types, parse_nostr_notes, parse_trade_product_form_field_types, parse_trade_products, trade_product_sort, type ILocationGcsGet, type ILocationGcsGetList, type ILocationGcsQueryBindValues, type ILocationGcsQueryBindValuesTuple, type ILocationGcsUpdate, type IModelsQueryBindValueOpt, type IModelsQueryBindValueTuple, type IModelsQueryParam, type INostrNoteGet, type INostrNoteGetList, type INostrNoteQueryBindValues, type INostrNoteQueryBindValuesTuple, type INostrNoteUpdate, type ITradeProductGet, type ITradeProductGetList, type ITradeProductQueryBindValues, type ITradeProductQueryBindValuesTuple, type ITradeProductUpdate, type LocationGcs, type LocationGcsFields, type LocationGcsFormFields, type NostrNote, type NostrNoteFields, type NostrNoteFormFields, type TradeProduct, type TradeProductFields, type TradeProductFormFields } from "@radroots/models";
+import { LocationGcsSchema, NostrNoteSchema, TradeOfferSchema, TradeProductSchema, location_gcs_sort, models_initial_upgrade, nostr_note_sort, parse_location_gcs_form_field_types, parse_location_gcss, parse_nostr_note_form_field_types, parse_nostr_notes, parse_trade_offer_form_field_types, parse_trade_offers, parse_trade_product_form_field_types, parse_trade_products, trade_offer_sort, trade_product_sort, type ILocationGcsGet, type ILocationGcsGetList, type ILocationGcsQueryBindValues, type ILocationGcsQueryBindValuesTuple, type ILocationGcsUpdate, type IModelsQueryBindValueOpt, type IModelsQueryBindValueTuple, type IModelsQueryParam, type INostrNoteGet, type INostrNoteGetList, type INostrNoteQueryBindValues, type INostrNoteQueryBindValuesTuple, type INostrNoteUpdate, type ITradeOfferGet, type ITradeOfferGetList, type ITradeOfferQueryBindValues, type ITradeOfferQueryBindValuesTuple, type ITradeOfferUpdate, type ITradeProductGet, type ITradeProductGetList, type ITradeProductQueryBindValues, type ITradeProductQueryBindValuesTuple, type ITradeProductUpdate, type LocationGcs, type LocationGcsFields, type LocationGcsFormFields, type NostrNote, type NostrNoteFields, type NostrNoteFormFields, type TradeOffer, type TradeOfferFields, type TradeOfferFormFields, type TradeProduct, type TradeProductFields, type TradeProductFormFields } from "@radroots/models";
import { err_msg, time_created_on, uuidv4 } from '@radroots/utils';
const models_upgrades = [
@@ -25,6 +25,10 @@ export type IISQLiteServiceMessage =
| "*-trade-product-key-unique"
| "*-trade-product-lot-unique"
| "*-trade-product-varietal-unique"
+ | "*-trade-offer-quantity-amt-unique"
+ | "*-trade-offer-quantity-unit-unique"
+ | "*-trade-offer-price-amt-unique"
+ | "*-trade-offer-price-currency-unique"
| "*-nostr-note-ev-id-unique"
| "*-validate"
| "*-result"
@@ -190,6 +194,10 @@ export class CapacitorClientSQLite {
else if (String(e).includes("UNIQUE constraint failed: trade_product.key")) return "*-trade-product-key-unique";
else if (String(e).includes("UNIQUE constraint failed: trade_product.lot")) return "*-trade-product-lot-unique";
else if (String(e).includes("UNIQUE constraint failed: trade_product.varietal")) return "*-trade-product-varietal-unique";
+ else if (String(e).includes("UNIQUE constraint failed: trade_offer.quantity_amt")) return "*-trade-offer-quantity-amt-unique";
+ else if (String(e).includes("UNIQUE constraint failed: trade_offer.quantity_unit")) return "*-trade-offer-quantity-unit-unique";
+ else if (String(e).includes("UNIQUE constraint failed: trade_offer.price_amt")) return "*-trade-offer-price-amt-unique";
+ else if (String(e).includes("UNIQUE constraint failed: trade_offer.price_currency")) return "*-trade-offer-price-currency-unique";
else if (String(e).includes("UNIQUE constraint failed: nostr_note.ev_id")) return "*-nostr-note-ev-id-unique";
return this.append_logs("*-exe", bv_o, query, error);
};
@@ -487,6 +495,129 @@ export class CapacitorClientSQLite {
};
};
+ private trade_offer_add_validate(opts: TradeOfferFormFields): TradeOfferFields | string[] {
+ const opts_filtered = Object.entries(opts).reduce((acc: Record<string, (string | number)>, [key, value]) => {
+ if (!!value) {
+ switch (parse_trade_offer_form_field_types(key)) {
+ case "string":
+ acc[key] = value;
+ break;
+ case "number":
+ acc[key] = Number(value);
+ break;
+ }
+ };
+ return acc;
+ }, {});
+ const trade_offer_v = TradeOfferSchema.safeParse(opts_filtered);
+ if (!trade_offer_v.success) return trade_offer_v.error.issues.map(i => i.message);
+ else return {
+ ...trade_offer_v.data,
+ };
+ };
+
+ public async trade_offer_add(opts: TradeOfferFormFields): Promise<{ id: string; } | string[] | IISQLiteServiceMessage> {
+ const optsv = this.trade_offer_add_validate(opts);
+ if (Array.isArray(optsv)) return optsv;
+ const fields = Object.entries(optsv);
+ if (!fields.length) return "*-fields";
+ const id = uuidv4();
+ const bind_values_tup: IModelsQueryBindValueTuple[] = [
+ ["id", id],
+ ["created_at", time_created_on()]
+ ];
+ for (const field of this.filter_bind_value_fields(fields)) bind_values_tup.push(field);
+ const bind_values = bind_values_tup.map(([_, v]) => v);
+ const query = `INSERT INTO trade_offer (${bind_values_tup.map(([k]) => k).join(", ")}) VALUES (${bind_values_tup.map((_, num) => `$${1 + num}`).join(", ")});`;
+ try {
+ const result = await this.execute(query, bind_values);
+ if (typeof result !== "string" && typeof result.changes?.changes === "number" && result.changes.changes > 0) return { id };
+ else if (typeof result === "string") return result;
+ return "*-result";
+ } catch (e) {
+ return this.append_logs("*", bind_values, query, ["trade_offer_add", e]);
+ };
+ };
+
+ private trade_offer_query_bind_values = (opts: ITradeOfferQueryBindValues): ITradeOfferQueryBindValuesTuple => {
+ return ["id", opts.id];
+ };
+
+ private trade_offer_get_query_list = (opts: ITradeOfferGetList): IModelsQueryParam => {
+ const sort = trade_offer_sort[opts.sort || "newest"];
+ let query = "";
+ let bind_values = null;
+ if (opts.list[0] === "all") {
+ query = `SELECT * FROM trade_offer ORDER BY ${sort};`;
+ } else if (opts.list[0] === "unique") {
+ query = `SELECT * FROM trade_offer WHERE quantity_amt = $1 AND quantity_unit = $2 AND price_amt = $3 AND price_currency = $4;`;
+ bind_values = [opts.list[1].quantity_amt, opts.list[1].quantity_unit, opts.list[1].price_amt, opts.list[1].price_currency];
+ }
+ if (!query) throw new Error("Error: Missing query (trade_offer_get_query_list)")
+ return {
+ query,
+ bind_values
+ };
+ };
+
+ private trade_offer_get_parse_opts = (opts: ITradeOfferGet): IModelsQueryParam => {
+ if ("list" in opts) return this.trade_offer_get_query_list(opts);
+ else {
+ const bv_tup = this.trade_offer_query_bind_values(opts);
+ return {
+ query: `SELECT * FROM trade_offer WHERE ${bv_tup[0]} = $1;`,
+ bind_values: [bv_tup[1]]
+ };
+ };
+ };
+
+ public async trade_offer_get(opts: ITradeOfferGet): Promise<TradeOffer[] | IISQLiteServiceMessage> {
+ const { query, bind_values } = this.trade_offer_get_parse_opts(opts);
+ try {
+ const response = await this.select(query, bind_values);
+ if (typeof response === "string") return response;
+ else {
+ const result = parse_trade_offers(response);
+ if (result) return result;
+ }
+ return "*-result";
+ } catch (e) {
+ return this.append_logs("*", opts, query, ["trade_offer_get", e]);
+ };
+ };
+
+ public async trade_offer_delete(opts: ITradeOfferQueryBindValues): Promise<true | IISQLiteServiceMessage> {
+ const bv_tup = this.trade_offer_query_bind_values(opts);
+ const bind_values = [bv_tup[1]];
+ const query = `DELETE FROM trade_offer WHERE ${bv_tup[0]} = $1;`;
+ try {
+ const response = await this.execute(query, bind_values);
+ if (typeof response === "string") return response;
+ else if (typeof response.changes?.changes === "number" && response.changes.changes > 0) return true;
+ return "*-result";
+ } catch (e) {
+ return this.append_logs("*", [], query, ["trade_offer_delete", e]);
+ };
+ };
+
+ public async trade_offer_update(opts: ITradeOfferUpdate): Promise<true | string[] | IISQLiteServiceMessage> {
+ const optsv = this.trade_offer_add_validate(opts.fields);
+ if (Array.isArray(optsv)) return optsv;
+ const fields = this.filter_bind_value_fields(Object.entries(optsv));
+ if (!fields.length) return "*-fields";
+ const bv_tup = this.trade_offer_query_bind_values(opts.on);
+ const bind_values = [bv_tup[1], ...fields.map(([_, v]) => v)];
+ const query = `UPDATE trade_offer SET ${fields.map(([k], num) => `${k} = $${1 + num}`).join(", ")} WHERE ${bv_tup[0]} = $1;`;
+ try {
+ const response = await this.execute(query, bind_values);
+ if (typeof response === "string") return response;
+ else if (typeof response.changes?.changes === "number" && response.changes.changes > 0) return true;
+ return "*-result";
+ } catch (e) {
+ return this.append_logs("*", [], query, ["trade_offer_update", e]);
+ };
+ };
+
private nostr_note_add_validate(opts: NostrNoteFormFields): NostrNoteFields | string[] {
const opts_filtered = Object.entries(opts).reduce((acc: Record<string, (string | number)>, [key, value]) => {
if (!!value) {