facturas 0.12.0 → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3035,11 +3035,10 @@ async function runOperation(wsfe, operation, input, prepare, options, context, r
3035
3035
  return "blocked" in barrier ? barrier.blocked : await claim(barrier.reserved);
3036
3036
  }
3037
3037
  );
3038
- async function claim(reserved) {
3039
- const number = options.number ?? reserved ?? await nextNumber(wsfe, prepared.data, options);
3038
+ function reservation(number) {
3040
3039
  const service = options.service ?? "wsfe";
3041
3040
  const versioned = service === "wsmtxca" || prepared.data.details !== void 0;
3042
- const record = {
3041
+ return {
3043
3042
  v: versioned ? 2 : 1,
3044
3043
  operation,
3045
3044
  ...versioned ? { service } : {},
@@ -3051,19 +3050,25 @@ async function runOperation(wsfe, operation, input, prepare, options, context, r
3051
3050
  sent: prepared.data,
3052
3051
  createdAt: (/* @__PURE__ */ new Date()).toISOString()
3053
3052
  };
3053
+ }
3054
+ async function claim(reserved) {
3055
+ const coordinated = store.withLock !== void 0;
3056
+ const prior = coordinated ? await storeCall(() => store.get(key)) : null;
3057
+ if (prior !== null) {
3058
+ return await replay(prior, true);
3059
+ }
3060
+ const number = options.number ?? reserved ?? await nextNumber(wsfe, prepared.data, options);
3061
+ const record = reservation(number);
3062
+ const claimed = {
3063
+ v: 1,
3064
+ key: idempotencyKey,
3065
+ number,
3066
+ claimedAt: (/* @__PURE__ */ new Date()).toISOString()
3067
+ };
3068
+ if (coordinated) {
3069
+ await storeCall(() => store.set(sequenceRecord, JSON.stringify(claimed)));
3070
+ }
3054
3071
  if (await storeCall(() => store.add(key, JSON.stringify(record)))) {
3055
- const claimed = {
3056
- v: 1,
3057
- key: idempotencyKey,
3058
- number,
3059
- claimedAt: (/* @__PURE__ */ new Date()).toISOString()
3060
- };
3061
- const coordinated = store.withLock !== void 0;
3062
- if (coordinated) {
3063
- await storeCall(
3064
- () => store.set(sequenceRecord, JSON.stringify(claimed))
3065
- );
3066
- }
3067
3072
  const outcome = await settle(
3068
3073
  runAuthorization(wsfe, prepared, options, number)
3069
3074
  );
@@ -3103,7 +3108,8 @@ async function runOperation(wsfe, operation, input, prepare, options, context, r
3103
3108
  select ?? (() => wsfe),
3104
3109
  stored,
3105
3110
  readSettledRecord(recorded),
3106
- options
3111
+ options,
3112
+ (other) => storeCall(() => store.get(settledKey(environment, taxId, other)))
3107
3113
  );
3108
3114
  }
3109
3115
  return await settle(
@@ -3166,24 +3172,40 @@ function readSettledRecord(json) {
3166
3172
  );
3167
3173
  }
3168
3174
  }
3169
- async function settledOutcome(select, reservation, settled, options) {
3175
+ async function settledOutcome(select, reservation, settled, options, settledFor) {
3170
3176
  if (settled.kind === "conflict") {
3171
3177
  return settledConflict(settled, reservation);
3172
3178
  }
3173
3179
  const outcome = await consultReservation(select, reservation, options, true);
3174
- if (outcome.kind === "indeterminate" && outcome.lookup.kind === "not_found") {
3175
- return {
3176
- kind: "indeterminate",
3177
- attempted: {
3178
- salesPoint: reservation.salesPoint,
3179
- voucherType: reservation.voucherType,
3180
- number: settled.number
3181
- },
3182
- attempt: replayEvidence(reservation.service),
3183
- lookup: { kind: "superseded", by: settled.by }
3184
- };
3180
+ const empty = outcome.kind === "indeterminate" && outcome.lookup.kind === "not_found";
3181
+ if (!empty && (outcome.kind !== "conflict" || await successionDisputed(settled.by, settledFor))) {
3182
+ return outcome;
3185
3183
  }
3186
- return outcome;
3184
+ return {
3185
+ kind: "indeterminate",
3186
+ attempted: {
3187
+ salesPoint: reservation.salesPoint,
3188
+ voucherType: reservation.voucherType,
3189
+ number: settled.number
3190
+ },
3191
+ attempt: replayEvidence(reservation.service),
3192
+ lookup: { kind: "superseded", by: settled.by }
3193
+ };
3194
+ }
3195
+ async function successionDisputed(key, settledFor) {
3196
+ let next = key;
3197
+ for (let hop = 0; hop < 16; hop += 1) {
3198
+ const json = await settledFor(next);
3199
+ if (json === null) {
3200
+ return false;
3201
+ }
3202
+ const record = readSettledRecord(json);
3203
+ if (record.kind === "conflict") {
3204
+ return true;
3205
+ }
3206
+ next = record.by;
3207
+ }
3208
+ return true;
3187
3209
  }
3188
3210
  function settledConflict(settled, reservation) {
3189
3211
  return {
@@ -3764,7 +3786,16 @@ async function recoverOperation(select, key, inputOptions, context) {
3764
3786
  select,
3765
3787
  record,
3766
3788
  readSettledRecord(recorded),
3767
- options
3789
+ options,
3790
+ (other) => storeCall(
3791
+ () => store.get(
3792
+ settledKey(
3793
+ context.environment,
3794
+ context.taxId,
3795
+ other
3796
+ )
3797
+ )
3798
+ )
3768
3799
  );
3769
3800
  }
3770
3801
  return await recordConflict(
@@ -3839,10 +3870,11 @@ async function runSequenceBarrier({
3839
3870
  if (reservation === null) {
3840
3871
  return {};
3841
3872
  }
3873
+ const record = readRecord(reservation);
3842
3874
  const blocked = {
3843
3875
  blocked: {
3844
3876
  kind: "indeterminate",
3845
- attempted: { ...coordinates, number: claimed.number },
3877
+ attempted: { ...coordinates, number: record.number },
3846
3878
  attempt: replayEvidence(options.service),
3847
3879
  lookup: { kind: "blocked", by: claimed.key }
3848
3880
  }
@@ -3850,7 +3882,7 @@ async function runSequenceBarrier({
3850
3882
  const outcome = await recordConflict(
3851
3883
  store,
3852
3884
  settled,
3853
- await consultReservation(select, readRecord(reservation), {
3885
+ await consultReservation(select, record, {
3854
3886
  forceRefresh: options.forceRefresh,
3855
3887
  ...options.signal === void 0 ? {} : { signal: options.signal }
3856
3888
  })
@@ -3862,7 +3894,7 @@ async function runSequenceBarrier({
3862
3894
  return blocked;
3863
3895
  }
3864
3896
  const next = await readNext();
3865
- if (next !== claimed.number) {
3897
+ if (next !== record.number) {
3866
3898
  return blocked;
3867
3899
  }
3868
3900
  await storeCall(
@@ -3871,7 +3903,7 @@ async function runSequenceBarrier({
3871
3903
  JSON.stringify({
3872
3904
  v: 1,
3873
3905
  kind: "superseded",
3874
- number: claimed.number,
3906
+ number: record.number,
3875
3907
  by: supersededBy,
3876
3908
  settledAt: (/* @__PURE__ */ new Date()).toISOString()
3877
3909
  })
@@ -4240,4 +4272,4 @@ export {
4240
4272
  withLease,
4241
4273
  createFileStore
4242
4274
  };
4243
- //# sourceMappingURL=chunk-QEJCLJDS.mjs.map
4275
+ //# sourceMappingURL=chunk-BP72XNPS.mjs.map