@fabriktor/fx 0.0.86 → 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.
- package/dist/src/billable/billable.d.ts +64 -1
- package/dist/src/billable/billable.js +197 -2
- package/dist/src/contacts/contacts.d.ts +19 -2
- package/dist/src/contacts/contacts.js +108 -1
- package/dist/src/core/search.d.ts +15 -0
- package/dist/src/core/search.js +126 -0
- package/dist/src/fx.js +5 -3
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type BillableOperator } from "@fabriktor/client";
|
|
2
2
|
import { type Bill, BillableFolder, Charge, Equipment, Item, LaborRate, MeterReading, Quantifier, Tax, TmpBillNumber, VendorInvoice, Warehouse } from "@fabriktor/schema";
|
|
3
|
-
import {
|
|
3
|
+
import type { ContactsFXOperator, EmploymentOwnerScope } from "../contacts/contacts.js";
|
|
4
|
+
import { type OwnerSearchOptions, type SearchOptions, type SearchPage, type SearchQuery } from "../core/search.js";
|
|
4
5
|
type GenerateBillPayload = Parameters<BillableOperator["generateBill"]>[0];
|
|
5
6
|
type BillInsertOptions = Parameters<BillableOperator["insertOneBill"]>[0];
|
|
6
7
|
type BillReplaceOptions = Parameters<BillableOperator["replaceOneBill"]>[0];
|
|
@@ -79,6 +80,46 @@ export type SearchWarehousesOptions = OwnerSearchOptions;
|
|
|
79
80
|
export type SearchWarehousesPage = SearchPage<Warehouse>;
|
|
80
81
|
export type BillableFXOptions = {
|
|
81
82
|
billable: BillableOperator;
|
|
83
|
+
contacts_fx: ContactsFXOperator;
|
|
84
|
+
};
|
|
85
|
+
export type LoadBillableReadContextOptions = {
|
|
86
|
+
owner_id: string;
|
|
87
|
+
uid: string;
|
|
88
|
+
active_mode: string;
|
|
89
|
+
};
|
|
90
|
+
export type BillableReadContext = {
|
|
91
|
+
owner_id: string;
|
|
92
|
+
active_mode: string;
|
|
93
|
+
employment: EmploymentOwnerScope;
|
|
94
|
+
};
|
|
95
|
+
export declare const BillableReadResource: {
|
|
96
|
+
readonly Bill: "bill";
|
|
97
|
+
readonly BillableFolder: "billable_folder";
|
|
98
|
+
readonly Charge: "charge";
|
|
99
|
+
readonly Equipment: "equipment";
|
|
100
|
+
readonly Item: "item";
|
|
101
|
+
readonly LaborRate: "labor_rate";
|
|
102
|
+
readonly MeterReading: "meter_reading";
|
|
103
|
+
readonly Quantifier: "quantifier";
|
|
104
|
+
readonly Tax: "tax";
|
|
105
|
+
readonly TmpBillNumber: "tmp_bill_number";
|
|
106
|
+
readonly VendorInvoice: "vendor_invoice";
|
|
107
|
+
readonly Warehouse: "warehouse";
|
|
108
|
+
};
|
|
109
|
+
export type BillableReadResource = (typeof BillableReadResource)[keyof typeof BillableReadResource];
|
|
110
|
+
export type BillableReadSearchOptions = SearchOptions & {
|
|
111
|
+
context: BillableReadContext;
|
|
112
|
+
owner_ids?: Iterable<string>;
|
|
113
|
+
employer_scope_ids?: Iterable<string>;
|
|
114
|
+
query?: SearchQuery;
|
|
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;
|
|
82
123
|
};
|
|
83
124
|
export type GenerateFixedEstimateOptions = {
|
|
84
125
|
in: GenerateBillPayload;
|
|
@@ -105,6 +146,7 @@ export declare const BillablePreActionReason: {
|
|
|
105
146
|
};
|
|
106
147
|
export type BillablePreActionReason = (typeof BillablePreActionReason)[keyof typeof BillablePreActionReason];
|
|
107
148
|
export interface BillableFXOperator {
|
|
149
|
+
loadReadContext(opts: LoadBillableReadContextOptions): Promise<BillableReadContext>;
|
|
108
150
|
createBill(opts: CreateBillOptions): Promise<Bill>;
|
|
109
151
|
replaceBill(opts: ReplaceBillOptions): Promise<Bill>;
|
|
110
152
|
searchBills(opts: SearchBillsOptions): Promise<SearchBillsPage>;
|
|
@@ -114,21 +156,26 @@ export interface BillableFXOperator {
|
|
|
114
156
|
createCharge(opts: CreateChargeOptions): Promise<Charge>;
|
|
115
157
|
replaceCharge(opts: ReplaceChargeOptions): Promise<Charge>;
|
|
116
158
|
searchCharges(opts: SearchChargesOptions): Promise<SearchChargesPage>;
|
|
159
|
+
searchReadableCharges(opts: BillableReadSearchOptions): Promise<SearchChargesPage>;
|
|
117
160
|
createEquipment(opts: CreateEquipmentOptions): Promise<Equipment>;
|
|
118
161
|
replaceEquipment(opts: ReplaceEquipmentOptions): Promise<Equipment>;
|
|
119
162
|
searchEquipments(opts: SearchEquipmentsOptions): Promise<SearchEquipmentsPage>;
|
|
163
|
+
searchReadableEquipments(opts: BillableEquipmentReadSearchOptions): Promise<SearchEquipmentsPage>;
|
|
120
164
|
createItem(opts: CreateItemOptions): Promise<Item>;
|
|
121
165
|
replaceItem(opts: ReplaceItemOptions): Promise<Item>;
|
|
122
166
|
searchItems(opts: SearchItemsOptions): Promise<SearchItemsPage>;
|
|
167
|
+
searchReadableItems(opts: BillableItemReadSearchOptions): Promise<SearchItemsPage>;
|
|
123
168
|
createLaborRate(opts: CreateLaborRateOptions): Promise<LaborRate>;
|
|
124
169
|
replaceLaborRate(opts: ReplaceLaborRateOptions): Promise<LaborRate>;
|
|
125
170
|
searchLaborRates(opts: SearchLaborRatesOptions): Promise<SearchLaborRatesPage>;
|
|
126
171
|
createMeterReading(opts: CreateMeterReadingOptions): Promise<MeterReading>;
|
|
127
172
|
replaceMeterReading(opts: ReplaceMeterReadingOptions): Promise<MeterReading>;
|
|
128
173
|
searchMeterReadings(opts: SearchMeterReadingsOptions): Promise<SearchMeterReadingsPage>;
|
|
174
|
+
searchReadableMeterReadings(opts: BillableReadSearchOptions): Promise<SearchMeterReadingsPage>;
|
|
129
175
|
createQuantifier(opts: CreateQuantifierOptions): Promise<Quantifier>;
|
|
130
176
|
replaceQuantifier(opts: ReplaceQuantifierOptions): Promise<Quantifier>;
|
|
131
177
|
searchQuantifiers(opts: SearchQuantifiersOptions): Promise<SearchQuantifiersPage>;
|
|
178
|
+
searchReadableQuantifiers(opts: BillableReadSearchOptions): Promise<SearchQuantifiersPage>;
|
|
132
179
|
createTax(opts: CreateTaxOptions): Promise<Tax>;
|
|
133
180
|
replaceTax(opts: ReplaceTaxOptions): Promise<Tax>;
|
|
134
181
|
searchTaxes(opts: SearchTaxesOptions): Promise<SearchTaxesPage>;
|
|
@@ -141,6 +188,7 @@ export interface BillableFXOperator {
|
|
|
141
188
|
createWarehouse(opts: CreateWarehouseOptions): Promise<Warehouse>;
|
|
142
189
|
replaceWarehouse(opts: ReplaceWarehouseOptions): Promise<Warehouse>;
|
|
143
190
|
searchWarehouses(opts: SearchWarehousesOptions): Promise<SearchWarehousesPage>;
|
|
191
|
+
searchReadableWarehouses(opts: BillableReadSearchOptions): Promise<SearchWarehousesPage>;
|
|
144
192
|
generateFixedEstimate(opts: GenerateFixedEstimateOptions): Promise<Bill>;
|
|
145
193
|
acceptFixedEstimate(opts: AcceptFixedEstimateOptions): Promise<Bill>;
|
|
146
194
|
generateDynamicEstimate(opts: GenerateDynamicEstimateOptions): Promise<Bill>;
|
|
@@ -151,7 +199,9 @@ export interface BillableFXOperator {
|
|
|
151
199
|
}
|
|
152
200
|
export declare class BillableFX implements BillableFXOperator {
|
|
153
201
|
private billable;
|
|
202
|
+
private contacts_fx;
|
|
154
203
|
constructor(opts: BillableFXOptions);
|
|
204
|
+
loadReadContext(opts: LoadBillableReadContextOptions): Promise<BillableReadContext>;
|
|
155
205
|
createBill(opts: CreateBillOptions): Promise<Bill>;
|
|
156
206
|
replaceBill(opts: ReplaceBillOptions): Promise<Bill>;
|
|
157
207
|
searchBills(opts: SearchBillsOptions): Promise<SearchBillsPage>;
|
|
@@ -161,21 +211,26 @@ export declare class BillableFX implements BillableFXOperator {
|
|
|
161
211
|
createCharge(opts: CreateChargeOptions): Promise<Charge>;
|
|
162
212
|
replaceCharge(opts: ReplaceChargeOptions): Promise<Charge>;
|
|
163
213
|
searchCharges(opts: SearchChargesOptions): Promise<SearchChargesPage>;
|
|
214
|
+
searchReadableCharges(opts: BillableReadSearchOptions): Promise<SearchChargesPage>;
|
|
164
215
|
createEquipment(opts: CreateEquipmentOptions): Promise<Equipment>;
|
|
165
216
|
replaceEquipment(opts: ReplaceEquipmentOptions): Promise<Equipment>;
|
|
166
217
|
searchEquipments(opts: SearchEquipmentsOptions): Promise<SearchEquipmentsPage>;
|
|
218
|
+
searchReadableEquipments(opts: BillableEquipmentReadSearchOptions): Promise<SearchEquipmentsPage>;
|
|
167
219
|
createItem(opts: CreateItemOptions): Promise<Item>;
|
|
168
220
|
replaceItem(opts: ReplaceItemOptions): Promise<Item>;
|
|
169
221
|
searchItems(opts: SearchItemsOptions): Promise<SearchItemsPage>;
|
|
222
|
+
searchReadableItems(opts: BillableItemReadSearchOptions): Promise<SearchItemsPage>;
|
|
170
223
|
createLaborRate(opts: CreateLaborRateOptions): Promise<LaborRate>;
|
|
171
224
|
replaceLaborRate(opts: ReplaceLaborRateOptions): Promise<LaborRate>;
|
|
172
225
|
searchLaborRates(opts: SearchLaborRatesOptions): Promise<SearchLaborRatesPage>;
|
|
173
226
|
createMeterReading(opts: CreateMeterReadingOptions): Promise<MeterReading>;
|
|
174
227
|
replaceMeterReading(opts: ReplaceMeterReadingOptions): Promise<MeterReading>;
|
|
175
228
|
searchMeterReadings(opts: SearchMeterReadingsOptions): Promise<SearchMeterReadingsPage>;
|
|
229
|
+
searchReadableMeterReadings(opts: BillableReadSearchOptions): Promise<SearchMeterReadingsPage>;
|
|
176
230
|
createQuantifier(opts: CreateQuantifierOptions): Promise<Quantifier>;
|
|
177
231
|
replaceQuantifier(opts: ReplaceQuantifierOptions): Promise<Quantifier>;
|
|
178
232
|
searchQuantifiers(opts: SearchQuantifiersOptions): Promise<SearchQuantifiersPage>;
|
|
233
|
+
searchReadableQuantifiers(opts: BillableReadSearchOptions): Promise<SearchQuantifiersPage>;
|
|
179
234
|
createTax(opts: CreateTaxOptions): Promise<Tax>;
|
|
180
235
|
replaceTax(opts: ReplaceTaxOptions): Promise<Tax>;
|
|
181
236
|
searchTaxes(opts: SearchTaxesOptions): Promise<SearchTaxesPage>;
|
|
@@ -188,6 +243,7 @@ export declare class BillableFX implements BillableFXOperator {
|
|
|
188
243
|
createWarehouse(opts: CreateWarehouseOptions): Promise<Warehouse>;
|
|
189
244
|
replaceWarehouse(opts: ReplaceWarehouseOptions): Promise<Warehouse>;
|
|
190
245
|
searchWarehouses(opts: SearchWarehousesOptions): Promise<SearchWarehousesPage>;
|
|
246
|
+
searchReadableWarehouses(opts: BillableReadSearchOptions): Promise<SearchWarehousesPage>;
|
|
191
247
|
generateFixedEstimate(opts: GenerateFixedEstimateOptions): Promise<Bill>;
|
|
192
248
|
acceptFixedEstimate(opts: AcceptFixedEstimateOptions): Promise<Bill>;
|
|
193
249
|
generateDynamicEstimate(opts: GenerateDynamicEstimateOptions): Promise<Bill>;
|
|
@@ -197,6 +253,7 @@ export declare class BillableFX implements BillableFXOperator {
|
|
|
197
253
|
vendorInvoiceFromBill(opts: VendorInvoiceFromBillOptions): Promise<VendorInvoice>;
|
|
198
254
|
private generateBill;
|
|
199
255
|
private checkVendorInvoiceNumber;
|
|
256
|
+
private searchReadable;
|
|
200
257
|
private findBill;
|
|
201
258
|
private findBillableFolder;
|
|
202
259
|
private findCharge;
|
|
@@ -210,5 +267,11 @@ export declare class BillableFX implements BillableFXOperator {
|
|
|
210
267
|
private findVendorInvoice;
|
|
211
268
|
private findWarehouse;
|
|
212
269
|
}
|
|
270
|
+
export declare function billableCanReadResource(context: BillableReadContext, resource: BillableReadResource): boolean;
|
|
271
|
+
export declare function billableCanCreateResource(context: BillableReadContext, resource: BillableReadResource): boolean;
|
|
272
|
+
export declare function billableOwnsRecord(context: BillableReadContext, record: {
|
|
273
|
+
owner_id: string;
|
|
274
|
+
} | null | undefined): boolean;
|
|
275
|
+
export declare function billableCatalogOwnerIDs(context: BillableReadContext): string[];
|
|
213
276
|
export declare function newBillableFX(opts: BillableFXOptions): BillableFX;
|
|
214
277
|
export {};
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
// not use this file except in compliance with the License. You may obtain
|
|
5
5
|
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
6
|
import { queryParams } from "@fabriktor/client";
|
|
7
|
-
import { BillTypeDynamic, BillTypeFixed, } from "@fabriktor/schema";
|
|
7
|
+
import { BillTypeDynamic, BillTypeFixed, UserTypeEmployee, UserTypeEmployer, } from "@fabriktor/schema";
|
|
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 { 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.";
|
|
@@ -51,13 +51,48 @@ const msgFixedEstimateRequired = "Only a fixed estimate can be accepted.";
|
|
|
51
51
|
const msgInternalFixedEstimateRequired = "Only an internal fixed estimate can be accepted in Fabriktor.";
|
|
52
52
|
const msgCanceledEstimate = "A canceled fixed estimate cannot be accepted.";
|
|
53
53
|
const msgEstimateAlreadyAccepted = "This fixed estimate is already accepted.";
|
|
54
|
+
export const BillableReadResource = {
|
|
55
|
+
Bill: "bill",
|
|
56
|
+
BillableFolder: "billable_folder",
|
|
57
|
+
Charge: "charge",
|
|
58
|
+
Equipment: "equipment",
|
|
59
|
+
Item: "item",
|
|
60
|
+
LaborRate: "labor_rate",
|
|
61
|
+
MeterReading: "meter_reading",
|
|
62
|
+
Quantifier: "quantifier",
|
|
63
|
+
Tax: "tax",
|
|
64
|
+
TmpBillNumber: "tmp_bill_number",
|
|
65
|
+
VendorInvoice: "vendor_invoice",
|
|
66
|
+
Warehouse: "warehouse",
|
|
67
|
+
};
|
|
54
68
|
export const BillablePreActionReason = {
|
|
55
69
|
VendorInvoiceNumberExists: "vendor_invoice_number_exists",
|
|
56
70
|
};
|
|
57
71
|
export class BillableFX {
|
|
58
72
|
billable;
|
|
73
|
+
contacts_fx;
|
|
59
74
|
constructor(opts) {
|
|
60
75
|
this.billable = opts.billable;
|
|
76
|
+
this.contacts_fx = opts.contacts_fx;
|
|
77
|
+
}
|
|
78
|
+
async loadReadContext(opts) {
|
|
79
|
+
const owner_id = opts.owner_id.trim();
|
|
80
|
+
if (owner_id === "") {
|
|
81
|
+
throw errRequired({
|
|
82
|
+
field: "owner_id",
|
|
83
|
+
msg: msgOwnerIDRequired,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const employment = await this.contacts_fx.employmentOwnerScope({
|
|
87
|
+
owner_id,
|
|
88
|
+
uid: opts.uid,
|
|
89
|
+
active_mode: opts.active_mode,
|
|
90
|
+
});
|
|
91
|
+
return {
|
|
92
|
+
owner_id,
|
|
93
|
+
active_mode: opts.active_mode,
|
|
94
|
+
employment,
|
|
95
|
+
};
|
|
61
96
|
}
|
|
62
97
|
async createBill(opts) {
|
|
63
98
|
const out = await this.billable.insertOneBill(opts);
|
|
@@ -104,6 +139,17 @@ export class BillableFX {
|
|
|
104
139
|
search: (searchOpts) => this.billable.searchManyCharges(searchOpts),
|
|
105
140
|
});
|
|
106
141
|
}
|
|
142
|
+
async searchReadableCharges(opts) {
|
|
143
|
+
const employer_scope_ids = activityEmployerScopeIDs(opts);
|
|
144
|
+
return await this.searchReadable({
|
|
145
|
+
...opts,
|
|
146
|
+
allowed_owner_ids: activityOwnerIDs(opts.context),
|
|
147
|
+
filter: employer_scope_ids === undefined
|
|
148
|
+
? undefined
|
|
149
|
+
: (charge) => employer_scope_ids.has(charge.employer_id),
|
|
150
|
+
search: (searchOpts) => this.billable.searchManyCharges(searchOpts),
|
|
151
|
+
});
|
|
152
|
+
}
|
|
107
153
|
async createEquipment(opts) {
|
|
108
154
|
const out = await this.billable.insertOneEquipment(opts);
|
|
109
155
|
const id = requiredOperationID(out, msgCreatedEquipmentIDMissing);
|
|
@@ -119,6 +165,18 @@ export class BillableFX {
|
|
|
119
165
|
search: (searchOpts) => this.billable.searchManyEquipments(searchOpts),
|
|
120
166
|
});
|
|
121
167
|
}
|
|
168
|
+
async searchReadableEquipments(opts) {
|
|
169
|
+
const { warehouse_id, rental_type, ...search_opts } = opts;
|
|
170
|
+
return await this.searchReadable({
|
|
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),
|
|
177
|
+
search: (searchOpts) => this.billable.searchManyEquipments(searchOpts),
|
|
178
|
+
});
|
|
179
|
+
}
|
|
122
180
|
async createItem(opts) {
|
|
123
181
|
const out = await this.billable.insertOneItem(opts);
|
|
124
182
|
const id = requiredOperationID(out, msgCreatedItemIDMissing);
|
|
@@ -134,6 +192,19 @@ export class BillableFX {
|
|
|
134
192
|
search: (searchOpts) => this.billable.searchManyItems(searchOpts),
|
|
135
193
|
});
|
|
136
194
|
}
|
|
195
|
+
async searchReadableItems(opts) {
|
|
196
|
+
const { warehouse_id, quantifier_id, ...search_opts } = opts;
|
|
197
|
+
return await this.searchReadable({
|
|
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),
|
|
205
|
+
search: (searchOpts) => this.billable.searchManyItems(searchOpts),
|
|
206
|
+
});
|
|
207
|
+
}
|
|
137
208
|
async createLaborRate(opts) {
|
|
138
209
|
const out = await this.billable.insertOneLaborRate(opts);
|
|
139
210
|
const id = requiredOperationID(out, msgCreatedLaborRateIDMissing);
|
|
@@ -164,6 +235,25 @@ export class BillableFX {
|
|
|
164
235
|
search: (searchOpts) => this.billable.searchManyMeterReadings(searchOpts),
|
|
165
236
|
});
|
|
166
237
|
}
|
|
238
|
+
async searchReadableMeterReadings(opts) {
|
|
239
|
+
const employer_scope_ids = activityEmployerScopeIDs(opts);
|
|
240
|
+
const equipment_owners = new Map();
|
|
241
|
+
return await this.searchReadable({
|
|
242
|
+
...opts,
|
|
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
|
+
},
|
|
254
|
+
search: (searchOpts) => this.billable.searchManyMeterReadings(searchOpts),
|
|
255
|
+
});
|
|
256
|
+
}
|
|
167
257
|
async createQuantifier(opts) {
|
|
168
258
|
const out = await this.billable.insertOneQuantifier(opts);
|
|
169
259
|
const id = requiredOperationID(out, msgCreatedQuantifierIDMissing);
|
|
@@ -179,6 +269,13 @@ export class BillableFX {
|
|
|
179
269
|
search: (searchOpts) => this.billable.searchManyQuantifiers(searchOpts),
|
|
180
270
|
});
|
|
181
271
|
}
|
|
272
|
+
async searchReadableQuantifiers(opts) {
|
|
273
|
+
return await this.searchReadable({
|
|
274
|
+
...opts,
|
|
275
|
+
allowed_owner_ids: catalogOwnerIDs(opts.context, opts.employer_scope_ids),
|
|
276
|
+
search: (searchOpts) => this.billable.searchManyQuantifiers(searchOpts),
|
|
277
|
+
});
|
|
278
|
+
}
|
|
182
279
|
async createTax(opts) {
|
|
183
280
|
const out = await this.billable.insertOneTax(opts);
|
|
184
281
|
const id = requiredOperationID(out, msgCreatedTaxIDMissing);
|
|
@@ -253,6 +350,13 @@ export class BillableFX {
|
|
|
253
350
|
search: (searchOpts) => this.billable.searchManyWarehouses(searchOpts),
|
|
254
351
|
});
|
|
255
352
|
}
|
|
353
|
+
async searchReadableWarehouses(opts) {
|
|
354
|
+
return await this.searchReadable({
|
|
355
|
+
...opts,
|
|
356
|
+
allowed_owner_ids: catalogOwnerIDs(opts.context, opts.employer_scope_ids),
|
|
357
|
+
search: (searchOpts) => this.billable.searchManyWarehouses(searchOpts),
|
|
358
|
+
});
|
|
359
|
+
}
|
|
256
360
|
async generateFixedEstimate(opts) {
|
|
257
361
|
return await this.generateBill(billWith(opts.in, {
|
|
258
362
|
type: BillTypeFixed,
|
|
@@ -311,6 +415,29 @@ export class BillableFX {
|
|
|
311
415
|
},
|
|
312
416
|
});
|
|
313
417
|
}
|
|
418
|
+
async searchReadable(opts) {
|
|
419
|
+
const { context: _context, owner_ids: requested_owner_ids, employer_scope_ids: _employer_scope_ids, allowed_owner_ids, search, filter, query, ...searchOpts } = opts;
|
|
420
|
+
const owner_ids = restrictOwnerIDs(allowed_owner_ids, requested_owner_ids);
|
|
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
|
+
});
|
|
436
|
+
return {
|
|
437
|
+
...page,
|
|
438
|
+
results: [...page.results].sort(compareCreatedAtDescending),
|
|
439
|
+
};
|
|
440
|
+
}
|
|
314
441
|
async findBill(id) {
|
|
315
442
|
const out = await this.billable.findOneBill({
|
|
316
443
|
id,
|
|
@@ -468,6 +595,74 @@ export class BillableFX {
|
|
|
468
595
|
return value;
|
|
469
596
|
}
|
|
470
597
|
}
|
|
598
|
+
export function billableCanReadResource(context, resource) {
|
|
599
|
+
if (context.active_mode !== UserTypeEmployee) {
|
|
600
|
+
return true;
|
|
601
|
+
}
|
|
602
|
+
return employeeReadableResources.has(resource);
|
|
603
|
+
}
|
|
604
|
+
export function billableCanCreateResource(context, resource) {
|
|
605
|
+
if (context.active_mode === UserTypeEmployer) {
|
|
606
|
+
return true;
|
|
607
|
+
}
|
|
608
|
+
return (context.active_mode === UserTypeEmployee &&
|
|
609
|
+
(resource === BillableReadResource.Charge ||
|
|
610
|
+
resource === BillableReadResource.MeterReading));
|
|
611
|
+
}
|
|
612
|
+
export function billableOwnsRecord(context, record) {
|
|
613
|
+
return record !== null && record !== undefined && record.owner_id === context.owner_id;
|
|
614
|
+
}
|
|
615
|
+
export function billableCatalogOwnerIDs(context) {
|
|
616
|
+
return catalogOwnerIDs(context);
|
|
617
|
+
}
|
|
618
|
+
const employeeReadableResources = new Set([
|
|
619
|
+
BillableReadResource.Item,
|
|
620
|
+
BillableReadResource.Equipment,
|
|
621
|
+
BillableReadResource.Charge,
|
|
622
|
+
BillableReadResource.Warehouse,
|
|
623
|
+
BillableReadResource.Quantifier,
|
|
624
|
+
BillableReadResource.MeterReading,
|
|
625
|
+
]);
|
|
626
|
+
function catalogOwnerIDs(context, employer_scope_ids) {
|
|
627
|
+
if (context.active_mode === UserTypeEmployee) {
|
|
628
|
+
return restrictOwnerIDs(context.employment.active_employer_ids, employer_scope_ids);
|
|
629
|
+
}
|
|
630
|
+
return uniqueIDs([context.owner_id]);
|
|
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
|
+
}
|
|
642
|
+
function activityOwnerIDs(context) {
|
|
643
|
+
if (context.active_mode === UserTypeEmployer) {
|
|
644
|
+
return uniqueIDs([context.owner_id, ...context.employment.related_employee_ids]);
|
|
645
|
+
}
|
|
646
|
+
return uniqueIDs([context.owner_id]);
|
|
647
|
+
}
|
|
648
|
+
function restrictOwnerIDs(allowed, requested) {
|
|
649
|
+
const allowed_ids = uniqueIDs(allowed);
|
|
650
|
+
if (requested === undefined) {
|
|
651
|
+
return allowed_ids;
|
|
652
|
+
}
|
|
653
|
+
const allowed_set = new Set(allowed_ids);
|
|
654
|
+
return uniqueIDs(requested).filter((id) => allowed_set.has(id));
|
|
655
|
+
}
|
|
656
|
+
function uniqueIDs(ids) {
|
|
657
|
+
return [...new Set([...ids].map((id) => id.trim()).filter((id) => id !== ""))];
|
|
658
|
+
}
|
|
659
|
+
function compareCreatedAtDescending(left, right) {
|
|
660
|
+
return timestamp(right.created_at) - timestamp(left.created_at);
|
|
661
|
+
}
|
|
662
|
+
function timestamp(value) {
|
|
663
|
+
const out = globalThis.Date.parse(value);
|
|
664
|
+
return Number.isFinite(out) ? out : 0;
|
|
665
|
+
}
|
|
471
666
|
export function newBillableFX(opts) {
|
|
472
667
|
return new BillableFX(opts);
|
|
473
668
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { ContactsOperator } from "@fabriktor/client";
|
|
2
|
-
import { Friend, Hired, Manager, Na, OldHire, type Contact, type ID, type OperationResult } from "@fabriktor/schema";
|
|
1
|
+
import type { ContactsOperator, UsersOperator } from "@fabriktor/client";
|
|
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;
|
|
@@ -8,10 +8,22 @@ export type ContactRelation = typeof Na | typeof Friend | typeof OldHire | typeo
|
|
|
8
8
|
export type ActiveContactRelation = typeof Friend | typeof Hired | typeof Manager;
|
|
9
9
|
export type ContactsFXOptions = {
|
|
10
10
|
contacts: ContactsOperator;
|
|
11
|
+
users: UsersOperator;
|
|
11
12
|
};
|
|
12
13
|
export type LoadContactIndexOptions = {
|
|
13
14
|
owner_id: string;
|
|
14
15
|
};
|
|
16
|
+
export type EmploymentOwnerScopeOptions = {
|
|
17
|
+
owner_id: string;
|
|
18
|
+
uid: string;
|
|
19
|
+
active_mode: string;
|
|
20
|
+
};
|
|
21
|
+
export type EmploymentOwnerScope = {
|
|
22
|
+
active_employer_ids: string[];
|
|
23
|
+
active_employers: User[];
|
|
24
|
+
current_employee_ids: string[];
|
|
25
|
+
related_employee_ids: string[];
|
|
26
|
+
};
|
|
15
27
|
export type ContactIndex = {
|
|
16
28
|
contacts: Contact[];
|
|
17
29
|
peer_ids: Set<string>;
|
|
@@ -49,6 +61,7 @@ export interface ContactsFXOperator {
|
|
|
49
61
|
createContact(opts: CreateContactOptions): Promise<Contact>;
|
|
50
62
|
replaceContact(opts: ReplaceContactOptions): Promise<Contact>;
|
|
51
63
|
loadContactIndex(opts: LoadContactIndexOptions): Promise<ContactIndex>;
|
|
64
|
+
employmentOwnerScope(opts: EmploymentOwnerScopeOptions): Promise<EmploymentOwnerScope>;
|
|
52
65
|
startPresence(opts: StartPresenceOptions): Promise<PresenceSession>;
|
|
53
66
|
sendFriendRequest(opts: SendFriendRequestOptions): Promise<OperationResult>;
|
|
54
67
|
requestRelationUpgrade(opts: RequestRelationUpgradeOptions): Promise<OperationResult>;
|
|
@@ -59,10 +72,12 @@ export interface ContactsFXOperator {
|
|
|
59
72
|
}
|
|
60
73
|
export declare class ContactsFX implements ContactsFXOperator {
|
|
61
74
|
private contacts;
|
|
75
|
+
private users;
|
|
62
76
|
constructor(opts: ContactsFXOptions);
|
|
63
77
|
createContact(opts: CreateContactOptions): Promise<Contact>;
|
|
64
78
|
replaceContact(opts: ReplaceContactOptions): Promise<Contact>;
|
|
65
79
|
loadContactIndex(opts: LoadContactIndexOptions): Promise<ContactIndex>;
|
|
80
|
+
employmentOwnerScope(opts: EmploymentOwnerScopeOptions): Promise<EmploymentOwnerScope>;
|
|
66
81
|
startPresence(opts: StartPresenceOptions): Promise<PresenceSession>;
|
|
67
82
|
sendFriendRequest(opts: SendFriendRequestOptions): Promise<OperationResult>;
|
|
68
83
|
requestRelationUpgrade(opts: RequestRelationUpgradeOptions): Promise<OperationResult>;
|
|
@@ -71,7 +86,9 @@ export declare class ContactsFX implements ContactsFXOperator {
|
|
|
71
86
|
rejectContactRequest(opts: RejectContactRequestOptions): Promise<OperationResult>;
|
|
72
87
|
endHireRelation(opts: EndHireRelationOptions): Promise<OperationResult>;
|
|
73
88
|
private replaceContactResult;
|
|
89
|
+
private usersByIDs;
|
|
74
90
|
private findContact;
|
|
75
91
|
}
|
|
76
92
|
export declare function newContactsFX(opts: ContactsFXOptions): ContactsFX;
|
|
93
|
+
export declare function emptyEmploymentOwnerScope(): EmploymentOwnerScope;
|
|
77
94
|
export {};
|
|
@@ -3,22 +3,27 @@
|
|
|
3
3
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
4
4
|
// not use this file except in compliance with the License. You may obtain
|
|
5
5
|
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
-
import { Friend, Na, OldHire, } from "@fabriktor/schema";
|
|
6
|
+
import { Friend, Hired, IDKey, Na, OldHire, UserTypeEmployee, UserTypeEmployer, } from "@fabriktor/schema";
|
|
7
7
|
import { errRequired, errResolveFailed, errValidation } from "../core/err.js";
|
|
8
8
|
import { requiredOperationID } from "../core/id.js";
|
|
9
|
+
import { runSearch } from "../core/search.js";
|
|
9
10
|
const presenceIntervalMS = 120_000;
|
|
11
|
+
const employmentUserBatchSize = 30;
|
|
10
12
|
const resourceContact = "contact";
|
|
11
13
|
const msgCreatedContactIDMissing = "Created contact response did not contain an ID.";
|
|
12
14
|
const msgContactMissing = "Contact could not be resolved after the mutation.";
|
|
13
15
|
const msgOwnerIDRequired = "Owner ID is required.";
|
|
16
|
+
const msgUIDRequired = "User UID is required.";
|
|
14
17
|
const msgContactIDRequired = "Contact ID is required.";
|
|
15
18
|
const msgRelationUpgradeInvalid = "Requested relation must be higher than the current relation.";
|
|
16
19
|
const msgRelationDowngradeInvalid = "Requested relation must be lower than the current relation.";
|
|
17
20
|
const msgPendingRelationRequired = "Contact request does not contain a pending relation.";
|
|
18
21
|
export class ContactsFX {
|
|
19
22
|
contacts;
|
|
23
|
+
users;
|
|
20
24
|
constructor(opts) {
|
|
21
25
|
this.contacts = opts.contacts;
|
|
26
|
+
this.users = opts.users;
|
|
22
27
|
}
|
|
23
28
|
async createContact(opts) {
|
|
24
29
|
const out = await this.contacts.insertOneContact(opts);
|
|
@@ -51,6 +56,69 @@ export class ContactsFX {
|
|
|
51
56
|
peer_ids,
|
|
52
57
|
};
|
|
53
58
|
}
|
|
59
|
+
async employmentOwnerScope(opts) {
|
|
60
|
+
const owner_id = opts.owner_id.trim();
|
|
61
|
+
if (owner_id === "") {
|
|
62
|
+
throw errRequired({
|
|
63
|
+
field: "owner_id",
|
|
64
|
+
msg: msgOwnerIDRequired,
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
const uid = opts.uid.trim();
|
|
68
|
+
if (uid === "") {
|
|
69
|
+
throw errRequired({
|
|
70
|
+
field: "uid",
|
|
71
|
+
msg: msgUIDRequired,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if (opts.active_mode !== UserTypeEmployer &&
|
|
75
|
+
opts.active_mode !== UserTypeEmployee) {
|
|
76
|
+
return emptyEmploymentOwnerScope();
|
|
77
|
+
}
|
|
78
|
+
const out = await this.contacts.findContacts();
|
|
79
|
+
const contacts = (out.value ?? []).filter((contact) => !contact.is_archived &&
|
|
80
|
+
!contact.is_pending &&
|
|
81
|
+
(contact.relation === Hired || contact.relation === OldHire));
|
|
82
|
+
const peer_ids = contacts
|
|
83
|
+
.map((contact) => contactPeerID(contact, owner_id))
|
|
84
|
+
.filter((id) => id !== undefined);
|
|
85
|
+
const users = await this.usersByIDs(uid, peer_ids);
|
|
86
|
+
const users_by_id = new Map(users.map((user) => [user.id, user]));
|
|
87
|
+
const active_employer_ids = new Set();
|
|
88
|
+
const current_employee_ids = new Set();
|
|
89
|
+
const related_employee_ids = new Set();
|
|
90
|
+
for (const contact of contacts) {
|
|
91
|
+
const peer_id = contactPeerID(contact, owner_id);
|
|
92
|
+
if (peer_id === undefined) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const peer = users_by_id.get(peer_id);
|
|
96
|
+
if (peer === undefined) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (opts.active_mode === UserTypeEmployee &&
|
|
100
|
+
peer.account_identity === UserTypeEmployer &&
|
|
101
|
+
contact.relation === Hired) {
|
|
102
|
+
active_employer_ids.add(peer_id);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (opts.active_mode === UserTypeEmployer &&
|
|
106
|
+
peer.account_identity === UserTypeEmployee) {
|
|
107
|
+
related_employee_ids.add(peer_id);
|
|
108
|
+
if (contact.relation === Hired) {
|
|
109
|
+
current_employee_ids.add(peer_id);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
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),
|
|
118
|
+
current_employee_ids: [...current_employee_ids],
|
|
119
|
+
related_employee_ids: [...related_employee_ids],
|
|
120
|
+
};
|
|
121
|
+
}
|
|
54
122
|
async startPresence(opts) {
|
|
55
123
|
const initial = await this.contacts.signal();
|
|
56
124
|
opts.on_active(initial.value ?? []);
|
|
@@ -175,6 +243,27 @@ export class ContactsFX {
|
|
|
175
243
|
},
|
|
176
244
|
});
|
|
177
245
|
}
|
|
246
|
+
async usersByIDs(uid, ids) {
|
|
247
|
+
const values = uniqueIDs(ids);
|
|
248
|
+
if (values.length === 0) {
|
|
249
|
+
return [];
|
|
250
|
+
}
|
|
251
|
+
const pages = await Promise.all(chunks(values, employmentUserBatchSize).map((chunk) => runSearch({
|
|
252
|
+
per_page: chunk.length,
|
|
253
|
+
query: { [IDKey]: chunk },
|
|
254
|
+
search: (searchOpts) => this.users.searchManyUsers({
|
|
255
|
+
...searchOpts,
|
|
256
|
+
uid,
|
|
257
|
+
}),
|
|
258
|
+
})));
|
|
259
|
+
const users = new Map();
|
|
260
|
+
for (const page of pages) {
|
|
261
|
+
for (const user of page.results) {
|
|
262
|
+
users.set(user.id, user);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return [...users.values()];
|
|
266
|
+
}
|
|
178
267
|
async findContact(id) {
|
|
179
268
|
const out = await this.contacts.findOneContact({
|
|
180
269
|
id,
|
|
@@ -192,6 +281,24 @@ export class ContactsFX {
|
|
|
192
281
|
export function newContactsFX(opts) {
|
|
193
282
|
return new ContactsFX(opts);
|
|
194
283
|
}
|
|
284
|
+
export function emptyEmploymentOwnerScope() {
|
|
285
|
+
return {
|
|
286
|
+
active_employer_ids: [],
|
|
287
|
+
active_employers: [],
|
|
288
|
+
current_employee_ids: [],
|
|
289
|
+
related_employee_ids: [],
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function uniqueIDs(ids) {
|
|
293
|
+
return [...new Set([...ids].map((id) => id.trim()).filter((id) => id !== ""))];
|
|
294
|
+
}
|
|
295
|
+
function chunks(values, size) {
|
|
296
|
+
const out = [];
|
|
297
|
+
for (let i = 0; i < values.length; i += size) {
|
|
298
|
+
out.push(values.slice(i, i + size));
|
|
299
|
+
}
|
|
300
|
+
return out;
|
|
301
|
+
}
|
|
195
302
|
function contactPeerID(contact, owner_id) {
|
|
196
303
|
const contact_owner_id = contact.owner_id.trim();
|
|
197
304
|
const contact_id = contact.contact_id.trim();
|
|
@@ -22,6 +22,15 @@ export type RunOwnerSearchOptions<T> = OwnerSearchOptions & {
|
|
|
22
22
|
search: SearchCall<T>;
|
|
23
23
|
query?: SearchQuery;
|
|
24
24
|
};
|
|
25
|
+
export type OwnerScopeSearchOptions<T> = SearchOptions & {
|
|
26
|
+
owner_ids: Iterable<string>;
|
|
27
|
+
search: SearchCall<T>;
|
|
28
|
+
query?: SearchQuery;
|
|
29
|
+
};
|
|
30
|
+
export type OwnerScopeFilteredSearchOptions<T> = OwnerScopeSearchOptions<T> & {
|
|
31
|
+
filter: (value: T) => boolean | Promise<boolean>;
|
|
32
|
+
compare?: (left: T, right: T) => number;
|
|
33
|
+
};
|
|
25
34
|
export type SearchPage<T> = {
|
|
26
35
|
results: T[];
|
|
27
36
|
page: number;
|
|
@@ -34,6 +43,12 @@ export type SearchPage<T> = {
|
|
|
34
43
|
};
|
|
35
44
|
export declare function withOwnerQuery(owner_id: string, query?: SearchQuery): SearchQuery;
|
|
36
45
|
export declare function runOwnerSearch<T>(opts: RunOwnerSearchOptions<T>): Promise<SearchPage<T>>;
|
|
46
|
+
export declare function runOwnerScopeSearch<T extends {
|
|
47
|
+
id: string;
|
|
48
|
+
}>(opts: OwnerScopeSearchOptions<T>): Promise<SearchPage<T>>;
|
|
49
|
+
export declare function runOwnerScopeFilteredSearch<T extends {
|
|
50
|
+
id: string;
|
|
51
|
+
}>(opts: OwnerScopeFilteredSearchOptions<T>): Promise<SearchPage<T>>;
|
|
37
52
|
export declare function runSearch<T>(opts: RunSearchOptions<T>): Promise<SearchPage<T>>;
|
|
38
53
|
export declare function normalizeSearchPage(value?: number): number;
|
|
39
54
|
export declare function normalizeSearchPerPage(value?: number): number;
|
package/dist/src/core/search.js
CHANGED
|
@@ -19,6 +19,101 @@ export async function runOwnerSearch(opts) {
|
|
|
19
19
|
query: withOwnerQuery(owner_id, query),
|
|
20
20
|
});
|
|
21
21
|
}
|
|
22
|
+
export async function runOwnerScopeSearch(opts) {
|
|
23
|
+
const owner_ids = uniqueOwnerIDs(opts.owner_ids);
|
|
24
|
+
const page = normalizeSearchPage(opts.page);
|
|
25
|
+
const per_page = normalizeSearchPerPage(opts.per_page);
|
|
26
|
+
if (owner_ids.length === 0) {
|
|
27
|
+
return emptySearchPage(page, per_page);
|
|
28
|
+
}
|
|
29
|
+
const pages = await Promise.all(owner_ids.map((owner_id) => runOwnerSearch({
|
|
30
|
+
owner_id,
|
|
31
|
+
q: opts.q,
|
|
32
|
+
page,
|
|
33
|
+
per_page,
|
|
34
|
+
query: opts.query,
|
|
35
|
+
search: opts.search,
|
|
36
|
+
})));
|
|
37
|
+
const results = new Map();
|
|
38
|
+
for (const ownerPage of pages) {
|
|
39
|
+
for (const result of ownerPage.results) {
|
|
40
|
+
results.set(result.id, result);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const has_more = pages.some((ownerPage) => ownerPage.has_more);
|
|
44
|
+
const total_count = sumDefined(pages.map((ownerPage) => ownerPage.total_count));
|
|
45
|
+
const last_page = maxDefined(pages.map((ownerPage) => ownerPage.last_page));
|
|
46
|
+
return {
|
|
47
|
+
results: [...results.values()],
|
|
48
|
+
page,
|
|
49
|
+
per_page,
|
|
50
|
+
total_count,
|
|
51
|
+
last_page,
|
|
52
|
+
has_more,
|
|
53
|
+
next_page: has_more ? page + 1 : page,
|
|
54
|
+
done: !has_more,
|
|
55
|
+
};
|
|
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
|
+
}
|
|
22
117
|
export async function runSearch(opts) {
|
|
23
118
|
const page = normalizeSearchPage(opts.page);
|
|
24
119
|
const per_page = normalizeSearchPerPage(opts.per_page);
|
|
@@ -95,3 +190,34 @@ function hasMoreSearchResults(opts) {
|
|
|
95
190
|
}
|
|
96
191
|
return opts.result_count >= opts.per_page;
|
|
97
192
|
}
|
|
193
|
+
function uniqueOwnerIDs(ids) {
|
|
194
|
+
return [...new Set([...ids].map((id) => id.trim()).filter((id) => id !== ""))];
|
|
195
|
+
}
|
|
196
|
+
function emptySearchPage(page, per_page) {
|
|
197
|
+
return {
|
|
198
|
+
results: [],
|
|
199
|
+
page,
|
|
200
|
+
per_page,
|
|
201
|
+
total_count: 0,
|
|
202
|
+
last_page: page,
|
|
203
|
+
has_more: false,
|
|
204
|
+
next_page: page,
|
|
205
|
+
done: true,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
function sumDefined(values) {
|
|
209
|
+
if (values.some((value) => value === undefined)) {
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
let sum = 0;
|
|
213
|
+
for (const value of values) {
|
|
214
|
+
sum += value ?? 0;
|
|
215
|
+
}
|
|
216
|
+
return sum;
|
|
217
|
+
}
|
|
218
|
+
function maxDefined(values) {
|
|
219
|
+
if (values.some((value) => value === undefined) || values.length === 0) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
return Math.max(...values.map((value) => value ?? 0));
|
|
223
|
+
}
|
package/dist/src/fx.js
CHANGED
|
@@ -46,8 +46,13 @@ export class FX {
|
|
|
46
46
|
timesheets;
|
|
47
47
|
users;
|
|
48
48
|
constructor(opts) {
|
|
49
|
+
this.contacts = newContactsFX({
|
|
50
|
+
contacts: opts.contacts,
|
|
51
|
+
users: opts.users,
|
|
52
|
+
});
|
|
49
53
|
this.billable = newBillableFX({
|
|
50
54
|
billable: opts.billable,
|
|
55
|
+
contacts_fx: this.contacts,
|
|
51
56
|
});
|
|
52
57
|
this.calls = newCallsFX({
|
|
53
58
|
calls: opts.calls,
|
|
@@ -57,9 +62,6 @@ export class FX {
|
|
|
57
62
|
chat: opts.chat,
|
|
58
63
|
users: opts.users,
|
|
59
64
|
});
|
|
60
|
-
this.contacts = newContactsFX({
|
|
61
|
-
contacts: opts.contacts,
|
|
62
|
-
});
|
|
63
65
|
this.fulfillments = newFulfillmentsFX({
|
|
64
66
|
fulfillments: opts.fulfillments,
|
|
65
67
|
});
|