@fabriktor/fx 0.0.87 → 0.0.88

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>;
@@ -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,13 @@ export class BillableFX {
140
140
  });
141
141
  }
142
142
  async searchReadableCharges(opts) {
143
+ const employer_scope_ids = activityEmployerScopeIDs(opts);
143
144
  return await this.searchReadable({
144
145
  ...opts,
145
146
  allowed_owner_ids: activityOwnerIDs(opts.context),
147
+ filter: employer_scope_ids === undefined
148
+ ? undefined
149
+ : (charge) => employer_scope_ids.has(charge.employer_id),
146
150
  search: (searchOpts) => this.billable.searchManyCharges(searchOpts),
147
151
  });
148
152
  }
@@ -162,9 +166,14 @@ export class BillableFX {
162
166
  });
163
167
  }
164
168
  async searchReadableEquipments(opts) {
169
+ const { warehouse_id, rental_type, ...search_opts } = opts;
165
170
  return await this.searchReadable({
166
- ...opts,
167
- allowed_owner_ids: catalogOwnerIDs(opts.context),
171
+ ...search_opts,
172
+ allowed_owner_ids: catalogOwnerIDs(opts.context, opts.employer_scope_ids),
173
+ filter: warehouse_id === undefined && rental_type === undefined
174
+ ? undefined
175
+ : (equipment) => (warehouse_id === undefined || equipment.warehouse_id === warehouse_id) &&
176
+ (rental_type === undefined || equipment.rental_type === rental_type),
168
177
  search: (searchOpts) => this.billable.searchManyEquipments(searchOpts),
169
178
  });
170
179
  }
@@ -184,9 +193,15 @@ export class BillableFX {
184
193
  });
185
194
  }
186
195
  async searchReadableItems(opts) {
196
+ const { warehouse_id, quantifier_id, ...search_opts } = opts;
187
197
  return await this.searchReadable({
188
- ...opts,
189
- allowed_owner_ids: catalogOwnerIDs(opts.context),
198
+ ...search_opts,
199
+ allowed_owner_ids: catalogOwnerIDs(opts.context, opts.employer_scope_ids),
200
+ filter: warehouse_id === undefined && quantifier_id === undefined
201
+ ? undefined
202
+ : (item) => (warehouse_id === undefined ||
203
+ item.per_warehouse_quantity.some((quantity) => quantity.warehouse_id === warehouse_id)) &&
204
+ (quantifier_id === undefined || item.quantifier_id === quantifier_id),
190
205
  search: (searchOpts) => this.billable.searchManyItems(searchOpts),
191
206
  });
192
207
  }
@@ -221,9 +236,21 @@ export class BillableFX {
221
236
  });
222
237
  }
223
238
  async searchReadableMeterReadings(opts) {
239
+ const employer_scope_ids = activityEmployerScopeIDs(opts);
240
+ const equipment_owners = new Map();
224
241
  return await this.searchReadable({
225
242
  ...opts,
226
243
  allowed_owner_ids: activityOwnerIDs(opts.context),
244
+ filter: employer_scope_ids === undefined
245
+ ? undefined
246
+ : async (reading) => {
247
+ let owner = equipment_owners.get(reading.equipment_id);
248
+ if (owner === undefined) {
249
+ owner = this.findEquipment(reading.equipment_id).then((equipment) => equipment.owner_id);
250
+ equipment_owners.set(reading.equipment_id, owner);
251
+ }
252
+ return employer_scope_ids.has(await owner);
253
+ },
227
254
  search: (searchOpts) => this.billable.searchManyMeterReadings(searchOpts),
228
255
  });
229
256
  }
@@ -245,7 +272,7 @@ export class BillableFX {
245
272
  async searchReadableQuantifiers(opts) {
246
273
  return await this.searchReadable({
247
274
  ...opts,
248
- allowed_owner_ids: catalogOwnerIDs(opts.context),
275
+ allowed_owner_ids: catalogOwnerIDs(opts.context, opts.employer_scope_ids),
249
276
  search: (searchOpts) => this.billable.searchManyQuantifiers(searchOpts),
250
277
  });
251
278
  }
@@ -326,7 +353,7 @@ export class BillableFX {
326
353
  async searchReadableWarehouses(opts) {
327
354
  return await this.searchReadable({
328
355
  ...opts,
329
- allowed_owner_ids: catalogOwnerIDs(opts.context),
356
+ allowed_owner_ids: catalogOwnerIDs(opts.context, opts.employer_scope_ids),
330
357
  search: (searchOpts) => this.billable.searchManyWarehouses(searchOpts),
331
358
  });
332
359
  }
@@ -389,14 +416,23 @@ export class BillableFX {
389
416
  });
390
417
  }
391
418
  async searchReadable(opts) {
392
- const { context: _context, owner_ids: requested_owner_ids, allowed_owner_ids, search, query, ...searchOpts } = opts;
419
+ const { context: _context, owner_ids: requested_owner_ids, employer_scope_ids: _employer_scope_ids, allowed_owner_ids, search, filter, query, ...searchOpts } = opts;
393
420
  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
- });
421
+ const page = filter === undefined
422
+ ? await runOwnerScopeSearch({
423
+ ...searchOpts,
424
+ owner_ids,
425
+ query,
426
+ search,
427
+ })
428
+ : await runOwnerScopeFilteredSearch({
429
+ ...searchOpts,
430
+ owner_ids,
431
+ query,
432
+ search,
433
+ filter,
434
+ compare: compareCreatedAtDescending,
435
+ });
400
436
  return {
401
437
  ...page,
402
438
  results: [...page.results].sort(compareCreatedAtDescending),
@@ -587,12 +623,22 @@ const employeeReadableResources = new Set([
587
623
  BillableReadResource.Quantifier,
588
624
  BillableReadResource.MeterReading,
589
625
  ]);
590
- function catalogOwnerIDs(context) {
626
+ function catalogOwnerIDs(context, employer_scope_ids) {
591
627
  if (context.active_mode === UserTypeEmployee) {
592
- return uniqueIDs(context.employment.active_employer_ids);
628
+ return restrictOwnerIDs(context.employment.active_employer_ids, employer_scope_ids);
593
629
  }
594
630
  return uniqueIDs([context.owner_id]);
595
631
  }
632
+ function activityEmployerScopeIDs(opts) {
633
+ if (opts.context.active_mode === UserTypeEmployer) {
634
+ return new Set([opts.context.owner_id]);
635
+ }
636
+ if (opts.context.active_mode !== UserTypeEmployee ||
637
+ opts.employer_scope_ids === undefined) {
638
+ return undefined;
639
+ }
640
+ return new Set(restrictOwnerIDs(opts.context.employment.active_employer_ids, opts.employer_scope_ids));
641
+ }
596
642
  function activityOwnerIDs(context) {
597
643
  if (context.active_mode === UserTypeEmployer) {
598
644
  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.88",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {