@bsv/wallet-toolbox-client
Version:
Client only Wallet Storage
98 lines • 3.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityTxLabelMap = void 0;
const utilityHelpers_1 = require("../../../utility/utilityHelpers");
const EntityBase_1 = require("./EntityBase");
const WERR_errors_1 = require("../../../sdk/WERR_errors");
class EntityTxLabelMap extends EntityBase_1.EntityBase {
constructor(api) {
const now = new Date();
super(api || {
created_at: now,
updated_at: now,
transactionId: 0,
txLabelId: 0,
isDeleted: false
});
}
updateApi() {
/* nothing needed yet... */
}
get txLabelId() {
return this.api.txLabelId;
}
set txLabelId(v) {
this.api.txLabelId = v;
}
get transactionId() {
return this.api.transactionId;
}
set transactionId(v) {
this.api.transactionId = v;
}
get created_at() {
return this.api.created_at;
}
set created_at(v) {
this.api.created_at = v;
}
get updated_at() {
return this.api.updated_at;
}
set updated_at(v) {
this.api.updated_at = v;
}
get isDeleted() {
return this.api.isDeleted;
}
set isDeleted(v) {
this.api.isDeleted = v;
}
get id() {
throw new WERR_errors_1.WERR_INVALID_OPERATION('entity has no "id" value');
} // entity does not have its own id.
get entityName() {
return 'txLabelMap';
}
get entityTable() {
return 'tx_labels_map';
}
equals(ei, syncMap) {
const eo = this.toApi();
if (eo.transactionId !== (syncMap ? syncMap.transaction.idMap[(0, utilityHelpers_1.verifyId)(ei.transactionId)] : ei.transactionId) ||
eo.txLabelId !== (syncMap ? syncMap.txLabel.idMap[(0, utilityHelpers_1.verifyId)(ei.txLabelId)] : ei.txLabelId) ||
eo.isDeleted !== ei.isDeleted)
return false;
return true;
}
static async mergeFind(storage, userId, ei, syncMap, trx) {
const transactionId = syncMap.transaction.idMap[ei.transactionId];
const txLabelId = syncMap.txLabel.idMap[ei.txLabelId];
const ef = (0, utilityHelpers_1.verifyOneOrNone)(await storage.findTxLabelMaps({
partial: { transactionId, txLabelId },
trx
}));
return {
found: !!ef,
eo: new EntityTxLabelMap(ef || { ...ei }),
eiId: -1
};
}
async mergeNew(storage, userId, syncMap, trx) {
this.transactionId = syncMap.transaction.idMap[this.transactionId];
this.txLabelId = syncMap.txLabel.idMap[this.txLabelId];
await storage.insertTxLabelMap(this.toApi(), trx);
}
async mergeExisting(storage, since, ei, syncMap, trx) {
let wasMerged = false;
if (ei.updated_at > this.updated_at) {
this.isDeleted = ei.isDeleted;
this.updated_at = new Date(Math.max(ei.updated_at.getTime(), this.updated_at.getTime()));
await storage.updateTxLabelMap(this.transactionId, this.txLabelId, this.toApi(), trx);
wasMerged = true;
}
return wasMerged;
}
}
exports.EntityTxLabelMap = EntityTxLabelMap;
//# sourceMappingURL=EntityTxLabelMap.js.map