@fabriktor/fx 0.0.82 → 0.0.84

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.
@@ -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;
@@ -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) {
@@ -18,10 +18,12 @@ export declare const JobsPreActionReason: {
18
18
  export type JobsPreActionReason = (typeof JobsPreActionReason)[keyof typeof JobsPreActionReason];
19
19
  export type CreateLocationOptions = LocationInsertOptions;
20
20
  export type UpdateLocationOptions = {
21
+ owner_id: string;
21
22
  location: Location;
22
23
  in: LocationReplaceInput;
23
24
  };
24
25
  export type ArchiveLocationOptions = {
26
+ owner_id: string;
25
27
  location: Location;
26
28
  };
27
29
  export type SearchLocationsOptions = OwnerSearchOptions & {
@@ -69,15 +71,20 @@ export type RemoveJobMemberOptions = {
69
71
  member_id: string;
70
72
  };
71
73
  export type SetLocationMembersOptions = {
74
+ owner_id: string;
72
75
  location: Location;
73
76
  current_members: string[];
74
77
  };
75
- export type SetJobMembersOptions = {
78
+ type JobMembershipOptions = {
76
79
  job: Job;
77
80
  location: Location;
78
81
  current_members: string[];
79
82
  foremen: string[];
80
83
  };
84
+ export type SetJobMembersOptions = JobMembershipOptions;
85
+ export type UpdateJobOptions = JobMembershipOptions & {
86
+ description: string;
87
+ };
81
88
  export type JobLifecycleOptions = {
82
89
  job: Job;
83
90
  };
@@ -104,6 +111,7 @@ export interface JobsFXOperator {
104
111
  removeJobMember(opts: RemoveJobMemberOptions): Promise<Job>;
105
112
  setLocationMembers(opts: SetLocationMembersOptions): Promise<Location>;
106
113
  setJobMembers(opts: SetJobMembersOptions): Promise<Job>;
114
+ updateJob(opts: UpdateJobOptions): Promise<Job>;
107
115
  jobLifecycleControls(opts: JobLifecycleControlsOptions): JobLifecycleControls;
108
116
  startJob(opts: JobLifecycleOptions): Promise<Job>;
109
117
  completeJob(opts: JobLifecycleOptions): Promise<Job>;
@@ -127,6 +135,7 @@ export declare class JobsFX implements JobsFXOperator {
127
135
  removeJobMember(opts: RemoveJobMemberOptions): Promise<Job>;
128
136
  setLocationMembers(opts: SetLocationMembersOptions): Promise<Location>;
129
137
  setJobMembers(opts: SetJobMembersOptions): Promise<Job>;
138
+ updateJob(opts: UpdateJobOptions): Promise<Job>;
130
139
  jobLifecycleControls(opts: JobLifecycleControlsOptions): JobLifecycleControls;
131
140
  startJob(opts: JobLifecycleOptions): Promise<Job>;
132
141
  completeJob(opts: JobLifecycleOptions): Promise<Job>;
@@ -9,9 +9,10 @@ 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
11
  import { runPreActionCheck } from "../core/pre-action.js";
12
- import { runOwnerSearch, runSearch, } from "../core/search.js";
12
+ import { runOwnerSearch, runSearch, withOwnerQuery, } from "../core/search.js";
13
13
  const msgJobIDRequired = "Job ID is required.";
14
14
  const msgLocationIDRequired = "Location ID is required.";
15
+ const msgOwnerIDRequired = "Owner ID is required.";
15
16
  const msgMemberIDRequired = "Member ID is required.";
16
17
  const msgJobLocationMismatch = "Job location does not match the provided parent location.";
17
18
  const msgProgramLocationMismatch = "Job program location does not match the provided parent location.";
@@ -69,6 +70,7 @@ export class JobsFX {
69
70
  return await this.findLocation(id);
70
71
  }
71
72
  async updateLocation(opts) {
73
+ const owner_id = requiredID(opts.owner_id, "owner_id", msgOwnerIDRequired);
72
74
  const id = requiredID(opts.location.id, "location", msgLocationIDRequired);
73
75
  const input = {
74
76
  ...opts.in,
@@ -78,7 +80,7 @@ export class JobsFX {
78
80
  const current_members = normalizeIDs(opts.in.current_members);
79
81
  const removed_members = removedIDs(opts.location.current_members, current_members);
80
82
  if (removed_members.length > 0) {
81
- await this.checkLocationMemberRemoval(id, removed_members);
83
+ await this.checkLocationMemberRemoval(owner_id, id, removed_members);
82
84
  }
83
85
  input.current_members = current_members;
84
86
  }
@@ -86,11 +88,12 @@ export class JobsFX {
86
88
  return await this.findLocation(id);
87
89
  }
88
90
  async archiveLocation(opts) {
91
+ const owner_id = requiredID(opts.owner_id, "owner_id", msgOwnerIDRequired);
89
92
  const id = requiredID(opts.location.id, "location", msgLocationIDRequired);
90
93
  if (opts.location.is_archived) {
91
94
  return opts.location;
92
95
  }
93
- await this.checkLocationArchive(id);
96
+ await this.checkLocationArchive(owner_id, id);
94
97
  await this.jobs.replaceOneLocation({ id, in: { is_archived: true } });
95
98
  return await this.findLocation(id);
96
99
  }
@@ -271,6 +274,7 @@ export class JobsFX {
271
274
  }
272
275
  async setLocationMembers(opts) {
273
276
  return await this.updateLocation({
277
+ owner_id: opts.owner_id,
274
278
  location: opts.location,
275
279
  in: {
276
280
  current_members: normalizeIDs(opts.current_members),
@@ -278,28 +282,29 @@ export class JobsFX {
278
282
  });
279
283
  }
280
284
  async setJobMembers(opts) {
281
- const id = requiredID(opts.job.id, "job", msgJobIDRequired);
282
- const location_id = locationID(opts.location, opts.job.location_id, "job", msgJobLocationMismatch);
283
- validateLocationAvailable(opts.location, "location");
284
- const current_members = normalizeIDs(opts.current_members);
285
- const foremen = normalizeIDs(opts.foremen);
286
- validateLocationMembers({
287
- members: current_members,
288
- location: opts.location,
289
- field: "current_members",
290
- msg: msgJobMembersOutsideLocation,
291
- });
292
- validateForemen({
293
- foremen,
294
- members: current_members,
295
- field: "foremen",
296
- msg: msgForemenOutsideJob,
285
+ const input = jobMembershipInput(opts);
286
+ await this.jobs.replaceOneJob({
287
+ id: input.id,
288
+ in: {
289
+ location_id: input.location_id,
290
+ current_members: input.current_members,
291
+ foremen: input.foremen,
292
+ },
297
293
  });
294
+ return await this.findJob(input.id);
295
+ }
296
+ async updateJob(opts) {
297
+ const input = jobMembershipInput(opts);
298
298
  await this.jobs.replaceOneJob({
299
- id,
300
- in: { location_id, current_members, foremen },
299
+ id: input.id,
300
+ in: {
301
+ description: opts.description,
302
+ location_id: input.location_id,
303
+ current_members: input.current_members,
304
+ foremen: input.foremen,
305
+ },
301
306
  });
302
- return await this.findJob(id);
307
+ return await this.findJob(input.id);
303
308
  }
304
309
  jobLifecycleControls(opts) {
305
310
  const actor_id = requiredID(opts.actor_id, "actor_id", msgMemberIDRequired);
@@ -348,30 +353,30 @@ export class JobsFX {
348
353
  }
349
354
  return await this.setJobStatus(opts.job, Cancelled);
350
355
  }
351
- async checkLocationMemberRemoval(location_id, removed_members) {
356
+ async checkLocationMemberRemoval(owner_id, location_id, removed_members) {
352
357
  await runPreActionCheck({
353
358
  reason: JobsPreActionReason.LocationMemberAssignedOpenJob,
354
359
  allowed: async () => {
355
360
  const out = await this.jobs.queryJobs({
356
- query: queryParams({
361
+ query: withOwnerQuery(owner_id, queryParams({
357
362
  location_id,
358
363
  status: [Pending, Started],
359
364
  current_members: removed_members,
360
- }),
365
+ })),
361
366
  });
362
367
  return (out.value?.length ?? 0) === 0;
363
368
  },
364
369
  });
365
370
  }
366
- async checkLocationArchive(location_id) {
371
+ async checkLocationArchive(owner_id, location_id) {
367
372
  await runPreActionCheck({
368
373
  reason: JobsPreActionReason.LocationHasOpenJobs,
369
374
  allowed: async () => {
370
375
  const out = await this.jobs.queryJobs({
371
- query: queryParams({
376
+ query: withOwnerQuery(owner_id, queryParams({
372
377
  location_id,
373
378
  status: [Pending, Started],
374
- }),
379
+ })),
375
380
  });
376
381
  return (out.value?.length ?? 0) === 0;
377
382
  },
@@ -380,10 +385,10 @@ export class JobsFX {
380
385
  reason: JobsPreActionReason.LocationHasActivePrograms,
381
386
  allowed: async () => {
382
387
  const out = await this.jobs.queryJobPrograms({
383
- query: queryParams({
388
+ query: withOwnerQuery(owner_id, queryParams({
384
389
  job_location_id: location_id,
385
390
  finished: false,
386
- }),
391
+ })),
387
392
  });
388
393
  return (out.value?.length ?? 0) === 0;
389
394
  },
@@ -431,6 +436,26 @@ export class JobsFX {
431
436
  return job;
432
437
  }
433
438
  }
439
+ function jobMembershipInput(opts) {
440
+ const id = requiredID(opts.job.id, "job", msgJobIDRequired);
441
+ const location_id = locationID(opts.location, opts.job.location_id, "job", msgJobLocationMismatch);
442
+ validateLocationAvailable(opts.location, "location");
443
+ const current_members = normalizeIDs(opts.current_members);
444
+ const foremen = normalizeIDs(opts.foremen);
445
+ validateLocationMembers({
446
+ members: current_members,
447
+ location: opts.location,
448
+ field: "current_members",
449
+ msg: msgJobMembersOutsideLocation,
450
+ });
451
+ validateForemen({
452
+ foremen,
453
+ members: current_members,
454
+ field: "foremen",
455
+ msg: msgForemenOutsideJob,
456
+ });
457
+ return { id, location_id, current_members, foremen };
458
+ }
434
459
  export function newJobsFX(opts) {
435
460
  return new JobsFX(opts);
436
461
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.82",
3
+ "version": "0.0.84",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {