@fabriktor/fx 0.0.87 → 0.0.89

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.
@@ -110,8 +110,17 @@ export type BillableReadResource = (typeof BillableReadResource)[keyof typeof Bi
110
110
  export type BillableReadSearchOptions = SearchOptions & {
111
111
  context: BillableReadContext;
112
112
  owner_ids?: Iterable<string>;
113
+ employer_scope_ids?: Iterable<string>;
113
114
  query?: SearchQuery;
114
115
  };
116
+ export type BillableItemReadSearchOptions = BillableReadSearchOptions & {
117
+ warehouse_id?: string;
118
+ quantifier_id?: string;
119
+ };
120
+ export type BillableEquipmentReadSearchOptions = BillableReadSearchOptions & {
121
+ warehouse_id?: string;
122
+ rental_type?: string;
123
+ };
115
124
  export type GenerateFixedEstimateOptions = {
116
125
  in: GenerateBillPayload;
117
126
  };
@@ -151,11 +160,11 @@ export interface BillableFXOperator {
151
160
  createEquipment(opts: CreateEquipmentOptions): Promise<Equipment>;
152
161
  replaceEquipment(opts: ReplaceEquipmentOptions): Promise<Equipment>;
153
162
  searchEquipments(opts: SearchEquipmentsOptions): Promise<SearchEquipmentsPage>;
154
- searchReadableEquipments(opts: BillableReadSearchOptions): Promise<SearchEquipmentsPage>;
163
+ searchReadableEquipments(opts: BillableEquipmentReadSearchOptions): Promise<SearchEquipmentsPage>;
155
164
  createItem(opts: CreateItemOptions): Promise<Item>;
156
165
  replaceItem(opts: ReplaceItemOptions): Promise<Item>;
157
166
  searchItems(opts: SearchItemsOptions): Promise<SearchItemsPage>;
158
- searchReadableItems(opts: BillableReadSearchOptions): Promise<SearchItemsPage>;
167
+ searchReadableItems(opts: BillableItemReadSearchOptions): Promise<SearchItemsPage>;
159
168
  createLaborRate(opts: CreateLaborRateOptions): Promise<LaborRate>;
160
169
  replaceLaborRate(opts: ReplaceLaborRateOptions): Promise<LaborRate>;
161
170
  searchLaborRates(opts: SearchLaborRatesOptions): Promise<SearchLaborRatesPage>;
@@ -206,11 +215,11 @@ export declare class BillableFX implements BillableFXOperator {
206
215
  createEquipment(opts: CreateEquipmentOptions): Promise<Equipment>;
207
216
  replaceEquipment(opts: ReplaceEquipmentOptions): Promise<Equipment>;
208
217
  searchEquipments(opts: SearchEquipmentsOptions): Promise<SearchEquipmentsPage>;
209
- searchReadableEquipments(opts: BillableReadSearchOptions): Promise<SearchEquipmentsPage>;
218
+ searchReadableEquipments(opts: BillableEquipmentReadSearchOptions): Promise<SearchEquipmentsPage>;
210
219
  createItem(opts: CreateItemOptions): Promise<Item>;
211
220
  replaceItem(opts: ReplaceItemOptions): Promise<Item>;
212
221
  searchItems(opts: SearchItemsOptions): Promise<SearchItemsPage>;
213
- searchReadableItems(opts: BillableReadSearchOptions): Promise<SearchItemsPage>;
222
+ searchReadableItems(opts: BillableItemReadSearchOptions): Promise<SearchItemsPage>;
214
223
  createLaborRate(opts: CreateLaborRateOptions): Promise<LaborRate>;
215
224
  replaceLaborRate(opts: ReplaceLaborRateOptions): Promise<LaborRate>;
216
225
  searchLaborRates(opts: SearchLaborRatesOptions): Promise<SearchLaborRatesPage>;
@@ -244,6 +253,7 @@ export declare class BillableFX implements BillableFXOperator {
244
253
  vendorInvoiceFromBill(opts: VendorInvoiceFromBillOptions): Promise<VendorInvoice>;
245
254
  private generateBill;
246
255
  private checkVendorInvoiceNumber;
256
+ private searchReadableActivity;
247
257
  private searchReadable;
248
258
  private findBill;
249
259
  private findBillableFolder;
@@ -8,7 +8,7 @@ import { BillTypeDynamic, BillTypeFixed, UserTypeEmployee, UserTypeEmployer, } f
8
8
  import { errRequired, errResolveFailed, errValidation } from "../core/err.js";
9
9
  import { requiredOperationID, resolvedID } from "../core/id.js";
10
10
  import { runPreActionCheck } from "../core/pre-action.js";
11
- import { runOwnerScopeSearch, runOwnerSearch, withOwnerQuery, } from "../core/search.js";
11
+ import { runOwnerScopeFilteredSearch, runOwnerScopeSearch, runOwnerSearch, withOwnerQuery, } from "../core/search.js";
12
12
  const resourceBill = "bill";
13
13
  const msgCreatedBillIDMissing = "Created bill response did not contain an ID.";
14
14
  const msgBillMissing = "Bill could not be resolved after the mutation.";
@@ -140,9 +140,8 @@ export class BillableFX {
140
140
  });
141
141
  }
142
142
  async searchReadableCharges(opts) {
143
- return await this.searchReadable({
143
+ return await this.searchReadableActivity({
144
144
  ...opts,
145
- allowed_owner_ids: activityOwnerIDs(opts.context),
146
145
  search: (searchOpts) => this.billable.searchManyCharges(searchOpts),
147
146
  });
148
147
  }
@@ -162,9 +161,14 @@ export class BillableFX {
162
161
  });
163
162
  }
164
163
  async searchReadableEquipments(opts) {
164
+ const { warehouse_id, rental_type, ...search_opts } = opts;
165
165
  return await this.searchReadable({
166
- ...opts,
167
- allowed_owner_ids: catalogOwnerIDs(opts.context),
166
+ ...search_opts,
167
+ allowed_owner_ids: catalogOwnerIDs(opts.context, opts.employer_scope_ids),
168
+ filter: warehouse_id === undefined && rental_type === undefined
169
+ ? undefined
170
+ : (equipment) => (warehouse_id === undefined || equipment.warehouse_id === warehouse_id) &&
171
+ (rental_type === undefined || equipment.rental_type === rental_type),
168
172
  search: (searchOpts) => this.billable.searchManyEquipments(searchOpts),
169
173
  });
170
174
  }
@@ -184,9 +188,15 @@ export class BillableFX {
184
188
  });
185
189
  }
186
190
  async searchReadableItems(opts) {
191
+ const { warehouse_id, quantifier_id, ...search_opts } = opts;
187
192
  return await this.searchReadable({
188
- ...opts,
189
- allowed_owner_ids: catalogOwnerIDs(opts.context),
193
+ ...search_opts,
194
+ allowed_owner_ids: catalogOwnerIDs(opts.context, opts.employer_scope_ids),
195
+ filter: warehouse_id === undefined && quantifier_id === undefined
196
+ ? undefined
197
+ : (item) => (warehouse_id === undefined ||
198
+ item.per_warehouse_quantity.some((quantity) => quantity.warehouse_id === warehouse_id)) &&
199
+ (quantifier_id === undefined || item.quantifier_id === quantifier_id),
190
200
  search: (searchOpts) => this.billable.searchManyItems(searchOpts),
191
201
  });
192
202
  }
@@ -221,9 +231,8 @@ export class BillableFX {
221
231
  });
222
232
  }
223
233
  async searchReadableMeterReadings(opts) {
224
- return await this.searchReadable({
234
+ return await this.searchReadableActivity({
225
235
  ...opts,
226
- allowed_owner_ids: activityOwnerIDs(opts.context),
227
236
  search: (searchOpts) => this.billable.searchManyMeterReadings(searchOpts),
228
237
  });
229
238
  }
@@ -245,7 +254,7 @@ export class BillableFX {
245
254
  async searchReadableQuantifiers(opts) {
246
255
  return await this.searchReadable({
247
256
  ...opts,
248
- allowed_owner_ids: catalogOwnerIDs(opts.context),
257
+ allowed_owner_ids: catalogOwnerIDs(opts.context, opts.employer_scope_ids),
249
258
  search: (searchOpts) => this.billable.searchManyQuantifiers(searchOpts),
250
259
  });
251
260
  }
@@ -326,7 +335,7 @@ export class BillableFX {
326
335
  async searchReadableWarehouses(opts) {
327
336
  return await this.searchReadable({
328
337
  ...opts,
329
- allowed_owner_ids: catalogOwnerIDs(opts.context),
338
+ allowed_owner_ids: catalogOwnerIDs(opts.context, opts.employer_scope_ids),
330
339
  search: (searchOpts) => this.billable.searchManyWarehouses(searchOpts),
331
340
  });
332
341
  }
@@ -388,15 +397,34 @@ export class BillableFX {
388
397
  },
389
398
  });
390
399
  }
400
+ async searchReadableActivity(opts) {
401
+ const employer_scope_ids = activityEmployerScopeIDs(opts);
402
+ return await this.searchReadable({
403
+ ...opts,
404
+ allowed_owner_ids: activityOwnerIDs(opts.context),
405
+ filter: employer_scope_ids === undefined
406
+ ? undefined
407
+ : (value) => employer_scope_ids.has(value.employer_id),
408
+ });
409
+ }
391
410
  async searchReadable(opts) {
392
- const { context: _context, owner_ids: requested_owner_ids, allowed_owner_ids, search, query, ...searchOpts } = opts;
411
+ const { context: _context, owner_ids: requested_owner_ids, employer_scope_ids: _employer_scope_ids, allowed_owner_ids, search, filter, query, ...searchOpts } = opts;
393
412
  const owner_ids = restrictOwnerIDs(allowed_owner_ids, requested_owner_ids);
394
- const page = await runOwnerScopeSearch({
395
- ...searchOpts,
396
- owner_ids,
397
- query,
398
- search,
399
- });
413
+ const page = filter === undefined
414
+ ? await runOwnerScopeSearch({
415
+ ...searchOpts,
416
+ owner_ids,
417
+ query,
418
+ search,
419
+ })
420
+ : await runOwnerScopeFilteredSearch({
421
+ ...searchOpts,
422
+ owner_ids,
423
+ query,
424
+ search,
425
+ filter,
426
+ compare: compareCreatedAtDescending,
427
+ });
400
428
  return {
401
429
  ...page,
402
430
  results: [...page.results].sort(compareCreatedAtDescending),
@@ -587,12 +615,22 @@ const employeeReadableResources = new Set([
587
615
  BillableReadResource.Quantifier,
588
616
  BillableReadResource.MeterReading,
589
617
  ]);
590
- function catalogOwnerIDs(context) {
618
+ function catalogOwnerIDs(context, employer_scope_ids) {
591
619
  if (context.active_mode === UserTypeEmployee) {
592
- return uniqueIDs(context.employment.active_employer_ids);
620
+ return restrictOwnerIDs(context.employment.active_employer_ids, employer_scope_ids);
593
621
  }
594
622
  return uniqueIDs([context.owner_id]);
595
623
  }
624
+ function activityEmployerScopeIDs(opts) {
625
+ if (opts.context.active_mode === UserTypeEmployer) {
626
+ return new Set([opts.context.owner_id]);
627
+ }
628
+ if (opts.context.active_mode !== UserTypeEmployee ||
629
+ opts.employer_scope_ids === undefined) {
630
+ return undefined;
631
+ }
632
+ return new Set(restrictOwnerIDs(opts.context.employment.active_employer_ids, opts.employer_scope_ids));
633
+ }
596
634
  function activityOwnerIDs(context) {
597
635
  if (context.active_mode === UserTypeEmployer) {
598
636
  return uniqueIDs([context.owner_id, ...context.employment.related_employee_ids]);
@@ -1,5 +1,5 @@
1
1
  import type { ContactsOperator, UsersOperator } from "@fabriktor/client";
2
- import { Friend, Hired, Manager, Na, OldHire, type Contact, type ID, type OperationResult } from "@fabriktor/schema";
2
+ import { Friend, Hired, Manager, Na, OldHire, type Contact, type ID, type OperationResult, type User } from "@fabriktor/schema";
3
3
  type ContactInsertOptions = Parameters<ContactsOperator["insertOneContact"]>[0];
4
4
  type ContactReplaceOptions = Parameters<ContactsOperator["replaceOneContact"]>[0];
5
5
  export type CreateContactOptions = ContactInsertOptions;
@@ -20,6 +20,7 @@ export type EmploymentOwnerScopeOptions = {
20
20
  };
21
21
  export type EmploymentOwnerScope = {
22
22
  active_employer_ids: string[];
23
+ active_employers: User[];
23
24
  current_employee_ids: string[];
24
25
  related_employee_ids: string[];
25
26
  };
@@ -112,6 +112,9 @@ export class ContactsFX {
112
112
  }
113
113
  return {
114
114
  active_employer_ids: [...active_employer_ids],
115
+ active_employers: [...active_employer_ids]
116
+ .map((id) => users_by_id.get(id))
117
+ .filter((user) => user !== undefined),
115
118
  current_employee_ids: [...current_employee_ids],
116
119
  related_employee_ids: [...related_employee_ids],
117
120
  };
@@ -281,6 +284,7 @@ export function newContactsFX(opts) {
281
284
  export function emptyEmploymentOwnerScope() {
282
285
  return {
283
286
  active_employer_ids: [],
287
+ active_employers: [],
284
288
  current_employee_ids: [],
285
289
  related_employee_ids: [],
286
290
  };
@@ -27,6 +27,10 @@ export type OwnerScopeSearchOptions<T> = SearchOptions & {
27
27
  search: SearchCall<T>;
28
28
  query?: SearchQuery;
29
29
  };
30
+ export type OwnerScopeFilteredSearchOptions<T> = OwnerScopeSearchOptions<T> & {
31
+ filter: (value: T) => boolean | Promise<boolean>;
32
+ compare?: (left: T, right: T) => number;
33
+ };
30
34
  export type SearchPage<T> = {
31
35
  results: T[];
32
36
  page: number;
@@ -42,6 +46,9 @@ export declare function runOwnerSearch<T>(opts: RunOwnerSearchOptions<T>): Promi
42
46
  export declare function runOwnerScopeSearch<T extends {
43
47
  id: string;
44
48
  }>(opts: OwnerScopeSearchOptions<T>): Promise<SearchPage<T>>;
49
+ export declare function runOwnerScopeFilteredSearch<T extends {
50
+ id: string;
51
+ }>(opts: OwnerScopeFilteredSearchOptions<T>): Promise<SearchPage<T>>;
45
52
  export declare function runSearch<T>(opts: RunSearchOptions<T>): Promise<SearchPage<T>>;
46
53
  export declare function normalizeSearchPage(value?: number): number;
47
54
  export declare function normalizeSearchPerPage(value?: number): number;
@@ -54,6 +54,66 @@ export async function runOwnerScopeSearch(opts) {
54
54
  done: !has_more,
55
55
  };
56
56
  }
57
+ // runOwnerScopeFilteredSearch scans each real owner independently, applies a
58
+ // client-side relationship filter, then paginates the merged result. This is
59
+ // intentionally heavier than runOwnerScopeSearch and should only be used when
60
+ // the backend model cannot express the requested relationship as a query rule.
61
+ export async function runOwnerScopeFilteredSearch(opts) {
62
+ const owner_ids = uniqueOwnerIDs(opts.owner_ids);
63
+ const page = normalizeSearchPage(opts.page);
64
+ const per_page = normalizeSearchPerPage(opts.per_page);
65
+ if (owner_ids.length === 0) {
66
+ return emptySearchPage(page, per_page);
67
+ }
68
+ const results = new Map();
69
+ const done = new Set();
70
+ let source_page = 1;
71
+ while (done.size < owner_ids.length) {
72
+ const active_owner_ids = owner_ids.filter((owner_id) => !done.has(owner_id));
73
+ const pages = await Promise.all(active_owner_ids.map(async (owner_id) => ({
74
+ owner_id,
75
+ page: await runOwnerSearch({
76
+ owner_id,
77
+ q: opts.q,
78
+ page: source_page,
79
+ per_page,
80
+ query: opts.query,
81
+ search: opts.search,
82
+ }),
83
+ })));
84
+ for (const owner_page of pages) {
85
+ if (owner_page.page.done) {
86
+ done.add(owner_page.owner_id);
87
+ }
88
+ for (const value of owner_page.page.results) {
89
+ if (await opts.filter(value)) {
90
+ results.set(value.id, value);
91
+ }
92
+ }
93
+ }
94
+ source_page += 1;
95
+ }
96
+ const merged = [...results.values()];
97
+ if (opts.compare !== undefined) {
98
+ merged.sort(opts.compare);
99
+ }
100
+ const start = (page - 1) * per_page;
101
+ const end = start + per_page;
102
+ const page_results = merged.slice(start, end);
103
+ const total_count = merged.length;
104
+ const last_page = Math.max(1, Math.ceil(total_count / per_page));
105
+ const has_more = page < last_page;
106
+ return {
107
+ results: page_results,
108
+ page,
109
+ per_page,
110
+ total_count,
111
+ last_page,
112
+ has_more,
113
+ next_page: has_more ? page + 1 : page,
114
+ done: !has_more,
115
+ };
116
+ }
57
117
  export async function runSearch(opts) {
58
118
  const page = normalizeSearchPage(opts.page);
59
119
  const per_page = normalizeSearchPerPage(opts.per_page);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.87",
3
+ "version": "0.0.89",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,7 +17,7 @@
17
17
  "typescript": "^6.0.3"
18
18
  },
19
19
  "dependencies": {
20
- "@fabriktor/client": "0.0.73",
21
- "@fabriktor/schema": "0.0.56"
20
+ "@fabriktor/client": "0.0.74",
21
+ "@fabriktor/schema": "0.0.57"
22
22
  }
23
23
  }