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

95.91% Statements 47/49
76.47% Branches 13/17
100% Functions 26/26
100% Lines 46/46

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131  57x 57x   57x   78x 78x                             2x     75x     2x     1x     38x     8x     2x     38x     39x     1x     4x     8x     4x     8x     4x     8x       80x     1x     1x     1x               4x 4x 2x 1x   1x   2x       71x 71x                 37x 37x 37x 37x         36x 36x   7x 7x 7x 7x 7x 7x   36x      
import { MerklePath } from '@bsv/sdk'
import { arraysEqual, entity, sdk, table, verifyId, verifyOneOrNone } from '../../../index.client'
import { EntityBase } from '.'
 
export class OutputBasket extends EntityBase<table.OutputBasket> {
  constructor(api?: table.OutputBasket) {
    const now = new Date()
    super(
      api || {
        basketId: 0,
        created_at: now,
        updated_at: now,
        userId: 0,
        name: '',
        numberOfDesiredUTXOs: 0,
        minimumDesiredUTXOValue: 0,
        isDeleted: false
      }
    )
  }
 
  get basketId() {
    return this.api.basketId
  }
  set basketId(v: number) {
    this.api.basketId = 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 userId() {
    return this.api.userId
  }
  set userId(v: number) {
    this.api.userId = v
  }
  get name() {
    return this.api.name
  }
  set name(v: string) {
    this.api.name = v
  }
  get numberOfDesiredUTXOs() {
    return this.api.numberOfDesiredUTXOs
  }
  set numberOfDesiredUTXOs(v: number) {
    this.api.numberOfDesiredUTXOs = v
  }
  get minimumDesiredUTXOValue() {
    return this.api.minimumDesiredUTXOValue
  }
  set minimumDesiredUTXOValue(v: number) {
    this.api.minimumDesiredUTXOValue = v
  }
  get isDeleted() {
    return this.api.isDeleted
  }
  set isDeleted(v: boolean) {
    this.api.isDeleted = v
  }
 
  override get id() {
    return this.api.basketId
  }
  override set id(v: number) {
    this.api.basketId = v
  }
  override get entityName(): string {
    return 'OutputBasket'
  }
  override get entityTable(): string {
    return 'output_baskets'
  }
 
  override updateApi(): void {
    /* nothing needed yet... */
  }
 
  override equals(ei: table.OutputBasket, syncMap?: entity.SyncMap): boolean {
    const eo = this.api
    if (eo.name != ei.name || eo.numberOfDesiredUTXOs != ei.numberOfDesiredUTXOs || eo.minimumDesiredUTXOValue != ei.minimumDesiredUTXOValue) return false
    if (syncMap) {
      Iif (eo.basketId !== syncMap.outputBasket.idMap[verifyId(ei.basketId)]) return false
    } else {
      Iif (eo.basketId !== ei.basketId || eo.userId !== ei.userId) return false
    }
    return true
  }
 
  static async mergeFind(storage: entity.EntityStorage, userId: number, ei: table.OutputBasket, syncMap: entity.SyncMap, trx?: sdk.TrxToken): Promise<{ found: boolean; eo: OutputBasket; eiId: number }> {
    const ef = verifyOneOrNone(await storage.findOutputBaskets({ partial: { name: ei.name, userId }, trx }))
    return {
      found: !!ef,
      eo: new OutputBasket(ef || { ...ei }),
      eiId: verifyId(ei.basketId)
    }
  }
 
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  override async mergeNew(storage: entity.EntityStorage, userId: number, syncMap: entity.SyncMap, trx?: sdk.TrxToken): Promise<void> {
    this.userId = userId
    this.name ||= 'default'
    this.basketId = 0
    this.basketId = await storage.insertOutputBasket(this.toApi(), trx)
  }
 
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  override async mergeExisting(storage: entity.EntityStorage, since: Date | undefined, ei: table.OutputBasket, syncMap: entity.SyncMap, trx?: sdk.TrxToken): Promise<boolean> {
    let wasMerged = false
    if (ei.updated_at > this.updated_at) {
      // basket name is its identity, should not change
      this.minimumDesiredUTXOValue = ei.minimumDesiredUTXOValue
      this.numberOfDesiredUTXOs = ei.numberOfDesiredUTXOs
      this.isDeleted = ei.isDeleted
      this.updated_at = new Date()
      await storage.updateOutputBasket(this.id, this.toApi(), trx)
      wasMerged = true
    }
    return wasMerged
  }
}