All files / src/storage/schema/entities Commission.ts

100% Statements 45/45
92.85% Branches 13/14
100% Functions 28/28
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91    57x 57x   57x   956x 956x                                 2x 1135x 2x 1x 382x 2x 1146x 568x 2x 568x 16x 2x 10x 1x 8x 1x 6x 1x   948x 1x 1x 1x     12x             10x   2x         945x 945x 945x               567x 567x 567x 567x       380x 380x 1x 1x 1x 1x   380x    
/* eslint-disable @typescript-eslint/no-unused-vars */
import { MerklePath } from "@bsv/sdk"
import { arraysEqual, entity, sdk, table, verifyId, verifyOneOrNone } from "../../../index.client";
import { EntityBase } from ".";
 
export class Commission extends EntityBase<table.Commission> {
    constructor(api?: table.Commission) {
        const now = new Date()
        super(api || {
            commissionId: 0,
            created_at: now,
            updated_at: now,
            transactionId: 0,
            userId: 0,
            isRedeemed: false,
            keyOffset: "",
            lockingScript: [],
            satoshis: 0
        })
    }
 
    override updateApi(): void {
        /* nothing needed yet... */
    }
 
    get commissionId() { return this.api.commissionId }
    set commissionId(v: number) { this.api.commissionId = v }
    get created_at() { return this.api.created_at }
    set created_at(v: Date) { this.api.created_at = v }
    get updated_at() { return this.api.updated_at }
    set updated_at(v: Date) { this.api.updated_at = v }
    get transactionId() { return this.api.transactionId }
    set transactionId(v: number) { this.api.transactionId = v }
    get userId() { return this.api.userId }
    set userId(v: number) { this.api.userId = v }
    get isRedeemed() { return this.api.isRedeemed }
    set isRedeemed(v: boolean) { this.api.isRedeemed = v }
    get keyOffset() { return this.api.keyOffset }
    set keyOffset(v: string) { this.api.keyOffset = v }
    get lockingScript() { return this.api.lockingScript }
    set lockingScript(v: number[]) { this.api.lockingScript = v }
    get satoshis() { return this.api.satoshis }
    set satoshis(v: number) { this.api.satoshis = v }
 
    override get id(): number { return this.api.commissionId }
    override set id(v: number) { this.api.commissionId = v }
    override get entityName(): string { return 'Commission' }
    override get entityTable(): string { return 'commissions' }
    
    override equals(ei: table.Commission, syncMap?: entity.SyncMap | undefined): boolean {
        if (
            this.isRedeemed !== ei.isRedeemed ||
            this.transactionId !== (syncMap ? syncMap.transaction.idMap[ei.transactionId] : ei.transactionId) ||
            this.keyOffset !== ei.keyOffset ||
            !arraysEqual(this.lockingScript, ei.lockingScript) ||
            this.satoshis !== ei.satoshis
        )
            return false
 
        return true
    }
 
    static async mergeFind(storage: entity.EntityStorage, userId: number, ei: table.Commission, syncMap: entity.SyncMap, trx?: sdk.TrxToken)
    : Promise<{ found: boolean, eo: entity.Commission, eiId: number }> {
        const transactionId = syncMap.transaction.idMap[ei.transactionId]
        const ef = verifyOneOrNone(await storage.findCommissions({ partial: { transactionId, userId }, trx }))
        return {
            found: !!ef,
            eo: new entity.Commission(ef || { ...ei }),
            eiId: verifyId(ei.commissionId)
        }
    }
 
    override async mergeNew(storage: entity.EntityStorage, userId: number, syncMap: entity.SyncMap, trx?: sdk.TrxToken): Promise<void> {
        if (this.transactionId) this.transactionId = syncMap.transaction.idMap[this.transactionId]
        this.userId = userId
        this.commissionId = 0
        this.commissionId = await storage.insertCommission(this.toApi(), trx)
    }
 
    override async mergeExisting(storage: entity.EntityStorage, since: Date | undefined, ei: table.Commission, syncMap: entity.SyncMap, trx?: sdk.TrxToken): Promise<boolean> {
        let wasMerged = false
        if (ei.updated_at > this.updated_at) {
            this.isRedeemed = ei.isRedeemed
            this.updated_at = new Date()
            await storage.updateCommission(this.id, this.toApi(), trx)
            wasMerged = true
        }
        return wasMerged
    }
}