@fabriktor/fx 0.0.86 → 0.0.87
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 +55 -1
- package/dist/src/billable/billable.js +151 -2
- package/dist/src/contacts/contacts.d.ts +17 -1
- package/dist/src/contacts/contacts.js +104 -1
- package/dist/src/core/search.d.ts +8 -0
- package/dist/src/core/search.js +66 -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,37 @@ 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
|
+
query?: SearchQuery;
|
|
82
114
|
};
|
|
83
115
|
export type GenerateFixedEstimateOptions = {
|
|
84
116
|
in: GenerateBillPayload;
|
|
@@ -105,6 +137,7 @@ export declare const BillablePreActionReason: {
|
|
|
105
137
|
};
|
|
106
138
|
export type BillablePreActionReason = (typeof BillablePreActionReason)[keyof typeof BillablePreActionReason];
|
|
107
139
|
export interface BillableFXOperator {
|
|
140
|
+
loadReadContext(opts: LoadBillableReadContextOptions): Promise<BillableReadContext>;
|
|
108
141
|
createBill(opts: CreateBillOptions): Promise<Bill>;
|
|
109
142
|
replaceBill(opts: ReplaceBillOptions): Promise<Bill>;
|
|
110
143
|
searchBills(opts: SearchBillsOptions): Promise<SearchBillsPage>;
|
|
@@ -114,21 +147,26 @@ export interface BillableFXOperator {
|
|
|
114
147
|
createCharge(opts: CreateChargeOptions): Promise<Charge>;
|
|
115
148
|
replaceCharge(opts: ReplaceChargeOptions): Promise<Charge>;
|
|
116
149
|
searchCharges(opts: SearchChargesOptions): Promise<SearchChargesPage>;
|
|
150
|
+
searchReadableCharges(opts: BillableReadSearchOptions): Promise<SearchChargesPage>;
|
|
117
151
|
createEquipment(opts: CreateEquipmentOptions): Promise<Equipment>;
|
|
118
152
|
replaceEquipment(opts: ReplaceEquipmentOptions): Promise<Equipment>;
|
|
119
153
|
searchEquipments(opts: SearchEquipmentsOptions): Promise<SearchEquipmentsPage>;
|
|
154
|
+
searchReadableEquipments(opts: BillableReadSearchOptions): Promise<SearchEquipmentsPage>;
|
|
120
155
|
createItem(opts: CreateItemOptions): Promise<Item>;
|
|
121
156
|
replaceItem(opts: ReplaceItemOptions): Promise<Item>;
|
|
122
157
|
searchItems(opts: SearchItemsOptions): Promise<SearchItemsPage>;
|
|
158
|
+
searchReadableItems(opts: BillableReadSearchOptions): Promise<SearchItemsPage>;
|
|
123
159
|
createLaborRate(opts: CreateLaborRateOptions): Promise<LaborRate>;
|
|
124
160
|
replaceLaborRate(opts: ReplaceLaborRateOptions): Promise<LaborRate>;
|
|
125
161
|
searchLaborRates(opts: SearchLaborRatesOptions): Promise<SearchLaborRatesPage>;
|
|
126
162
|
createMeterReading(opts: CreateMeterReadingOptions): Promise<MeterReading>;
|
|
127
163
|
replaceMeterReading(opts: ReplaceMeterReadingOptions): Promise<MeterReading>;
|
|
128
164
|
searchMeterReadings(opts: SearchMeterReadingsOptions): Promise<SearchMeterReadingsPage>;
|
|
165
|
+
searchReadableMeterReadings(opts: BillableReadSearchOptions): Promise<SearchMeterReadingsPage>;
|
|
129
166
|
createQuantifier(opts: CreateQuantifierOptions): Promise<Quantifier>;
|
|
130
167
|
replaceQuantifier(opts: ReplaceQuantifierOptions): Promise<Quantifier>;
|
|
131
168
|
searchQuantifiers(opts: SearchQuantifiersOptions): Promise<SearchQuantifiersPage>;
|
|
169
|
+
searchReadableQuantifiers(opts: BillableReadSearchOptions): Promise<SearchQuantifiersPage>;
|
|
132
170
|
createTax(opts: CreateTaxOptions): Promise<Tax>;
|
|
133
171
|
replaceTax(opts: ReplaceTaxOptions): Promise<Tax>;
|
|
134
172
|
searchTaxes(opts: SearchTaxesOptions): Promise<SearchTaxesPage>;
|
|
@@ -141,6 +179,7 @@ export interface BillableFXOperator {
|
|
|
141
179
|
createWarehouse(opts: CreateWarehouseOptions): Promise<Warehouse>;
|
|
142
180
|
replaceWarehouse(opts: ReplaceWarehouseOptions): Promise<Warehouse>;
|
|
143
181
|
searchWarehouses(opts: SearchWarehousesOptions): Promise<SearchWarehousesPage>;
|
|
182
|
+
searchReadableWarehouses(opts: BillableReadSearchOptions): Promise<SearchWarehousesPage>;
|
|
144
183
|
generateFixedEstimate(opts: GenerateFixedEstimateOptions): Promise<Bill>;
|
|
145
184
|
acceptFixedEstimate(opts: AcceptFixedEstimateOptions): Promise<Bill>;
|
|
146
185
|
generateDynamicEstimate(opts: GenerateDynamicEstimateOptions): Promise<Bill>;
|
|
@@ -151,7 +190,9 @@ export interface BillableFXOperator {
|
|
|
151
190
|
}
|
|
152
191
|
export declare class BillableFX implements BillableFXOperator {
|
|
153
192
|
private billable;
|
|
193
|
+
private contacts_fx;
|
|
154
194
|
constructor(opts: BillableFXOptions);
|
|
195
|
+
loadReadContext(opts: LoadBillableReadContextOptions): Promise<BillableReadContext>;
|
|
155
196
|
createBill(opts: CreateBillOptions): Promise<Bill>;
|
|
156
197
|
replaceBill(opts: ReplaceBillOptions): Promise<Bill>;
|
|
157
198
|
searchBills(opts: SearchBillsOptions): Promise<SearchBillsPage>;
|
|
@@ -161,21 +202,26 @@ export declare class BillableFX implements BillableFXOperator {
|
|
|
161
202
|
createCharge(opts: CreateChargeOptions): Promise<Charge>;
|
|
162
203
|
replaceCharge(opts: ReplaceChargeOptions): Promise<Charge>;
|
|
163
204
|
searchCharges(opts: SearchChargesOptions): Promise<SearchChargesPage>;
|
|
205
|
+
searchReadableCharges(opts: BillableReadSearchOptions): Promise<SearchChargesPage>;
|
|
164
206
|
createEquipment(opts: CreateEquipmentOptions): Promise<Equipment>;
|
|
165
207
|
replaceEquipment(opts: ReplaceEquipmentOptions): Promise<Equipment>;
|
|
166
208
|
searchEquipments(opts: SearchEquipmentsOptions): Promise<SearchEquipmentsPage>;
|
|
209
|
+
searchReadableEquipments(opts: BillableReadSearchOptions): Promise<SearchEquipmentsPage>;
|
|
167
210
|
createItem(opts: CreateItemOptions): Promise<Item>;
|
|
168
211
|
replaceItem(opts: ReplaceItemOptions): Promise<Item>;
|
|
169
212
|
searchItems(opts: SearchItemsOptions): Promise<SearchItemsPage>;
|
|
213
|
+
searchReadableItems(opts: BillableReadSearchOptions): Promise<SearchItemsPage>;
|
|
170
214
|
createLaborRate(opts: CreateLaborRateOptions): Promise<LaborRate>;
|
|
171
215
|
replaceLaborRate(opts: ReplaceLaborRateOptions): Promise<LaborRate>;
|
|
172
216
|
searchLaborRates(opts: SearchLaborRatesOptions): Promise<SearchLaborRatesPage>;
|
|
173
217
|
createMeterReading(opts: CreateMeterReadingOptions): Promise<MeterReading>;
|
|
174
218
|
replaceMeterReading(opts: ReplaceMeterReadingOptions): Promise<MeterReading>;
|
|
175
219
|
searchMeterReadings(opts: SearchMeterReadingsOptions): Promise<SearchMeterReadingsPage>;
|
|
220
|
+
searchReadableMeterReadings(opts: BillableReadSearchOptions): Promise<SearchMeterReadingsPage>;
|
|
176
221
|
createQuantifier(opts: CreateQuantifierOptions): Promise<Quantifier>;
|
|
177
222
|
replaceQuantifier(opts: ReplaceQuantifierOptions): Promise<Quantifier>;
|
|
178
223
|
searchQuantifiers(opts: SearchQuantifiersOptions): Promise<SearchQuantifiersPage>;
|
|
224
|
+
searchReadableQuantifiers(opts: BillableReadSearchOptions): Promise<SearchQuantifiersPage>;
|
|
179
225
|
createTax(opts: CreateTaxOptions): Promise<Tax>;
|
|
180
226
|
replaceTax(opts: ReplaceTaxOptions): Promise<Tax>;
|
|
181
227
|
searchTaxes(opts: SearchTaxesOptions): Promise<SearchTaxesPage>;
|
|
@@ -188,6 +234,7 @@ export declare class BillableFX implements BillableFXOperator {
|
|
|
188
234
|
createWarehouse(opts: CreateWarehouseOptions): Promise<Warehouse>;
|
|
189
235
|
replaceWarehouse(opts: ReplaceWarehouseOptions): Promise<Warehouse>;
|
|
190
236
|
searchWarehouses(opts: SearchWarehousesOptions): Promise<SearchWarehousesPage>;
|
|
237
|
+
searchReadableWarehouses(opts: BillableReadSearchOptions): Promise<SearchWarehousesPage>;
|
|
191
238
|
generateFixedEstimate(opts: GenerateFixedEstimateOptions): Promise<Bill>;
|
|
192
239
|
acceptFixedEstimate(opts: AcceptFixedEstimateOptions): Promise<Bill>;
|
|
193
240
|
generateDynamicEstimate(opts: GenerateDynamicEstimateOptions): Promise<Bill>;
|
|
@@ -197,6 +244,7 @@ export declare class BillableFX implements BillableFXOperator {
|
|
|
197
244
|
vendorInvoiceFromBill(opts: VendorInvoiceFromBillOptions): Promise<VendorInvoice>;
|
|
198
245
|
private generateBill;
|
|
199
246
|
private checkVendorInvoiceNumber;
|
|
247
|
+
private searchReadable;
|
|
200
248
|
private findBill;
|
|
201
249
|
private findBillableFolder;
|
|
202
250
|
private findCharge;
|
|
@@ -210,5 +258,11 @@ export declare class BillableFX implements BillableFXOperator {
|
|
|
210
258
|
private findVendorInvoice;
|
|
211
259
|
private findWarehouse;
|
|
212
260
|
}
|
|
261
|
+
export declare function billableCanReadResource(context: BillableReadContext, resource: BillableReadResource): boolean;
|
|
262
|
+
export declare function billableCanCreateResource(context: BillableReadContext, resource: BillableReadResource): boolean;
|
|
263
|
+
export declare function billableOwnsRecord(context: BillableReadContext, record: {
|
|
264
|
+
owner_id: string;
|
|
265
|
+
} | null | undefined): boolean;
|
|
266
|
+
export declare function billableCatalogOwnerIDs(context: BillableReadContext): string[];
|
|
213
267
|
export declare function newBillableFX(opts: BillableFXOptions): BillableFX;
|
|
214
268
|
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 { 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,13 @@ export class BillableFX {
|
|
|
104
139
|
search: (searchOpts) => this.billable.searchManyCharges(searchOpts),
|
|
105
140
|
});
|
|
106
141
|
}
|
|
142
|
+
async searchReadableCharges(opts) {
|
|
143
|
+
return await this.searchReadable({
|
|
144
|
+
...opts,
|
|
145
|
+
allowed_owner_ids: activityOwnerIDs(opts.context),
|
|
146
|
+
search: (searchOpts) => this.billable.searchManyCharges(searchOpts),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
107
149
|
async createEquipment(opts) {
|
|
108
150
|
const out = await this.billable.insertOneEquipment(opts);
|
|
109
151
|
const id = requiredOperationID(out, msgCreatedEquipmentIDMissing);
|
|
@@ -119,6 +161,13 @@ export class BillableFX {
|
|
|
119
161
|
search: (searchOpts) => this.billable.searchManyEquipments(searchOpts),
|
|
120
162
|
});
|
|
121
163
|
}
|
|
164
|
+
async searchReadableEquipments(opts) {
|
|
165
|
+
return await this.searchReadable({
|
|
166
|
+
...opts,
|
|
167
|
+
allowed_owner_ids: catalogOwnerIDs(opts.context),
|
|
168
|
+
search: (searchOpts) => this.billable.searchManyEquipments(searchOpts),
|
|
169
|
+
});
|
|
170
|
+
}
|
|
122
171
|
async createItem(opts) {
|
|
123
172
|
const out = await this.billable.insertOneItem(opts);
|
|
124
173
|
const id = requiredOperationID(out, msgCreatedItemIDMissing);
|
|
@@ -134,6 +183,13 @@ export class BillableFX {
|
|
|
134
183
|
search: (searchOpts) => this.billable.searchManyItems(searchOpts),
|
|
135
184
|
});
|
|
136
185
|
}
|
|
186
|
+
async searchReadableItems(opts) {
|
|
187
|
+
return await this.searchReadable({
|
|
188
|
+
...opts,
|
|
189
|
+
allowed_owner_ids: catalogOwnerIDs(opts.context),
|
|
190
|
+
search: (searchOpts) => this.billable.searchManyItems(searchOpts),
|
|
191
|
+
});
|
|
192
|
+
}
|
|
137
193
|
async createLaborRate(opts) {
|
|
138
194
|
const out = await this.billable.insertOneLaborRate(opts);
|
|
139
195
|
const id = requiredOperationID(out, msgCreatedLaborRateIDMissing);
|
|
@@ -164,6 +220,13 @@ export class BillableFX {
|
|
|
164
220
|
search: (searchOpts) => this.billable.searchManyMeterReadings(searchOpts),
|
|
165
221
|
});
|
|
166
222
|
}
|
|
223
|
+
async searchReadableMeterReadings(opts) {
|
|
224
|
+
return await this.searchReadable({
|
|
225
|
+
...opts,
|
|
226
|
+
allowed_owner_ids: activityOwnerIDs(opts.context),
|
|
227
|
+
search: (searchOpts) => this.billable.searchManyMeterReadings(searchOpts),
|
|
228
|
+
});
|
|
229
|
+
}
|
|
167
230
|
async createQuantifier(opts) {
|
|
168
231
|
const out = await this.billable.insertOneQuantifier(opts);
|
|
169
232
|
const id = requiredOperationID(out, msgCreatedQuantifierIDMissing);
|
|
@@ -179,6 +242,13 @@ export class BillableFX {
|
|
|
179
242
|
search: (searchOpts) => this.billable.searchManyQuantifiers(searchOpts),
|
|
180
243
|
});
|
|
181
244
|
}
|
|
245
|
+
async searchReadableQuantifiers(opts) {
|
|
246
|
+
return await this.searchReadable({
|
|
247
|
+
...opts,
|
|
248
|
+
allowed_owner_ids: catalogOwnerIDs(opts.context),
|
|
249
|
+
search: (searchOpts) => this.billable.searchManyQuantifiers(searchOpts),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
182
252
|
async createTax(opts) {
|
|
183
253
|
const out = await this.billable.insertOneTax(opts);
|
|
184
254
|
const id = requiredOperationID(out, msgCreatedTaxIDMissing);
|
|
@@ -253,6 +323,13 @@ export class BillableFX {
|
|
|
253
323
|
search: (searchOpts) => this.billable.searchManyWarehouses(searchOpts),
|
|
254
324
|
});
|
|
255
325
|
}
|
|
326
|
+
async searchReadableWarehouses(opts) {
|
|
327
|
+
return await this.searchReadable({
|
|
328
|
+
...opts,
|
|
329
|
+
allowed_owner_ids: catalogOwnerIDs(opts.context),
|
|
330
|
+
search: (searchOpts) => this.billable.searchManyWarehouses(searchOpts),
|
|
331
|
+
});
|
|
332
|
+
}
|
|
256
333
|
async generateFixedEstimate(opts) {
|
|
257
334
|
return await this.generateBill(billWith(opts.in, {
|
|
258
335
|
type: BillTypeFixed,
|
|
@@ -311,6 +388,20 @@ export class BillableFX {
|
|
|
311
388
|
},
|
|
312
389
|
});
|
|
313
390
|
}
|
|
391
|
+
async searchReadable(opts) {
|
|
392
|
+
const { context: _context, owner_ids: requested_owner_ids, allowed_owner_ids, search, query, ...searchOpts } = opts;
|
|
393
|
+
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
|
+
});
|
|
400
|
+
return {
|
|
401
|
+
...page,
|
|
402
|
+
results: [...page.results].sort(compareCreatedAtDescending),
|
|
403
|
+
};
|
|
404
|
+
}
|
|
314
405
|
async findBill(id) {
|
|
315
406
|
const out = await this.billable.findOneBill({
|
|
316
407
|
id,
|
|
@@ -468,6 +559,64 @@ export class BillableFX {
|
|
|
468
559
|
return value;
|
|
469
560
|
}
|
|
470
561
|
}
|
|
562
|
+
export function billableCanReadResource(context, resource) {
|
|
563
|
+
if (context.active_mode !== UserTypeEmployee) {
|
|
564
|
+
return true;
|
|
565
|
+
}
|
|
566
|
+
return employeeReadableResources.has(resource);
|
|
567
|
+
}
|
|
568
|
+
export function billableCanCreateResource(context, resource) {
|
|
569
|
+
if (context.active_mode === UserTypeEmployer) {
|
|
570
|
+
return true;
|
|
571
|
+
}
|
|
572
|
+
return (context.active_mode === UserTypeEmployee &&
|
|
573
|
+
(resource === BillableReadResource.Charge ||
|
|
574
|
+
resource === BillableReadResource.MeterReading));
|
|
575
|
+
}
|
|
576
|
+
export function billableOwnsRecord(context, record) {
|
|
577
|
+
return record !== null && record !== undefined && record.owner_id === context.owner_id;
|
|
578
|
+
}
|
|
579
|
+
export function billableCatalogOwnerIDs(context) {
|
|
580
|
+
return catalogOwnerIDs(context);
|
|
581
|
+
}
|
|
582
|
+
const employeeReadableResources = new Set([
|
|
583
|
+
BillableReadResource.Item,
|
|
584
|
+
BillableReadResource.Equipment,
|
|
585
|
+
BillableReadResource.Charge,
|
|
586
|
+
BillableReadResource.Warehouse,
|
|
587
|
+
BillableReadResource.Quantifier,
|
|
588
|
+
BillableReadResource.MeterReading,
|
|
589
|
+
]);
|
|
590
|
+
function catalogOwnerIDs(context) {
|
|
591
|
+
if (context.active_mode === UserTypeEmployee) {
|
|
592
|
+
return uniqueIDs(context.employment.active_employer_ids);
|
|
593
|
+
}
|
|
594
|
+
return uniqueIDs([context.owner_id]);
|
|
595
|
+
}
|
|
596
|
+
function activityOwnerIDs(context) {
|
|
597
|
+
if (context.active_mode === UserTypeEmployer) {
|
|
598
|
+
return uniqueIDs([context.owner_id, ...context.employment.related_employee_ids]);
|
|
599
|
+
}
|
|
600
|
+
return uniqueIDs([context.owner_id]);
|
|
601
|
+
}
|
|
602
|
+
function restrictOwnerIDs(allowed, requested) {
|
|
603
|
+
const allowed_ids = uniqueIDs(allowed);
|
|
604
|
+
if (requested === undefined) {
|
|
605
|
+
return allowed_ids;
|
|
606
|
+
}
|
|
607
|
+
const allowed_set = new Set(allowed_ids);
|
|
608
|
+
return uniqueIDs(requested).filter((id) => allowed_set.has(id));
|
|
609
|
+
}
|
|
610
|
+
function uniqueIDs(ids) {
|
|
611
|
+
return [...new Set([...ids].map((id) => id.trim()).filter((id) => id !== ""))];
|
|
612
|
+
}
|
|
613
|
+
function compareCreatedAtDescending(left, right) {
|
|
614
|
+
return timestamp(right.created_at) - timestamp(left.created_at);
|
|
615
|
+
}
|
|
616
|
+
function timestamp(value) {
|
|
617
|
+
const out = globalThis.Date.parse(value);
|
|
618
|
+
return Number.isFinite(out) ? out : 0;
|
|
619
|
+
}
|
|
471
620
|
export function newBillableFX(opts) {
|
|
472
621
|
return new BillableFX(opts);
|
|
473
622
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ContactsOperator } from "@fabriktor/client";
|
|
1
|
+
import type { ContactsOperator, UsersOperator } from "@fabriktor/client";
|
|
2
2
|
import { Friend, Hired, Manager, Na, OldHire, type Contact, type ID, type OperationResult } from "@fabriktor/schema";
|
|
3
3
|
type ContactInsertOptions = Parameters<ContactsOperator["insertOneContact"]>[0];
|
|
4
4
|
type ContactReplaceOptions = Parameters<ContactsOperator["replaceOneContact"]>[0];
|
|
@@ -8,10 +8,21 @@ 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
|
+
current_employee_ids: string[];
|
|
24
|
+
related_employee_ids: string[];
|
|
25
|
+
};
|
|
15
26
|
export type ContactIndex = {
|
|
16
27
|
contacts: Contact[];
|
|
17
28
|
peer_ids: Set<string>;
|
|
@@ -49,6 +60,7 @@ export interface ContactsFXOperator {
|
|
|
49
60
|
createContact(opts: CreateContactOptions): Promise<Contact>;
|
|
50
61
|
replaceContact(opts: ReplaceContactOptions): Promise<Contact>;
|
|
51
62
|
loadContactIndex(opts: LoadContactIndexOptions): Promise<ContactIndex>;
|
|
63
|
+
employmentOwnerScope(opts: EmploymentOwnerScopeOptions): Promise<EmploymentOwnerScope>;
|
|
52
64
|
startPresence(opts: StartPresenceOptions): Promise<PresenceSession>;
|
|
53
65
|
sendFriendRequest(opts: SendFriendRequestOptions): Promise<OperationResult>;
|
|
54
66
|
requestRelationUpgrade(opts: RequestRelationUpgradeOptions): Promise<OperationResult>;
|
|
@@ -59,10 +71,12 @@ export interface ContactsFXOperator {
|
|
|
59
71
|
}
|
|
60
72
|
export declare class ContactsFX implements ContactsFXOperator {
|
|
61
73
|
private contacts;
|
|
74
|
+
private users;
|
|
62
75
|
constructor(opts: ContactsFXOptions);
|
|
63
76
|
createContact(opts: CreateContactOptions): Promise<Contact>;
|
|
64
77
|
replaceContact(opts: ReplaceContactOptions): Promise<Contact>;
|
|
65
78
|
loadContactIndex(opts: LoadContactIndexOptions): Promise<ContactIndex>;
|
|
79
|
+
employmentOwnerScope(opts: EmploymentOwnerScopeOptions): Promise<EmploymentOwnerScope>;
|
|
66
80
|
startPresence(opts: StartPresenceOptions): Promise<PresenceSession>;
|
|
67
81
|
sendFriendRequest(opts: SendFriendRequestOptions): Promise<OperationResult>;
|
|
68
82
|
requestRelationUpgrade(opts: RequestRelationUpgradeOptions): Promise<OperationResult>;
|
|
@@ -71,7 +85,9 @@ export declare class ContactsFX implements ContactsFXOperator {
|
|
|
71
85
|
rejectContactRequest(opts: RejectContactRequestOptions): Promise<OperationResult>;
|
|
72
86
|
endHireRelation(opts: EndHireRelationOptions): Promise<OperationResult>;
|
|
73
87
|
private replaceContactResult;
|
|
88
|
+
private usersByIDs;
|
|
74
89
|
private findContact;
|
|
75
90
|
}
|
|
76
91
|
export declare function newContactsFX(opts: ContactsFXOptions): ContactsFX;
|
|
92
|
+
export declare function emptyEmploymentOwnerScope(): EmploymentOwnerScope;
|
|
77
93
|
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,66 @@ 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
|
+
current_employee_ids: [...current_employee_ids],
|
|
116
|
+
related_employee_ids: [...related_employee_ids],
|
|
117
|
+
};
|
|
118
|
+
}
|
|
54
119
|
async startPresence(opts) {
|
|
55
120
|
const initial = await this.contacts.signal();
|
|
56
121
|
opts.on_active(initial.value ?? []);
|
|
@@ -175,6 +240,27 @@ export class ContactsFX {
|
|
|
175
240
|
},
|
|
176
241
|
});
|
|
177
242
|
}
|
|
243
|
+
async usersByIDs(uid, ids) {
|
|
244
|
+
const values = uniqueIDs(ids);
|
|
245
|
+
if (values.length === 0) {
|
|
246
|
+
return [];
|
|
247
|
+
}
|
|
248
|
+
const pages = await Promise.all(chunks(values, employmentUserBatchSize).map((chunk) => runSearch({
|
|
249
|
+
per_page: chunk.length,
|
|
250
|
+
query: { [IDKey]: chunk },
|
|
251
|
+
search: (searchOpts) => this.users.searchManyUsers({
|
|
252
|
+
...searchOpts,
|
|
253
|
+
uid,
|
|
254
|
+
}),
|
|
255
|
+
})));
|
|
256
|
+
const users = new Map();
|
|
257
|
+
for (const page of pages) {
|
|
258
|
+
for (const user of page.results) {
|
|
259
|
+
users.set(user.id, user);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return [...users.values()];
|
|
263
|
+
}
|
|
178
264
|
async findContact(id) {
|
|
179
265
|
const out = await this.contacts.findOneContact({
|
|
180
266
|
id,
|
|
@@ -192,6 +278,23 @@ export class ContactsFX {
|
|
|
192
278
|
export function newContactsFX(opts) {
|
|
193
279
|
return new ContactsFX(opts);
|
|
194
280
|
}
|
|
281
|
+
export function emptyEmploymentOwnerScope() {
|
|
282
|
+
return {
|
|
283
|
+
active_employer_ids: [],
|
|
284
|
+
current_employee_ids: [],
|
|
285
|
+
related_employee_ids: [],
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function uniqueIDs(ids) {
|
|
289
|
+
return [...new Set([...ids].map((id) => id.trim()).filter((id) => id !== ""))];
|
|
290
|
+
}
|
|
291
|
+
function chunks(values, size) {
|
|
292
|
+
const out = [];
|
|
293
|
+
for (let i = 0; i < values.length; i += size) {
|
|
294
|
+
out.push(values.slice(i, i + size));
|
|
295
|
+
}
|
|
296
|
+
return out;
|
|
297
|
+
}
|
|
195
298
|
function contactPeerID(contact, owner_id) {
|
|
196
299
|
const contact_owner_id = contact.owner_id.trim();
|
|
197
300
|
const contact_id = contact.contact_id.trim();
|
|
@@ -22,6 +22,11 @@ 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
|
+
};
|
|
25
30
|
export type SearchPage<T> = {
|
|
26
31
|
results: T[];
|
|
27
32
|
page: number;
|
|
@@ -34,6 +39,9 @@ export type SearchPage<T> = {
|
|
|
34
39
|
};
|
|
35
40
|
export declare function withOwnerQuery(owner_id: string, query?: SearchQuery): SearchQuery;
|
|
36
41
|
export declare function runOwnerSearch<T>(opts: RunOwnerSearchOptions<T>): Promise<SearchPage<T>>;
|
|
42
|
+
export declare function runOwnerScopeSearch<T extends {
|
|
43
|
+
id: string;
|
|
44
|
+
}>(opts: OwnerScopeSearchOptions<T>): Promise<SearchPage<T>>;
|
|
37
45
|
export declare function runSearch<T>(opts: RunSearchOptions<T>): Promise<SearchPage<T>>;
|
|
38
46
|
export declare function normalizeSearchPage(value?: number): number;
|
|
39
47
|
export declare function normalizeSearchPerPage(value?: number): number;
|
package/dist/src/core/search.js
CHANGED
|
@@ -19,6 +19,41 @@ 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
|
+
}
|
|
22
57
|
export async function runSearch(opts) {
|
|
23
58
|
const page = normalizeSearchPage(opts.page);
|
|
24
59
|
const per_page = normalizeSearchPerPage(opts.per_page);
|
|
@@ -95,3 +130,34 @@ function hasMoreSearchResults(opts) {
|
|
|
95
130
|
}
|
|
96
131
|
return opts.result_count >= opts.per_page;
|
|
97
132
|
}
|
|
133
|
+
function uniqueOwnerIDs(ids) {
|
|
134
|
+
return [...new Set([...ids].map((id) => id.trim()).filter((id) => id !== ""))];
|
|
135
|
+
}
|
|
136
|
+
function emptySearchPage(page, per_page) {
|
|
137
|
+
return {
|
|
138
|
+
results: [],
|
|
139
|
+
page,
|
|
140
|
+
per_page,
|
|
141
|
+
total_count: 0,
|
|
142
|
+
last_page: page,
|
|
143
|
+
has_more: false,
|
|
144
|
+
next_page: page,
|
|
145
|
+
done: true,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function sumDefined(values) {
|
|
149
|
+
if (values.some((value) => value === undefined)) {
|
|
150
|
+
return undefined;
|
|
151
|
+
}
|
|
152
|
+
let sum = 0;
|
|
153
|
+
for (const value of values) {
|
|
154
|
+
sum += value ?? 0;
|
|
155
|
+
}
|
|
156
|
+
return sum;
|
|
157
|
+
}
|
|
158
|
+
function maxDefined(values) {
|
|
159
|
+
if (values.some((value) => value === undefined) || values.length === 0) {
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
return Math.max(...values.map((value) => value ?? 0));
|
|
163
|
+
}
|
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
|
});
|