@fabriktor/fx 0.0.81 → 0.0.83
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/core/pre-action.d.ts +10 -0
- package/dist/src/core/pre-action.js +23 -0
- package/dist/src/core/search.d.ts +1 -0
- package/dist/src/core/search.js +7 -4
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/jobs/jobs.d.ts +16 -0
- package/dist/src/jobs/jobs.js +71 -2
- package/package.json +1 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type PreActionCheckOptions<R extends string> = {
|
|
2
|
+
reason: R;
|
|
3
|
+
allowed: () => boolean | Promise<boolean>;
|
|
4
|
+
};
|
|
5
|
+
export declare class PreActionBlocked<R extends string = string> extends Error {
|
|
6
|
+
readonly reason: R;
|
|
7
|
+
constructor(reason: R);
|
|
8
|
+
}
|
|
9
|
+
export declare function runPreActionCheck<R extends string>(opts: PreActionCheckOptions<R>): Promise<void>;
|
|
10
|
+
export declare function isPreActionBlocked(err: unknown): err is PreActionBlocked;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Copyright (C) Fabriktor, Inc. 2025-present.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
4
|
+
// not use this file except in compliance with the License. You may obtain
|
|
5
|
+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
export class PreActionBlocked extends Error {
|
|
7
|
+
reason;
|
|
8
|
+
constructor(reason) {
|
|
9
|
+
super(`Pre-action check blocked: ${reason}.`);
|
|
10
|
+
this.name = new.target.name;
|
|
11
|
+
this.reason = reason;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
// runPreActionCheck blocks an action when its proactive guard does not allow it.
|
|
15
|
+
export async function runPreActionCheck(opts) {
|
|
16
|
+
if (await opts.allowed()) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
throw new PreActionBlocked(opts.reason);
|
|
20
|
+
}
|
|
21
|
+
export function isPreActionBlocked(err) {
|
|
22
|
+
return err instanceof PreActionBlocked;
|
|
23
|
+
}
|
|
@@ -32,6 +32,7 @@ export type SearchPage<T> = {
|
|
|
32
32
|
next_page: number;
|
|
33
33
|
done: boolean;
|
|
34
34
|
};
|
|
35
|
+
export declare function withOwnerQuery(owner_id: string, query?: SearchQuery): SearchQuery;
|
|
35
36
|
export declare function runOwnerSearch<T>(opts: RunOwnerSearchOptions<T>): Promise<SearchPage<T>>;
|
|
36
37
|
export declare function runSearch<T>(opts: RunSearchOptions<T>): Promise<SearchPage<T>>;
|
|
37
38
|
export declare function normalizeSearchPage(value?: number): number;
|
package/dist/src/core/search.js
CHANGED
|
@@ -6,14 +6,17 @@
|
|
|
6
6
|
import { OwnerIDKey, SearchPageKey, SearchPerPageKey, SearchQueryKey, SearchResultResultsKey, } from "@fabriktor/schema";
|
|
7
7
|
const firstSearchPage = 1;
|
|
8
8
|
const defaultSearchPerPage = 30;
|
|
9
|
+
export function withOwnerQuery(owner_id, query) {
|
|
10
|
+
return {
|
|
11
|
+
...query,
|
|
12
|
+
[OwnerIDKey]: owner_id,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
9
15
|
export async function runOwnerSearch(opts) {
|
|
10
16
|
const { owner_id, query, ...searchOpts } = opts;
|
|
11
17
|
return await runSearch({
|
|
12
18
|
...searchOpts,
|
|
13
|
-
query:
|
|
14
|
-
...query,
|
|
15
|
-
[OwnerIDKey]: owner_id,
|
|
16
|
-
},
|
|
19
|
+
query: withOwnerQuery(owner_id, query),
|
|
17
20
|
});
|
|
18
21
|
}
|
|
19
22
|
export async function runSearch(opts) {
|
package/dist/src/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export * from "./contacts/contacts.js";
|
|
|
6
6
|
export * from "./core/date.js";
|
|
7
7
|
export * from "./core/err.js";
|
|
8
8
|
export * from "./core/id.js";
|
|
9
|
+
export * from "./core/pre-action.js";
|
|
9
10
|
export * from "./core/pull.js";
|
|
10
11
|
export * from "./core/search.js";
|
|
11
12
|
export * from "./feed/feed.js";
|
package/dist/src/index.js
CHANGED
|
@@ -6,6 +6,7 @@ export * from "./contacts/contacts.js";
|
|
|
6
6
|
export * from "./core/date.js";
|
|
7
7
|
export * from "./core/err.js";
|
|
8
8
|
export * from "./core/id.js";
|
|
9
|
+
export * from "./core/pre-action.js";
|
|
9
10
|
export * from "./core/pull.js";
|
|
10
11
|
export * from "./core/search.js";
|
|
11
12
|
export * from "./feed/feed.js";
|
package/dist/src/jobs/jobs.d.ts
CHANGED
|
@@ -10,11 +10,22 @@ export declare const JobProgramCreateMode: {
|
|
|
10
10
|
readonly Immediate: "immediate";
|
|
11
11
|
};
|
|
12
12
|
export type JobProgramCreateMode = (typeof JobProgramCreateMode)[keyof typeof JobProgramCreateMode];
|
|
13
|
+
export declare const JobsPreActionReason: {
|
|
14
|
+
readonly LocationMemberAssignedOpenJob: "location_member_assigned_open_job";
|
|
15
|
+
readonly LocationHasOpenJobs: "location_has_open_jobs";
|
|
16
|
+
readonly LocationHasActivePrograms: "location_has_active_programs";
|
|
17
|
+
};
|
|
18
|
+
export type JobsPreActionReason = (typeof JobsPreActionReason)[keyof typeof JobsPreActionReason];
|
|
13
19
|
export type CreateLocationOptions = LocationInsertOptions;
|
|
14
20
|
export type UpdateLocationOptions = {
|
|
21
|
+
owner_id: string;
|
|
15
22
|
location: Location;
|
|
16
23
|
in: LocationReplaceInput;
|
|
17
24
|
};
|
|
25
|
+
export type ArchiveLocationOptions = {
|
|
26
|
+
owner_id: string;
|
|
27
|
+
location: Location;
|
|
28
|
+
};
|
|
18
29
|
export type SearchLocationsOptions = OwnerSearchOptions & {
|
|
19
30
|
is_archived?: boolean;
|
|
20
31
|
};
|
|
@@ -60,6 +71,7 @@ export type RemoveJobMemberOptions = {
|
|
|
60
71
|
member_id: string;
|
|
61
72
|
};
|
|
62
73
|
export type SetLocationMembersOptions = {
|
|
74
|
+
owner_id: string;
|
|
63
75
|
location: Location;
|
|
64
76
|
current_members: string[];
|
|
65
77
|
};
|
|
@@ -83,6 +95,7 @@ export type JobLifecycleControls = {
|
|
|
83
95
|
export interface JobsFXOperator {
|
|
84
96
|
createLocation(opts: CreateLocationOptions): Promise<Location>;
|
|
85
97
|
updateLocation(opts: UpdateLocationOptions): Promise<Location>;
|
|
98
|
+
archiveLocation(opts: ArchiveLocationOptions): Promise<Location>;
|
|
86
99
|
searchLocations(opts: SearchLocationsOptions): Promise<SearchLocationsPage>;
|
|
87
100
|
searchAccessibleLocations(opts?: SearchAccessibleLocationsOptions): Promise<SearchLocationsPage>;
|
|
88
101
|
createJobProgram(opts: CreateJobProgramOptions): Promise<CreateJobProgramResult>;
|
|
@@ -105,6 +118,7 @@ export declare class JobsFX implements JobsFXOperator {
|
|
|
105
118
|
constructor(opts: JobsFXOptions);
|
|
106
119
|
createLocation(opts: CreateLocationOptions): Promise<Location>;
|
|
107
120
|
updateLocation(opts: UpdateLocationOptions): Promise<Location>;
|
|
121
|
+
archiveLocation(opts: ArchiveLocationOptions): Promise<Location>;
|
|
108
122
|
searchLocations(opts: SearchLocationsOptions): Promise<SearchLocationsPage>;
|
|
109
123
|
searchAccessibleLocations(opts?: SearchAccessibleLocationsOptions): Promise<SearchLocationsPage>;
|
|
110
124
|
createJobProgram(opts: CreateJobProgramOptions): Promise<CreateJobProgramResult>;
|
|
@@ -120,6 +134,8 @@ export declare class JobsFX implements JobsFXOperator {
|
|
|
120
134
|
startJob(opts: JobLifecycleOptions): Promise<Job>;
|
|
121
135
|
completeJob(opts: JobLifecycleOptions): Promise<Job>;
|
|
122
136
|
cancelJob(opts: JobLifecycleOptions): Promise<Job>;
|
|
137
|
+
private checkLocationMemberRemoval;
|
|
138
|
+
private checkLocationArchive;
|
|
123
139
|
private setJobStatus;
|
|
124
140
|
private findLocation;
|
|
125
141
|
private findJobProgram;
|
package/dist/src/jobs/jobs.js
CHANGED
|
@@ -8,9 +8,11 @@ import { queryParams } from "@fabriktor/client";
|
|
|
8
8
|
import { isZeroDate, normalizeInputDate, zeroInputDate, } from "../core/date.js";
|
|
9
9
|
import { errRequired, errResolveFailed, errValidation, } from "../core/err.js";
|
|
10
10
|
import { requiredOperationID, resolvedID } from "../core/id.js";
|
|
11
|
-
import {
|
|
11
|
+
import { runPreActionCheck } from "../core/pre-action.js";
|
|
12
|
+
import { runOwnerSearch, runSearch, withOwnerQuery, } from "../core/search.js";
|
|
12
13
|
const msgJobIDRequired = "Job ID is required.";
|
|
13
14
|
const msgLocationIDRequired = "Location ID is required.";
|
|
15
|
+
const msgOwnerIDRequired = "Owner ID is required.";
|
|
14
16
|
const msgMemberIDRequired = "Member ID is required.";
|
|
15
17
|
const msgJobLocationMismatch = "Job location does not match the provided parent location.";
|
|
16
18
|
const msgProgramLocationMismatch = "Job program location does not match the provided parent location.";
|
|
@@ -44,6 +46,11 @@ export const JobProgramCreateMode = {
|
|
|
44
46
|
Scheduled: "scheduled",
|
|
45
47
|
Immediate: "immediate",
|
|
46
48
|
};
|
|
49
|
+
export const JobsPreActionReason = {
|
|
50
|
+
LocationMemberAssignedOpenJob: "location_member_assigned_open_job",
|
|
51
|
+
LocationHasOpenJobs: "location_has_open_jobs",
|
|
52
|
+
LocationHasActivePrograms: "location_has_active_programs",
|
|
53
|
+
};
|
|
47
54
|
export class JobsFX {
|
|
48
55
|
jobs;
|
|
49
56
|
now;
|
|
@@ -63,17 +70,33 @@ export class JobsFX {
|
|
|
63
70
|
return await this.findLocation(id);
|
|
64
71
|
}
|
|
65
72
|
async updateLocation(opts) {
|
|
73
|
+
const owner_id = requiredID(opts.owner_id, "owner_id", msgOwnerIDRequired);
|
|
66
74
|
const id = requiredID(opts.location.id, "location", msgLocationIDRequired);
|
|
67
75
|
const input = {
|
|
68
76
|
...opts.in,
|
|
69
77
|
...locationAddressInput(opts.location),
|
|
70
78
|
};
|
|
71
79
|
if (opts.in.current_members !== undefined) {
|
|
72
|
-
|
|
80
|
+
const current_members = normalizeIDs(opts.in.current_members);
|
|
81
|
+
const removed_members = removedIDs(opts.location.current_members, current_members);
|
|
82
|
+
if (removed_members.length > 0) {
|
|
83
|
+
await this.checkLocationMemberRemoval(owner_id, id, removed_members);
|
|
84
|
+
}
|
|
85
|
+
input.current_members = current_members;
|
|
73
86
|
}
|
|
74
87
|
await this.jobs.replaceOneLocation({ id, in: input });
|
|
75
88
|
return await this.findLocation(id);
|
|
76
89
|
}
|
|
90
|
+
async archiveLocation(opts) {
|
|
91
|
+
const owner_id = requiredID(opts.owner_id, "owner_id", msgOwnerIDRequired);
|
|
92
|
+
const id = requiredID(opts.location.id, "location", msgLocationIDRequired);
|
|
93
|
+
if (opts.location.is_archived) {
|
|
94
|
+
return opts.location;
|
|
95
|
+
}
|
|
96
|
+
await this.checkLocationArchive(owner_id, id);
|
|
97
|
+
await this.jobs.replaceOneLocation({ id, in: { is_archived: true } });
|
|
98
|
+
return await this.findLocation(id);
|
|
99
|
+
}
|
|
77
100
|
async searchLocations(opts) {
|
|
78
101
|
const { is_archived, ...searchOpts } = opts;
|
|
79
102
|
return await runOwnerSearch({
|
|
@@ -251,6 +274,7 @@ export class JobsFX {
|
|
|
251
274
|
}
|
|
252
275
|
async setLocationMembers(opts) {
|
|
253
276
|
return await this.updateLocation({
|
|
277
|
+
owner_id: opts.owner_id,
|
|
254
278
|
location: opts.location,
|
|
255
279
|
in: {
|
|
256
280
|
current_members: normalizeIDs(opts.current_members),
|
|
@@ -328,6 +352,47 @@ export class JobsFX {
|
|
|
328
352
|
}
|
|
329
353
|
return await this.setJobStatus(opts.job, Cancelled);
|
|
330
354
|
}
|
|
355
|
+
async checkLocationMemberRemoval(owner_id, location_id, removed_members) {
|
|
356
|
+
await runPreActionCheck({
|
|
357
|
+
reason: JobsPreActionReason.LocationMemberAssignedOpenJob,
|
|
358
|
+
allowed: async () => {
|
|
359
|
+
const out = await this.jobs.queryJobs({
|
|
360
|
+
query: withOwnerQuery(owner_id, queryParams({
|
|
361
|
+
location_id,
|
|
362
|
+
status: [Pending, Started],
|
|
363
|
+
current_members: removed_members,
|
|
364
|
+
})),
|
|
365
|
+
});
|
|
366
|
+
return (out.value?.length ?? 0) === 0;
|
|
367
|
+
},
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
async checkLocationArchive(owner_id, location_id) {
|
|
371
|
+
await runPreActionCheck({
|
|
372
|
+
reason: JobsPreActionReason.LocationHasOpenJobs,
|
|
373
|
+
allowed: async () => {
|
|
374
|
+
const out = await this.jobs.queryJobs({
|
|
375
|
+
query: withOwnerQuery(owner_id, queryParams({
|
|
376
|
+
location_id,
|
|
377
|
+
status: [Pending, Started],
|
|
378
|
+
})),
|
|
379
|
+
});
|
|
380
|
+
return (out.value?.length ?? 0) === 0;
|
|
381
|
+
},
|
|
382
|
+
});
|
|
383
|
+
await runPreActionCheck({
|
|
384
|
+
reason: JobsPreActionReason.LocationHasActivePrograms,
|
|
385
|
+
allowed: async () => {
|
|
386
|
+
const out = await this.jobs.queryJobPrograms({
|
|
387
|
+
query: withOwnerQuery(owner_id, queryParams({
|
|
388
|
+
job_location_id: location_id,
|
|
389
|
+
finished: false,
|
|
390
|
+
})),
|
|
391
|
+
});
|
|
392
|
+
return (out.value?.length ?? 0) === 0;
|
|
393
|
+
},
|
|
394
|
+
});
|
|
395
|
+
}
|
|
331
396
|
async setJobStatus(job, status) {
|
|
332
397
|
const id = requiredID(job.id, "job", msgJobIDRequired);
|
|
333
398
|
await this.jobs.replaceOneJob({
|
|
@@ -582,6 +647,10 @@ function normalizeIDs(ids) {
|
|
|
582
647
|
}
|
|
583
648
|
return out;
|
|
584
649
|
}
|
|
650
|
+
function removedIDs(before, after) {
|
|
651
|
+
const current = new Set(normalizeIDs(after));
|
|
652
|
+
return normalizeIDs(before).filter((id) => !current.has(id));
|
|
653
|
+
}
|
|
585
654
|
function unionIDs(a, b) {
|
|
586
655
|
return normalizeIDs([...a, ...b]);
|
|
587
656
|
}
|