@fabriktor/fx 0.0.81 → 0.0.82

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.
@@ -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
+ }
@@ -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";
@@ -10,11 +10,20 @@ 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 = {
15
21
  location: Location;
16
22
  in: LocationReplaceInput;
17
23
  };
24
+ export type ArchiveLocationOptions = {
25
+ location: Location;
26
+ };
18
27
  export type SearchLocationsOptions = OwnerSearchOptions & {
19
28
  is_archived?: boolean;
20
29
  };
@@ -83,6 +92,7 @@ export type JobLifecycleControls = {
83
92
  export interface JobsFXOperator {
84
93
  createLocation(opts: CreateLocationOptions): Promise<Location>;
85
94
  updateLocation(opts: UpdateLocationOptions): Promise<Location>;
95
+ archiveLocation(opts: ArchiveLocationOptions): Promise<Location>;
86
96
  searchLocations(opts: SearchLocationsOptions): Promise<SearchLocationsPage>;
87
97
  searchAccessibleLocations(opts?: SearchAccessibleLocationsOptions): Promise<SearchLocationsPage>;
88
98
  createJobProgram(opts: CreateJobProgramOptions): Promise<CreateJobProgramResult>;
@@ -105,6 +115,7 @@ export declare class JobsFX implements JobsFXOperator {
105
115
  constructor(opts: JobsFXOptions);
106
116
  createLocation(opts: CreateLocationOptions): Promise<Location>;
107
117
  updateLocation(opts: UpdateLocationOptions): Promise<Location>;
118
+ archiveLocation(opts: ArchiveLocationOptions): Promise<Location>;
108
119
  searchLocations(opts: SearchLocationsOptions): Promise<SearchLocationsPage>;
109
120
  searchAccessibleLocations(opts?: SearchAccessibleLocationsOptions): Promise<SearchLocationsPage>;
110
121
  createJobProgram(opts: CreateJobProgramOptions): Promise<CreateJobProgramResult>;
@@ -120,6 +131,8 @@ export declare class JobsFX implements JobsFXOperator {
120
131
  startJob(opts: JobLifecycleOptions): Promise<Job>;
121
132
  completeJob(opts: JobLifecycleOptions): Promise<Job>;
122
133
  cancelJob(opts: JobLifecycleOptions): Promise<Job>;
134
+ private checkLocationMemberRemoval;
135
+ private checkLocationArchive;
123
136
  private setJobStatus;
124
137
  private findLocation;
125
138
  private findJobProgram;
@@ -8,6 +8,7 @@ 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 { runPreActionCheck } from "../core/pre-action.js";
11
12
  import { runOwnerSearch, runSearch, } from "../core/search.js";
12
13
  const msgJobIDRequired = "Job ID is required.";
13
14
  const msgLocationIDRequired = "Location ID is required.";
@@ -44,6 +45,11 @@ export const JobProgramCreateMode = {
44
45
  Scheduled: "scheduled",
45
46
  Immediate: "immediate",
46
47
  };
48
+ export const JobsPreActionReason = {
49
+ LocationMemberAssignedOpenJob: "location_member_assigned_open_job",
50
+ LocationHasOpenJobs: "location_has_open_jobs",
51
+ LocationHasActivePrograms: "location_has_active_programs",
52
+ };
47
53
  export class JobsFX {
48
54
  jobs;
49
55
  now;
@@ -69,11 +75,25 @@ export class JobsFX {
69
75
  ...locationAddressInput(opts.location),
70
76
  };
71
77
  if (opts.in.current_members !== undefined) {
72
- input.current_members = normalizeIDs(opts.in.current_members);
78
+ const current_members = normalizeIDs(opts.in.current_members);
79
+ const removed_members = removedIDs(opts.location.current_members, current_members);
80
+ if (removed_members.length > 0) {
81
+ await this.checkLocationMemberRemoval(id, removed_members);
82
+ }
83
+ input.current_members = current_members;
73
84
  }
74
85
  await this.jobs.replaceOneLocation({ id, in: input });
75
86
  return await this.findLocation(id);
76
87
  }
88
+ async archiveLocation(opts) {
89
+ const id = requiredID(opts.location.id, "location", msgLocationIDRequired);
90
+ if (opts.location.is_archived) {
91
+ return opts.location;
92
+ }
93
+ await this.checkLocationArchive(id);
94
+ await this.jobs.replaceOneLocation({ id, in: { is_archived: true } });
95
+ return await this.findLocation(id);
96
+ }
77
97
  async searchLocations(opts) {
78
98
  const { is_archived, ...searchOpts } = opts;
79
99
  return await runOwnerSearch({
@@ -328,6 +348,47 @@ export class JobsFX {
328
348
  }
329
349
  return await this.setJobStatus(opts.job, Cancelled);
330
350
  }
351
+ async checkLocationMemberRemoval(location_id, removed_members) {
352
+ await runPreActionCheck({
353
+ reason: JobsPreActionReason.LocationMemberAssignedOpenJob,
354
+ allowed: async () => {
355
+ const out = await this.jobs.queryJobs({
356
+ query: queryParams({
357
+ location_id,
358
+ status: [Pending, Started],
359
+ current_members: removed_members,
360
+ }),
361
+ });
362
+ return (out.value?.length ?? 0) === 0;
363
+ },
364
+ });
365
+ }
366
+ async checkLocationArchive(location_id) {
367
+ await runPreActionCheck({
368
+ reason: JobsPreActionReason.LocationHasOpenJobs,
369
+ allowed: async () => {
370
+ const out = await this.jobs.queryJobs({
371
+ query: queryParams({
372
+ location_id,
373
+ status: [Pending, Started],
374
+ }),
375
+ });
376
+ return (out.value?.length ?? 0) === 0;
377
+ },
378
+ });
379
+ await runPreActionCheck({
380
+ reason: JobsPreActionReason.LocationHasActivePrograms,
381
+ allowed: async () => {
382
+ const out = await this.jobs.queryJobPrograms({
383
+ query: queryParams({
384
+ job_location_id: location_id,
385
+ finished: false,
386
+ }),
387
+ });
388
+ return (out.value?.length ?? 0) === 0;
389
+ },
390
+ });
391
+ }
331
392
  async setJobStatus(job, status) {
332
393
  const id = requiredID(job.id, "job", msgJobIDRequired);
333
394
  await this.jobs.replaceOneJob({
@@ -582,6 +643,10 @@ function normalizeIDs(ids) {
582
643
  }
583
644
  return out;
584
645
  }
646
+ function removedIDs(before, after) {
647
+ const current = new Set(normalizeIDs(after));
648
+ return normalizeIDs(before).filter((id) => !current.has(id));
649
+ }
585
650
  function unionIDs(a, b) {
586
651
  return normalizeIDs([...a, ...b]);
587
652
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.81",
3
+ "version": "0.0.82",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {