@fabriktor/fx 0.0.202 → 0.0.204

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.
@@ -49,7 +49,7 @@ export declare function errValidation<T extends object = Record<string, unknown>
49
49
  export declare function errResolveFailed<T extends object = Record<string, unknown>>(opts: ResolveFailedFXErrOptions<T>): FXErr;
50
50
  export declare function errUnknown(cause: unknown): FXErr;
51
51
  export declare function isFXErr(err: unknown): err is FXErr;
52
- export declare function errUnconfirmedMutation(): FXErr;
52
+ export declare function errUnconfirmedMutation(cause?: unknown): FXErr;
53
53
  export declare function isUnconfirmedMutation(err: unknown): boolean;
54
54
  export declare function fxErrMessage(err: unknown): string;
55
55
  export {};
@@ -74,10 +74,11 @@ export function errUnknown(cause) {
74
74
  export function isFXErr(err) {
75
75
  return err instanceof FXErr;
76
76
  }
77
- export function errUnconfirmedMutation() {
77
+ export function errUnconfirmedMutation(cause) {
78
78
  return errResolveFailed({
79
79
  resource: resourceUnconfirmedMutation,
80
80
  msg: "The previous mutation was not acknowledged. Repeating it could duplicate the operation.",
81
+ cause,
81
82
  });
82
83
  }
83
84
  export function isUnconfirmedMutation(err) {
@@ -1,10 +1,20 @@
1
- import type { FulfillmentsOperator } from "@fabriktor/client";
2
- import type { FulfillOrder } from "@fabriktor/schema";
1
+ import { type FulfillmentsOperator } from "@fabriktor/client";
2
+ import { type FulfillOrder, type User } from "@fabriktor/schema";
3
3
  import { type OwnerSearchOptions, type SearchPage } from "../core/search.js";
4
4
  type FulfillOrderInsertOptions = Parameters<FulfillmentsOperator["insertOneFulfillOrder"]>[0];
5
- type FulfillOrderReplaceOptions = Parameters<FulfillmentsOperator["replaceOneFulfillOrder"]>[0];
6
- export type CreateFulfillOrderOptions = FulfillOrderInsertOptions;
7
- export type ReplaceFulfillOrderOptions = FulfillOrderReplaceOptions;
5
+ export type CreateFulfillOrderOptions = FulfillOrderInsertOptions & {
6
+ user: Pick<User, "id" | "account_identity">;
7
+ };
8
+ export type FulfillOrderAllowanceOptions = {
9
+ owner_id: string;
10
+ signal?: AbortSignal;
11
+ };
12
+ export type FulfillOrderAllowance = {
13
+ used: number;
14
+ remaining: number;
15
+ };
16
+ export declare function canOrderFreeMagnets(user: Pick<User, "account_identity"> | null | undefined): boolean;
17
+ export declare function isFulfillOrderAllowanceExceeded(error: unknown): boolean;
8
18
  export type SearchFulfillOrdersOptions = OwnerSearchOptions;
9
19
  export type SearchFulfillOrdersPage = SearchPage<FulfillOrder>;
10
20
  export type FulfillmentsFXOptions = {
@@ -12,14 +22,14 @@ export type FulfillmentsFXOptions = {
12
22
  };
13
23
  export interface FulfillmentsFXOperator {
14
24
  createFulfillOrder(opts: CreateFulfillOrderOptions): Promise<FulfillOrder>;
15
- replaceFulfillOrder(opts: ReplaceFulfillOrderOptions): Promise<FulfillOrder>;
25
+ getFulfillOrderAllowance(opts: FulfillOrderAllowanceOptions): Promise<FulfillOrderAllowance>;
16
26
  searchFulfillOrders(opts: SearchFulfillOrdersOptions): Promise<SearchFulfillOrdersPage>;
17
27
  }
18
28
  export declare class FulfillmentsFX implements FulfillmentsFXOperator {
19
29
  private fulfillments;
20
30
  constructor(opts: FulfillmentsFXOptions);
21
31
  createFulfillOrder(opts: CreateFulfillOrderOptions): Promise<FulfillOrder>;
22
- replaceFulfillOrder(opts: ReplaceFulfillOrderOptions): Promise<FulfillOrder>;
32
+ getFulfillOrderAllowance(opts: FulfillOrderAllowanceOptions): Promise<FulfillOrderAllowance>;
23
33
  searchFulfillOrders(opts: SearchFulfillOrdersOptions): Promise<SearchFulfillOrdersPage>;
24
34
  private findFulfillOrder;
25
35
  }
@@ -1,22 +1,97 @@
1
- import { errResolveFailed } from "../core/err.js";
1
+ import { ClientErrKind, isClientErr, isHTTPClientErr, } from "@fabriktor/client";
2
+ import { ErrCodeFulfillOrderAllowanceExceeded, FulfillOrderQuantityLimit, FulfillOrderQuantityMin, FulfillOrderStatusFailed, FulfillProductNameCarMagnet5x5, UserTypeEmployer, } from "@fabriktor/schema";
3
+ import { errResolveFailed, errValidation, errUnconfirmedMutation, isFXErr, } from "../core/err.js";
2
4
  import { requiredOperationID } from "../core/id.js";
5
+ import { withOwnerQuery } from "../core/query.js";
6
+ import { userSupportsMode } from "../users/users.js";
3
7
  import { runOwnerSearch, } from "../core/search.js";
4
8
  const resourceFulfillOrder = "fulfill_order";
5
9
  const msgCreatedFulfillOrderIDMissing = "Created fulfill order response did not contain an ID.";
6
10
  const msgFulfillOrderMissing = "Fulfill order could not be resolved after the mutation.";
11
+ export function canOrderFreeMagnets(user) {
12
+ return userSupportsMode(user, UserTypeEmployer);
13
+ }
14
+ export function isFulfillOrderAllowanceExceeded(error) {
15
+ return (((isHTTPClientErr(error) && error.status === 409) || isFXErr(error)) &&
16
+ error.message === ErrCodeFulfillOrderAllowanceExceeded);
17
+ }
7
18
  export class FulfillmentsFX {
8
19
  fulfillments;
9
20
  constructor(opts) {
10
21
  this.fulfillments = opts.fulfillments;
11
22
  }
12
23
  async createFulfillOrder(opts) {
13
- const out = await this.fulfillments.insertOneFulfillOrder(opts);
14
- const id = requiredOperationID(out, msgCreatedFulfillOrderIDMissing);
15
- return await this.findFulfillOrder(id);
24
+ const { user, ...insertOpts } = opts;
25
+ if (!canOrderFreeMagnets(user)) {
26
+ throw errValidation({
27
+ field: "account_identity",
28
+ msg: "Free magnets require an employer identity.",
29
+ });
30
+ }
31
+ if (!Number.isInteger(opts.in.quantity) ||
32
+ opts.in.quantity < FulfillOrderQuantityMin ||
33
+ opts.in.quantity > FulfillOrderQuantityLimit) {
34
+ throw errValidation({
35
+ field: "quantity",
36
+ msg: "Invalid magnet quantity.",
37
+ });
38
+ }
39
+ const allowance = await this.getFulfillOrderAllowance({
40
+ owner_id: user.id,
41
+ signal: opts.signal,
42
+ });
43
+ if (opts.in.quantity > allowance.remaining) {
44
+ throw errValidation({
45
+ field: "quantity",
46
+ msg: ErrCodeFulfillOrderAllowanceExceeded,
47
+ });
48
+ }
49
+ let out;
50
+ try {
51
+ out = await this.fulfillments.insertOneFulfillOrder(insertOpts);
52
+ }
53
+ catch (error) {
54
+ if (isClientErr(error) &&
55
+ (error.kind === ClientErrKind.HTTPRequestFailed ||
56
+ error.kind === ClientErrKind.HTTPResponseInvalid ||
57
+ (error.status !== undefined && error.status >= 500)))
58
+ throw errUnconfirmedMutation(error);
59
+ throw error;
60
+ }
61
+ try {
62
+ const id = requiredOperationID(out, msgCreatedFulfillOrderIDMissing);
63
+ return await this.findFulfillOrder(id, opts.signal);
64
+ }
65
+ catch (error) {
66
+ throw errUnconfirmedMutation(error);
67
+ }
16
68
  }
17
- async replaceFulfillOrder(opts) {
18
- await this.fulfillments.replaceOneFulfillOrder(opts);
19
- return await this.findFulfillOrder(opts.id);
69
+ async getFulfillOrderAllowance(opts) {
70
+ if (!opts.owner_id.trim()) {
71
+ throw errValidation({
72
+ field: "owner_id",
73
+ msg: "An owner is required to check the magnet allowance.",
74
+ });
75
+ }
76
+ // Query returns the complete retained history; search pages cannot determine the allowance.
77
+ const out = await this.fulfillments.queryFulfillOrders({
78
+ query: withOwnerQuery(opts.owner_id),
79
+ signal: opts.signal,
80
+ });
81
+ if (!Array.isArray(out.value)) {
82
+ throw errResolveFailed({
83
+ resource: resourceFulfillOrder,
84
+ msg: "Magnet allowance could not be resolved.",
85
+ });
86
+ }
87
+ const used = out.value.reduce((total, order) => {
88
+ if (order.owner_id !== opts.owner_id ||
89
+ order.product_name !== FulfillProductNameCarMagnet5x5 ||
90
+ order.status === FulfillOrderStatusFailed)
91
+ return total;
92
+ return total + order.quantity;
93
+ }, 0);
94
+ return { used, remaining: Math.max(0, FulfillOrderQuantityLimit - used) };
20
95
  }
21
96
  async searchFulfillOrders(opts) {
22
97
  return await runOwnerSearch({
@@ -24,9 +99,10 @@ export class FulfillmentsFX {
24
99
  search: (searchOpts) => this.fulfillments.searchManyFulfillOrders(searchOpts),
25
100
  });
26
101
  }
27
- async findFulfillOrder(id) {
102
+ async findFulfillOrder(id, signal) {
28
103
  const out = await this.fulfillments.findOneFulfillOrder({
29
104
  id,
105
+ signal,
30
106
  });
31
107
  const fulfill_order = out.value;
32
108
  if (fulfill_order === undefined) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.202",
3
+ "version": "0.0.204",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,9 +17,9 @@
17
17
  "typescript": "^6.0.3"
18
18
  },
19
19
  "dependencies": {
20
- "@fabriktor/client": "0.0.121",
20
+ "@fabriktor/client": "0.0.123",
21
21
  "@fabriktor/fx": "0.0.94",
22
- "@fabriktor/schema": "0.0.91",
22
+ "@fabriktor/schema": "0.0.93",
23
23
  "convert": "8.0.2"
24
24
  }
25
25
  }