@fabriktor/fx 0.0.76 → 0.0.77
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/date.d.ts +5 -0
- package/dist/src/core/date.js +39 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/jobs/jobs.d.ts +43 -73
- package/dist/src/jobs/jobs.js +289 -365
- package/dist/src/timesheets/timesheets.js +1 -11
- package/package.json +3 -3
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Date as SchemaDate } from "@fabriktor/schema";
|
|
2
|
+
export type LocalDateParts = Pick<SchemaDate, "y" | "m" | "d" | "h" | "min">;
|
|
3
|
+
export declare function normalizeBackendDate(v: LocalDateParts): SchemaDate;
|
|
4
|
+
export declare function zeroDate(): SchemaDate;
|
|
5
|
+
export declare function isZeroDate(v: LocalDateParts): boolean;
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
const zeroUTC = "0001-01-01T00:00:00Z";
|
|
7
|
+
const utcTimezone = "UTC";
|
|
8
|
+
export function normalizeBackendDate(v) {
|
|
9
|
+
if (isZeroDate(v)) {
|
|
10
|
+
return zeroDate();
|
|
11
|
+
}
|
|
12
|
+
return {
|
|
13
|
+
utc: zeroUTC,
|
|
14
|
+
y: v.y,
|
|
15
|
+
m: v.m,
|
|
16
|
+
d: v.d,
|
|
17
|
+
h: v.h,
|
|
18
|
+
min: v.min,
|
|
19
|
+
tz: utcTimezone,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export function zeroDate() {
|
|
23
|
+
return {
|
|
24
|
+
utc: zeroUTC,
|
|
25
|
+
y: 1,
|
|
26
|
+
m: 1,
|
|
27
|
+
d: 1,
|
|
28
|
+
h: 0,
|
|
29
|
+
min: 0,
|
|
30
|
+
tz: utcTimezone,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export function isZeroDate(v) {
|
|
34
|
+
if (v.h !== 0 || v.min !== 0) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
return ((v.y === 0 && v.m === 0 && v.d === 0) ||
|
|
38
|
+
(v.y === 1 && v.m === 1 && v.d === 1));
|
|
39
|
+
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from "./billable/billable.js";
|
|
|
3
3
|
export * from "./calls/calls.js";
|
|
4
4
|
export * from "./chat/chat.js";
|
|
5
5
|
export * from "./contacts/contacts.js";
|
|
6
|
+
export * from "./core/date.js";
|
|
6
7
|
export * from "./core/err.js";
|
|
7
8
|
export * from "./core/id.js";
|
|
8
9
|
export * from "./core/pull.js";
|
package/dist/src/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export * from "./billable/billable.js";
|
|
|
3
3
|
export * from "./calls/calls.js";
|
|
4
4
|
export * from "./chat/chat.js";
|
|
5
5
|
export * from "./contacts/contacts.js";
|
|
6
|
+
export * from "./core/date.js";
|
|
6
7
|
export * from "./core/err.js";
|
|
7
8
|
export * from "./core/id.js";
|
|
8
9
|
export * from "./core/pull.js";
|
package/dist/src/jobs/jobs.d.ts
CHANGED
|
@@ -1,31 +1,33 @@
|
|
|
1
|
-
import { type Job, type JobProgram, type Location
|
|
2
|
-
import type
|
|
1
|
+
import { type Job, type JobProgram, type Location } from "@fabriktor/schema";
|
|
2
|
+
import { type JobsOperator } from "@fabriktor/client";
|
|
3
3
|
import { type OwnerSearchOptions, type SearchOptions, type SearchPage } from "../core/search.js";
|
|
4
4
|
type JobInsert = Parameters<JobsOperator["insertOneJob"]>[0]["in"];
|
|
5
5
|
type JobProgramInsert = Parameters<JobsOperator["insertOneJobProgram"]>[0]["in"];
|
|
6
|
-
type LocationInsert = Parameters<JobsOperator["insertOneLocation"]>[0]["in"];
|
|
7
6
|
type LocationInsertOptions = Parameters<JobsOperator["insertOneLocation"]>[0];
|
|
8
|
-
type
|
|
9
|
-
type JobProgramReplaceOptions = Parameters<JobsOperator["replaceOneJobProgram"]>[0];
|
|
10
|
-
type JobReplaceOptions = Parameters<JobsOperator["replaceOneJob"]>[0];
|
|
7
|
+
type LocationReplaceInput = Parameters<JobsOperator["replaceOneLocation"]>[0]["in"];
|
|
11
8
|
export declare const JobProgramCreateMode: {
|
|
12
9
|
readonly Scheduled: "scheduled";
|
|
13
10
|
readonly Immediate: "immediate";
|
|
14
11
|
};
|
|
15
12
|
export type JobProgramCreateMode = (typeof JobProgramCreateMode)[keyof typeof JobProgramCreateMode];
|
|
16
13
|
export type CreateLocationOptions = LocationInsertOptions;
|
|
17
|
-
export type
|
|
18
|
-
|
|
19
|
-
in:
|
|
20
|
-
};
|
|
21
|
-
export type
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export type
|
|
25
|
-
|
|
14
|
+
export type UpdateLocationOptions = {
|
|
15
|
+
location: Location;
|
|
16
|
+
in: LocationReplaceInput;
|
|
17
|
+
};
|
|
18
|
+
export type SearchLocationsOptions = OwnerSearchOptions & {
|
|
19
|
+
is_archived?: boolean;
|
|
20
|
+
};
|
|
21
|
+
export type SearchAccessibleLocationsOptions = SearchOptions & {
|
|
22
|
+
is_archived?: boolean;
|
|
23
|
+
};
|
|
26
24
|
export type SearchJobProgramsOptions = OwnerSearchOptions;
|
|
27
|
-
export type SearchJobsOptions = OwnerSearchOptions
|
|
28
|
-
|
|
25
|
+
export type SearchJobsOptions = OwnerSearchOptions & {
|
|
26
|
+
location_id?: string;
|
|
27
|
+
};
|
|
28
|
+
export type SearchAccessibleJobsOptions = SearchOptions & {
|
|
29
|
+
location_id?: string;
|
|
30
|
+
};
|
|
29
31
|
export type SearchLocationsPage = SearchPage<Location>;
|
|
30
32
|
export type SearchJobProgramsPage = SearchPage<JobProgram>;
|
|
31
33
|
export type SearchJobsPage = SearchPage<Job>;
|
|
@@ -33,38 +35,20 @@ export type JobsFXOptions = {
|
|
|
33
35
|
jobs: JobsOperator;
|
|
34
36
|
now?: () => globalThis.Date;
|
|
35
37
|
};
|
|
36
|
-
export type CreateJobInput = Omit<JobInsert, "status" | "is_active" | "auto_start" | "all_members" | "location_id" | "recurrent">;
|
|
37
38
|
export type CreateJobOptions = {
|
|
38
|
-
in:
|
|
39
|
+
in: JobInsert;
|
|
39
40
|
location: Location;
|
|
40
|
-
auto_start: boolean;
|
|
41
|
-
start_immediately: boolean;
|
|
42
41
|
};
|
|
43
|
-
export type CreateJobProgramInput = Omit<JobProgramInsert, "job_location_id" | "current_occurrences">;
|
|
44
42
|
export type CreateJobProgramOptions = {
|
|
45
|
-
in:
|
|
43
|
+
in: JobProgramInsert;
|
|
46
44
|
location: Location;
|
|
47
45
|
};
|
|
48
|
-
export type CreateLocationJobProgramResult = {
|
|
49
|
-
mode: typeof JobProgramCreateMode.Scheduled;
|
|
50
|
-
create: OperationResult;
|
|
51
|
-
} | {
|
|
52
|
-
mode: typeof JobProgramCreateMode.Immediate;
|
|
53
|
-
};
|
|
54
46
|
export type CreateJobProgramResult = {
|
|
55
47
|
mode: typeof JobProgramCreateMode.Scheduled;
|
|
56
48
|
job_program: JobProgram;
|
|
57
49
|
} | {
|
|
58
50
|
mode: typeof JobProgramCreateMode.Immediate;
|
|
59
51
|
};
|
|
60
|
-
export type FindLocationOverlapsInput = Pick<LocationInsert, "y" | "m" | "d" | "h" | "min" | "tz" | "end_date" | "lat" | "lon">;
|
|
61
|
-
export type FindLocationOverlapsOptions = {
|
|
62
|
-
in: FindLocationOverlapsInput;
|
|
63
|
-
source_id?: string;
|
|
64
|
-
exclude_pending?: boolean;
|
|
65
|
-
exclude_started?: boolean;
|
|
66
|
-
exclude_terminated?: boolean;
|
|
67
|
-
};
|
|
68
52
|
export type AddJobForemanOptions = {
|
|
69
53
|
job: Job;
|
|
70
54
|
location: Location;
|
|
@@ -72,11 +56,8 @@ export type AddJobForemanOptions = {
|
|
|
72
56
|
};
|
|
73
57
|
export type RemoveJobMemberOptions = {
|
|
74
58
|
job: Job;
|
|
75
|
-
member_id: string;
|
|
76
|
-
};
|
|
77
|
-
export type UpdateOperationalLocationOptions = {
|
|
78
59
|
location: Location;
|
|
79
|
-
|
|
60
|
+
member_id: string;
|
|
80
61
|
};
|
|
81
62
|
export type SetLocationMembersOptions = {
|
|
82
63
|
location: Location;
|
|
@@ -96,61 +77,50 @@ export type JobLifecycleControlsOptions = JobLifecycleOptions & {
|
|
|
96
77
|
};
|
|
97
78
|
export type JobLifecycleControls = {
|
|
98
79
|
can_start: boolean;
|
|
99
|
-
|
|
80
|
+
can_complete: boolean;
|
|
81
|
+
can_cancel: boolean;
|
|
100
82
|
};
|
|
101
83
|
export interface JobsFXOperator {
|
|
102
84
|
createLocation(opts: CreateLocationOptions): Promise<Location>;
|
|
103
|
-
|
|
104
|
-
replaceLocation(opts: ReplaceLocationOptions): Promise<Location>;
|
|
85
|
+
updateLocation(opts: UpdateLocationOptions): Promise<Location>;
|
|
105
86
|
searchLocations(opts: SearchLocationsOptions): Promise<SearchLocationsPage>;
|
|
106
87
|
searchAccessibleLocations(opts?: SearchAccessibleLocationsOptions): Promise<SearchLocationsPage>;
|
|
107
|
-
|
|
88
|
+
createJobProgram(opts: CreateJobProgramOptions): Promise<CreateJobProgramResult>;
|
|
108
89
|
searchJobPrograms(opts: SearchJobProgramsOptions): Promise<SearchJobProgramsPage>;
|
|
109
|
-
|
|
90
|
+
createJob(opts: CreateJobOptions): Promise<Job>;
|
|
110
91
|
searchJobs(opts: SearchJobsOptions): Promise<SearchJobsPage>;
|
|
111
92
|
searchAccessibleJobs(opts?: SearchAccessibleJobsOptions): Promise<SearchJobsPage>;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
findLocationOverlaps(opts: FindLocationOverlapsOptions): Promise<OperationResultOf<RPJobsFindOverlaps>>;
|
|
117
|
-
updateOperationalLocation(opts: UpdateOperationalLocationOptions): Promise<Location>;
|
|
118
|
-
addJobForeman(opts: AddJobForemanOptions): Promise<OperationResult>;
|
|
119
|
-
removeJobMember(opts: RemoveJobMemberOptions): Promise<OperationResult>;
|
|
120
|
-
setLocationMembers(opts: SetLocationMembersOptions): Promise<OperationResult>;
|
|
121
|
-
setJobMembers(opts: SetJobMembersOptions): Promise<OperationResult>;
|
|
93
|
+
addJobForeman(opts: AddJobForemanOptions): Promise<Job>;
|
|
94
|
+
removeJobMember(opts: RemoveJobMemberOptions): Promise<Job>;
|
|
95
|
+
setLocationMembers(opts: SetLocationMembersOptions): Promise<Location>;
|
|
96
|
+
setJobMembers(opts: SetJobMembersOptions): Promise<Job>;
|
|
122
97
|
jobLifecycleControls(opts: JobLifecycleControlsOptions): JobLifecycleControls;
|
|
123
98
|
startJob(opts: JobLifecycleOptions): Promise<Job>;
|
|
124
|
-
|
|
99
|
+
completeJob(opts: JobLifecycleOptions): Promise<Job>;
|
|
100
|
+
cancelJob(opts: JobLifecycleOptions): Promise<Job>;
|
|
125
101
|
}
|
|
126
102
|
export declare class JobsFX implements JobsFXOperator {
|
|
127
103
|
private jobs;
|
|
128
104
|
private now;
|
|
129
105
|
constructor(opts: JobsFXOptions);
|
|
130
106
|
createLocation(opts: CreateLocationOptions): Promise<Location>;
|
|
131
|
-
|
|
132
|
-
replaceLocation(opts: ReplaceLocationOptions): Promise<Location>;
|
|
107
|
+
updateLocation(opts: UpdateLocationOptions): Promise<Location>;
|
|
133
108
|
searchLocations(opts: SearchLocationsOptions): Promise<SearchLocationsPage>;
|
|
134
109
|
searchAccessibleLocations(opts?: SearchAccessibleLocationsOptions): Promise<SearchLocationsPage>;
|
|
135
|
-
|
|
110
|
+
createJobProgram(opts: CreateJobProgramOptions): Promise<CreateJobProgramResult>;
|
|
136
111
|
searchJobPrograms(opts: SearchJobProgramsOptions): Promise<SearchJobProgramsPage>;
|
|
137
|
-
|
|
112
|
+
createJob(opts: CreateJobOptions): Promise<Job>;
|
|
138
113
|
searchJobs(opts: SearchJobsOptions): Promise<SearchJobsPage>;
|
|
139
114
|
searchAccessibleJobs(opts?: SearchAccessibleJobsOptions): Promise<SearchJobsPage>;
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
findLocationOverlaps(opts: FindLocationOverlapsOptions): Promise<OperationResultOf<RPJobsFindOverlaps>>;
|
|
145
|
-
addJobForeman(opts: AddJobForemanOptions): Promise<OperationResult>;
|
|
146
|
-
removeJobMember(opts: RemoveJobMemberOptions): Promise<OperationResult>;
|
|
147
|
-
updateOperationalLocation(opts: UpdateOperationalLocationOptions): Promise<Location>;
|
|
148
|
-
setLocationMembers(opts: SetLocationMembersOptions): Promise<OperationResult>;
|
|
149
|
-
setJobMembers(opts: SetJobMembersOptions): Promise<OperationResult>;
|
|
150
|
-
private validateLocationMemberRemoval;
|
|
115
|
+
addJobForeman(opts: AddJobForemanOptions): Promise<Job>;
|
|
116
|
+
removeJobMember(opts: RemoveJobMemberOptions): Promise<Job>;
|
|
117
|
+
setLocationMembers(opts: SetLocationMembersOptions): Promise<Location>;
|
|
118
|
+
setJobMembers(opts: SetJobMembersOptions): Promise<Job>;
|
|
151
119
|
jobLifecycleControls(opts: JobLifecycleControlsOptions): JobLifecycleControls;
|
|
152
120
|
startJob(opts: JobLifecycleOptions): Promise<Job>;
|
|
153
|
-
|
|
121
|
+
completeJob(opts: JobLifecycleOptions): Promise<Job>;
|
|
122
|
+
cancelJob(opts: JobLifecycleOptions): Promise<Job>;
|
|
123
|
+
private setJobStatus;
|
|
154
124
|
private findLocation;
|
|
155
125
|
private findJobProgram;
|
|
156
126
|
private findJob;
|
package/dist/src/jobs/jobs.js
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
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 {
|
|
6
|
+
import { Cancelled, Completed, Pending, RecurrenceEndTypeAfterDate, RecurrenceEndTypeAfterOccurrences, RecurrenceEndTypeIndefinite, Started, } from "@fabriktor/schema";
|
|
7
|
+
import { queryParams } from "@fabriktor/client";
|
|
8
|
+
import { isZeroDate, normalizeBackendDate, zeroDate, } from "../core/date.js";
|
|
7
9
|
import { errRequired, errResolveFailed, errValidation, } from "../core/err.js";
|
|
8
10
|
import { requiredOperationID, resolvedID } from "../core/id.js";
|
|
9
11
|
import { runOwnerSearch, runSearch, } from "../core/search.js";
|
|
@@ -11,16 +13,24 @@ const msgJobIDRequired = "Job ID is required.";
|
|
|
11
13
|
const msgLocationIDRequired = "Location ID is required.";
|
|
12
14
|
const msgMemberIDRequired = "Member ID is required.";
|
|
13
15
|
const msgJobLocationMismatch = "Job location does not match the provided parent location.";
|
|
16
|
+
const msgProgramLocationMismatch = "Job program location does not match the provided parent location.";
|
|
14
17
|
const msgJobMembersOutsideLocation = "All job members must belong to the parent location.";
|
|
15
18
|
const msgProgramMembersOutsideLocation = "All program job members must belong to the parent location.";
|
|
16
|
-
const
|
|
17
|
-
const
|
|
19
|
+
const msgForemenOutsideJob = "Foremen must belong to the job members.";
|
|
20
|
+
const msgProgramForemenOutsideJob = "Program foremen must belong to the program job members.";
|
|
21
|
+
const msgArchivedLocation = "Archived locations cannot receive new work.";
|
|
22
|
+
const msgExpectedEndBeforeStart = "Expected end date must not be before the planned start date.";
|
|
18
23
|
const msgProgramStartPast = "Program start date must not be in the past.";
|
|
19
|
-
const msgJobStartOutsideLocation = "Job start date must fall within the parent location time span.";
|
|
20
|
-
const msgProgramStartOutsideLocation = "Program job start date must fall within the parent location time span.";
|
|
21
|
-
const msgLocationMemberAssigned = "Cannot remove a location member while they are assigned to a non-terminated job in that location.";
|
|
22
|
-
const msgLocationRangeInvalid = "Location contains an invalid scheduled time span.";
|
|
23
24
|
const msgTimezoneInvalid = "Location timezone is invalid.";
|
|
25
|
+
const msgInitialJobStatusInvalid = "A new job must be created as pending or started.";
|
|
26
|
+
const msgJobStartInvalid = "Only pending jobs can be started.";
|
|
27
|
+
const msgJobCompleteInvalid = "Only started jobs can be completed.";
|
|
28
|
+
const msgJobCancelInvalid = "Only pending or started jobs can be cancelled.";
|
|
29
|
+
const msgProgramDurationInvalid = "Expected job duration must be a non-negative whole number of minutes.";
|
|
30
|
+
const msgRecurrenceRateInvalid = "Recurrence rate must be a positive whole number.";
|
|
31
|
+
const msgRecurrenceEndTypeInvalid = "Recurrence end type is invalid.";
|
|
32
|
+
const msgOccurrencesInvalid = "Occurrences to end must be a positive whole number.";
|
|
33
|
+
const msgRecurrenceEndDateRequired = "Recurrence end date is required.";
|
|
24
34
|
const resourceLocation = "location";
|
|
25
35
|
const resourceJobProgram = "job_program";
|
|
26
36
|
const resourceJob = "job";
|
|
@@ -30,8 +40,6 @@ const msgCreatedJobIDMissing = "Created job response did not contain an ID.";
|
|
|
30
40
|
const msgLocationMissing = "Location could not be resolved after the mutation.";
|
|
31
41
|
const msgJobProgramMissing = "Job program could not be resolved after the mutation.";
|
|
32
42
|
const msgJobMissing = "Job could not be resolved after the mutation.";
|
|
33
|
-
const msgJobNotActive = "Job must be active before it can be started.";
|
|
34
|
-
const msgJobTerminated = "Terminated jobs cannot be started.";
|
|
35
43
|
export const JobProgramCreateMode = {
|
|
36
44
|
Scheduled: "scheduled",
|
|
37
45
|
Immediate: "immediate",
|
|
@@ -44,378 +52,294 @@ export class JobsFX {
|
|
|
44
52
|
this.now = opts.now ?? (() => new globalThis.Date());
|
|
45
53
|
}
|
|
46
54
|
async createLocation(opts) {
|
|
47
|
-
const out = await this.jobs.insertOneLocation(
|
|
48
|
-
const id = requiredOperationID(out, msgCreatedLocationIDMissing);
|
|
49
|
-
return await this.findLocation(id);
|
|
50
|
-
}
|
|
51
|
-
async createOperationalLocation(opts) {
|
|
52
|
-
const persistent = opts.in.type === LocationTypePersistent;
|
|
53
|
-
return await this.createLocation({
|
|
55
|
+
const out = await this.jobs.insertOneLocation({
|
|
54
56
|
in: {
|
|
55
57
|
...opts.in,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
auto_start: persistent ? true : opts.in.auto_start,
|
|
59
|
-
all_members: [],
|
|
58
|
+
is_archived: false,
|
|
59
|
+
address_timezone: "",
|
|
60
60
|
current_members: normalizeIDs(opts.in.current_members),
|
|
61
61
|
},
|
|
62
62
|
});
|
|
63
|
+
const id = requiredOperationID(out, msgCreatedLocationIDMissing);
|
|
64
|
+
return await this.findLocation(id);
|
|
63
65
|
}
|
|
64
|
-
async
|
|
65
|
-
|
|
66
|
-
|
|
66
|
+
async updateLocation(opts) {
|
|
67
|
+
const id = requiredID(opts.location.id, "location", msgLocationIDRequired);
|
|
68
|
+
const input = {
|
|
69
|
+
...opts.in,
|
|
70
|
+
...locationAddressInput(opts.location),
|
|
71
|
+
};
|
|
72
|
+
if (opts.in.current_members !== undefined) {
|
|
73
|
+
input.current_members = normalizeIDs(opts.in.current_members);
|
|
74
|
+
}
|
|
75
|
+
await this.jobs.replaceOneLocation({ id, in: input });
|
|
76
|
+
return await this.findLocation(id);
|
|
67
77
|
}
|
|
68
78
|
async searchLocations(opts) {
|
|
79
|
+
const { is_archived, ...searchOpts } = opts;
|
|
69
80
|
return await runOwnerSearch({
|
|
70
|
-
...
|
|
71
|
-
|
|
81
|
+
...searchOpts,
|
|
82
|
+
query: is_archived === undefined
|
|
83
|
+
? undefined
|
|
84
|
+
: queryParams({ is_archived }),
|
|
85
|
+
search: (opts) => this.jobs.searchManyLocations(opts),
|
|
72
86
|
});
|
|
73
87
|
}
|
|
74
88
|
async searchAccessibleLocations(opts = {}) {
|
|
89
|
+
const { is_archived, ...searchOpts } = opts;
|
|
75
90
|
return await runSearch({
|
|
76
|
-
...
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
await this.jobs.replaceOneJobProgram(opts);
|
|
82
|
-
return await this.findJobProgram(opts.id);
|
|
83
|
-
}
|
|
84
|
-
async searchJobPrograms(opts) {
|
|
85
|
-
return await runOwnerSearch({
|
|
86
|
-
...opts,
|
|
87
|
-
search: (searchOpts) => this.jobs.searchManyJobPrograms(searchOpts),
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
async replaceJob(opts) {
|
|
91
|
-
await this.jobs.replaceOneJob(opts);
|
|
92
|
-
return await this.findJob(opts.id);
|
|
93
|
-
}
|
|
94
|
-
async searchJobs(opts) {
|
|
95
|
-
return await runOwnerSearch({
|
|
96
|
-
...opts,
|
|
97
|
-
search: (searchOpts) => this.jobs.searchManyJobs(searchOpts),
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
async searchAccessibleJobs(opts = {}) {
|
|
101
|
-
return await runSearch({
|
|
102
|
-
...opts,
|
|
103
|
-
search: (searchOpts) => this.jobs.searchMineJobs(searchOpts),
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
async createJob(opts) {
|
|
107
|
-
const out = await this.createLocationJob(opts);
|
|
108
|
-
const id = requiredOperationID(out, msgCreatedJobIDMissing);
|
|
109
|
-
return await this.findJob(id);
|
|
110
|
-
}
|
|
111
|
-
async createLocationJob(opts) {
|
|
112
|
-
const location_id = requiredID(opts.location.id, "location", msgLocationIDRequired);
|
|
113
|
-
const foremen = normalizeIDs(opts.in.foremen);
|
|
114
|
-
const current_members = unionIDs(opts.in.current_members, foremen);
|
|
115
|
-
validateLocationMembers({
|
|
116
|
-
members: current_members,
|
|
117
|
-
location: opts.location,
|
|
118
|
-
field: "in",
|
|
119
|
-
msg: msgJobMembersOutsideLocation,
|
|
120
|
-
});
|
|
121
|
-
const timezone = locationTimezone(opts.location, "location");
|
|
122
|
-
const input = {
|
|
123
|
-
...opts.in,
|
|
124
|
-
tz: timezone,
|
|
125
|
-
timezone,
|
|
126
|
-
status: opts.start_immediately ? Started : Pending,
|
|
127
|
-
is_active: opts.start_immediately,
|
|
128
|
-
auto_start: opts.start_immediately ? true : opts.auto_start,
|
|
129
|
-
all_members: [],
|
|
130
|
-
current_members,
|
|
131
|
-
foremen,
|
|
132
|
-
location_id,
|
|
133
|
-
recurrent: false,
|
|
134
|
-
};
|
|
135
|
-
validateJobStart({
|
|
136
|
-
input,
|
|
137
|
-
location: opts.location,
|
|
138
|
-
now: this.now(),
|
|
139
|
-
start_immediately: opts.start_immediately,
|
|
140
|
-
});
|
|
141
|
-
return await this.jobs.insertOneJob({
|
|
142
|
-
in: input,
|
|
91
|
+
...searchOpts,
|
|
92
|
+
query: is_archived === undefined
|
|
93
|
+
? undefined
|
|
94
|
+
: queryParams({ is_archived }),
|
|
95
|
+
search: (opts) => this.jobs.searchMineLocations(opts),
|
|
143
96
|
});
|
|
144
97
|
}
|
|
145
98
|
async createJobProgram(opts) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
mode: JobProgramCreateMode.Immediate,
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
const id = requiredOperationID(out.create, msgCreatedJobProgramIDMissing);
|
|
153
|
-
return {
|
|
154
|
-
mode: JobProgramCreateMode.Scheduled,
|
|
155
|
-
job_program: await this.findJobProgram(id),
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
async createLocationJobProgram(opts) {
|
|
159
|
-
const job_location_id = requiredID(opts.location.id, "location", msgLocationIDRequired);
|
|
99
|
+
validateLocationAvailable(opts.location, "location");
|
|
100
|
+
const job_location_id = locationID(opts.location, opts.in.job_location_id, "in", msgProgramLocationMismatch);
|
|
101
|
+
const job_current_members = normalizeIDs(opts.in.job_current_members);
|
|
160
102
|
const job_foremen = normalizeIDs(opts.in.job_foremen);
|
|
161
|
-
const job_current_members = unionIDs(opts.in.job_current_members, job_foremen);
|
|
162
103
|
validateLocationMembers({
|
|
163
104
|
members: job_current_members,
|
|
164
105
|
location: opts.location,
|
|
165
106
|
field: "in",
|
|
166
107
|
msg: msgProgramMembersOutsideLocation,
|
|
167
108
|
});
|
|
168
|
-
|
|
109
|
+
validateForemen({
|
|
110
|
+
foremen: job_foremen,
|
|
111
|
+
members: job_current_members,
|
|
112
|
+
field: "in",
|
|
113
|
+
msg: msgProgramForemenOutsideJob,
|
|
114
|
+
});
|
|
115
|
+
validateDuration(opts.in.job_expected_duration_in_minutes, "in");
|
|
116
|
+
validateRecurrenceRate(opts.in.recurrence_rate, "in");
|
|
117
|
+
const recurrence = normalizeRecurrenceEnd({
|
|
118
|
+
end_type: opts.in.recurrence_end_type,
|
|
119
|
+
occurrences_to_end: opts.in.occurrences_to_end,
|
|
120
|
+
recurrence_end_date: opts.in.recurrence_end_date,
|
|
121
|
+
field: "in",
|
|
122
|
+
});
|
|
169
123
|
const input = {
|
|
170
124
|
...opts.in,
|
|
171
|
-
|
|
172
|
-
job_start_date: {
|
|
173
|
-
...opts.in.job_start_date,
|
|
174
|
-
tz: timezone,
|
|
175
|
-
},
|
|
176
|
-
recurrence_end_date: {
|
|
177
|
-
...opts.in.recurrence_end_date,
|
|
178
|
-
tz: timezone,
|
|
179
|
-
},
|
|
125
|
+
job_start_date: normalizeBackendDate(opts.in.job_start_date),
|
|
180
126
|
job_current_members,
|
|
181
127
|
job_foremen,
|
|
182
128
|
job_location_id,
|
|
183
|
-
|
|
129
|
+
occurrences_to_end: recurrence.occurrences_to_end,
|
|
130
|
+
recurrence_end_date: recurrence.recurrence_end_date,
|
|
184
131
|
};
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
validateProgramStart({
|
|
188
|
-
input,
|
|
132
|
+
const current_day = validateProgramStart({
|
|
133
|
+
start: input.job_start_date,
|
|
189
134
|
location: opts.location,
|
|
190
|
-
now,
|
|
191
|
-
current_day,
|
|
135
|
+
now: this.now(),
|
|
192
136
|
});
|
|
193
137
|
if (current_day) {
|
|
194
138
|
await this.jobs.immediateJobFromProgram(input);
|
|
195
|
-
return {
|
|
196
|
-
mode: JobProgramCreateMode.Immediate,
|
|
197
|
-
};
|
|
139
|
+
return { mode: JobProgramCreateMode.Immediate };
|
|
198
140
|
}
|
|
199
|
-
const
|
|
200
|
-
|
|
201
|
-
});
|
|
141
|
+
const out = await this.jobs.insertOneJobProgram({ in: input });
|
|
142
|
+
const id = requiredOperationID(out, msgCreatedJobProgramIDMissing);
|
|
202
143
|
return {
|
|
203
144
|
mode: JobProgramCreateMode.Scheduled,
|
|
204
|
-
|
|
145
|
+
job_program: await this.findJobProgram(id),
|
|
205
146
|
};
|
|
206
147
|
}
|
|
207
|
-
async
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
y: opts.in.y,
|
|
213
|
-
m: opts.in.m,
|
|
214
|
-
d: opts.in.d,
|
|
215
|
-
h: opts.in.h,
|
|
216
|
-
min: opts.in.min,
|
|
217
|
-
tz: opts.in.tz,
|
|
218
|
-
},
|
|
219
|
-
end_date: {
|
|
220
|
-
y: opts.in.end_date.y,
|
|
221
|
-
m: opts.in.end_date.m,
|
|
222
|
-
d: opts.in.end_date.d,
|
|
223
|
-
h: opts.in.end_date.h,
|
|
224
|
-
min: opts.in.end_date.min,
|
|
225
|
-
tz: opts.in.end_date.tz,
|
|
226
|
-
},
|
|
227
|
-
source_id: resolvedID(opts.source_id) ?? ZeroID,
|
|
228
|
-
exclude_pending: opts.exclude_pending ?? false,
|
|
229
|
-
exclude_started: opts.exclude_started ?? false,
|
|
230
|
-
exclude_terminated: opts.exclude_terminated ?? false,
|
|
231
|
-
};
|
|
232
|
-
return await this.jobs.findOverlaps(input);
|
|
148
|
+
async searchJobPrograms(opts) {
|
|
149
|
+
return await runOwnerSearch({
|
|
150
|
+
...opts,
|
|
151
|
+
search: (searchOpts) => this.jobs.searchManyJobPrograms(searchOpts),
|
|
152
|
+
});
|
|
233
153
|
}
|
|
234
|
-
async
|
|
235
|
-
|
|
236
|
-
const location_id =
|
|
237
|
-
|
|
238
|
-
if (job_location_id !== location_id) {
|
|
239
|
-
throw errValidation({
|
|
240
|
-
field: "job",
|
|
241
|
-
msg: msgJobLocationMismatch,
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
const member_id = opts.member_id.trim();
|
|
245
|
-
if (member_id === "") {
|
|
246
|
-
throw errRequired({
|
|
247
|
-
field: "member_id",
|
|
248
|
-
msg: msgMemberIDRequired,
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
const location_members = new Set(normalizeIDs(opts.location.current_members));
|
|
252
|
-
if (!location_members.has(member_id)) {
|
|
154
|
+
async createJob(opts) {
|
|
155
|
+
validateLocationAvailable(opts.location, "location");
|
|
156
|
+
const location_id = locationID(opts.location, opts.in.location_id, "in", msgJobLocationMismatch);
|
|
157
|
+
if (opts.in.status !== Pending && opts.in.status !== Started) {
|
|
253
158
|
throw errValidation({
|
|
254
|
-
field: "
|
|
255
|
-
msg:
|
|
159
|
+
field: "in",
|
|
160
|
+
msg: msgInitialJobStatusInvalid,
|
|
256
161
|
});
|
|
257
162
|
}
|
|
258
|
-
|
|
259
|
-
|
|
163
|
+
const current_members = normalizeIDs(opts.in.current_members);
|
|
164
|
+
const foremen = normalizeIDs(opts.in.foremen);
|
|
165
|
+
validateLocationMembers({
|
|
166
|
+
members: current_members,
|
|
167
|
+
location: opts.location,
|
|
168
|
+
field: "in",
|
|
169
|
+
msg: msgJobMembersOutsideLocation,
|
|
170
|
+
});
|
|
171
|
+
validateForemen({
|
|
172
|
+
foremen,
|
|
173
|
+
members: current_members,
|
|
174
|
+
field: "in",
|
|
175
|
+
msg: msgForemenOutsideJob,
|
|
176
|
+
});
|
|
177
|
+
const expected_end_date = normalizeBackendDate(opts.in.expected_end_date);
|
|
178
|
+
validateExpectedEnd({
|
|
179
|
+
start: opts.in,
|
|
180
|
+
end: expected_end_date,
|
|
181
|
+
field: "in",
|
|
182
|
+
});
|
|
183
|
+
const out = await this.jobs.insertOneJob({
|
|
260
184
|
in: {
|
|
261
|
-
|
|
262
|
-
|
|
185
|
+
...opts.in,
|
|
186
|
+
...normalizeBackendDate(opts.in),
|
|
187
|
+
expected_end_date,
|
|
188
|
+
current_members,
|
|
189
|
+
foremen,
|
|
190
|
+
location_id,
|
|
263
191
|
},
|
|
264
192
|
});
|
|
193
|
+
const id = requiredOperationID(out, msgCreatedJobIDMissing);
|
|
194
|
+
return await this.findJob(id);
|
|
265
195
|
}
|
|
266
|
-
async
|
|
196
|
+
async searchJobs(opts) {
|
|
197
|
+
const { location_id, ...searchOpts } = opts;
|
|
198
|
+
const id = resolvedID(location_id);
|
|
199
|
+
return await runOwnerSearch({
|
|
200
|
+
...searchOpts,
|
|
201
|
+
query: id === undefined ? undefined : queryParams({ location_id: id }),
|
|
202
|
+
search: (opts) => this.jobs.searchManyJobs(opts),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
async searchAccessibleJobs(opts = {}) {
|
|
206
|
+
const { location_id, ...searchOpts } = opts;
|
|
207
|
+
const id = resolvedID(location_id);
|
|
208
|
+
return await runSearch({
|
|
209
|
+
...searchOpts,
|
|
210
|
+
query: id === undefined ? undefined : queryParams({ location_id: id }),
|
|
211
|
+
search: (opts) => this.jobs.searchMineJobs(opts),
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
async addJobForeman(opts) {
|
|
267
215
|
const id = requiredID(opts.job.id, "job", msgJobIDRequired);
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
216
|
+
const location_id = locationID(opts.location, opts.job.location_id, "job", msgJobLocationMismatch);
|
|
217
|
+
const member_id = requiredID(opts.member_id, "member_id", msgMemberIDRequired);
|
|
218
|
+
validateLocationAvailable(opts.location, "location");
|
|
219
|
+
if (!normalizeIDs(opts.job.current_members).includes(member_id)) {
|
|
220
|
+
throw errValidation({
|
|
271
221
|
field: "member_id",
|
|
272
|
-
msg:
|
|
222
|
+
msg: msgForemenOutsideJob,
|
|
273
223
|
});
|
|
274
224
|
}
|
|
275
|
-
|
|
225
|
+
validateLocationMembers({
|
|
226
|
+
members: [member_id],
|
|
227
|
+
location: opts.location,
|
|
228
|
+
field: "member_id",
|
|
229
|
+
msg: msgJobMembersOutsideLocation,
|
|
230
|
+
});
|
|
231
|
+
await this.jobs.replaceOneJob({
|
|
276
232
|
id,
|
|
277
233
|
in: {
|
|
278
|
-
|
|
234
|
+
location_id,
|
|
235
|
+
foremen: unionIDs(opts.job.foremen, [member_id]),
|
|
279
236
|
},
|
|
280
237
|
});
|
|
238
|
+
return await this.findJob(id);
|
|
281
239
|
}
|
|
282
|
-
async
|
|
283
|
-
const id = requiredID(opts.
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
});
|
|
289
|
-
return await this.replaceLocation({
|
|
240
|
+
async removeJobMember(opts) {
|
|
241
|
+
const id = requiredID(opts.job.id, "job", msgJobIDRequired);
|
|
242
|
+
locationID(opts.location, opts.job.location_id, "job", msgJobLocationMismatch);
|
|
243
|
+
validateLocationAvailable(opts.location, "location");
|
|
244
|
+
const member_id = requiredID(opts.member_id, "member_id", msgMemberIDRequired);
|
|
245
|
+
await this.jobs.replaceOneJob({
|
|
290
246
|
id,
|
|
291
247
|
in: {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
current_members,
|
|
248
|
+
current_members: withoutID(opts.job.current_members, member_id),
|
|
249
|
+
foremen: withoutID(opts.job.foremen, member_id),
|
|
295
250
|
},
|
|
296
251
|
});
|
|
252
|
+
return await this.findJob(id);
|
|
297
253
|
}
|
|
298
254
|
async setLocationMembers(opts) {
|
|
299
|
-
|
|
300
|
-
const current_members = normalizeIDs(opts.current_members);
|
|
301
|
-
await this.validateLocationMemberRemoval({
|
|
255
|
+
return await this.updateLocation({
|
|
302
256
|
location: opts.location,
|
|
303
|
-
current_members,
|
|
304
|
-
});
|
|
305
|
-
return await this.jobs.replaceOneLocation({
|
|
306
|
-
id,
|
|
307
257
|
in: {
|
|
308
|
-
current_members,
|
|
258
|
+
current_members: normalizeIDs(opts.current_members),
|
|
309
259
|
},
|
|
310
260
|
});
|
|
311
261
|
}
|
|
312
262
|
async setJobMembers(opts) {
|
|
313
263
|
const id = requiredID(opts.job.id, "job", msgJobIDRequired);
|
|
314
|
-
const location_id =
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
field: "job",
|
|
318
|
-
msg: msgJobLocationMismatch,
|
|
319
|
-
});
|
|
320
|
-
}
|
|
264
|
+
const location_id = locationID(opts.location, opts.job.location_id, "job", msgJobLocationMismatch);
|
|
265
|
+
validateLocationAvailable(opts.location, "location");
|
|
266
|
+
const current_members = normalizeIDs(opts.current_members);
|
|
321
267
|
const foremen = normalizeIDs(opts.foremen);
|
|
322
|
-
const current_members = unionIDs(opts.current_members, foremen);
|
|
323
268
|
validateLocationMembers({
|
|
324
269
|
members: current_members,
|
|
325
270
|
location: opts.location,
|
|
326
271
|
field: "current_members",
|
|
327
272
|
msg: msgJobMembersOutsideLocation,
|
|
328
273
|
});
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
},
|
|
274
|
+
validateForemen({
|
|
275
|
+
foremen,
|
|
276
|
+
members: current_members,
|
|
277
|
+
field: "foremen",
|
|
278
|
+
msg: msgForemenOutsideJob,
|
|
335
279
|
});
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
const next = new Set(opts.current_members);
|
|
340
|
-
const removed = normalizeIDs(opts.location.current_members).filter((member_id) => !next.has(member_id));
|
|
341
|
-
if (removed.length === 0) {
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
const out = await this.jobs.queryJobs({
|
|
345
|
-
query: {
|
|
346
|
-
owner_id: opts.location.owner_id,
|
|
347
|
-
location_id: id,
|
|
348
|
-
},
|
|
280
|
+
await this.jobs.replaceOneJob({
|
|
281
|
+
id,
|
|
282
|
+
in: { location_id, current_members, foremen },
|
|
349
283
|
});
|
|
350
|
-
|
|
351
|
-
for (const job of out.value ?? []) {
|
|
352
|
-
if (job.status === Terminated) {
|
|
353
|
-
continue;
|
|
354
|
-
}
|
|
355
|
-
if (normalizeIDs(job.current_members).some((member_id) => removed_set.has(member_id))) {
|
|
356
|
-
throw errValidation({
|
|
357
|
-
field: "current_members",
|
|
358
|
-
msg: msgLocationMemberAssigned,
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
}
|
|
284
|
+
return await this.findJob(id);
|
|
362
285
|
}
|
|
363
286
|
jobLifecycleControls(opts) {
|
|
364
287
|
const actor_id = requiredID(opts.actor_id, "actor_id", msgMemberIDRequired);
|
|
365
288
|
const can_manage = opts.job.owner_id === actor_id ||
|
|
366
289
|
normalizeIDs(opts.job.foremen).includes(actor_id);
|
|
367
|
-
const terminated = opts.job.status === Terminated;
|
|
368
290
|
return {
|
|
369
|
-
can_start: can_manage &&
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
can_terminate: can_manage && !terminated,
|
|
291
|
+
can_start: can_manage && opts.job.status === Pending,
|
|
292
|
+
can_complete: can_manage && opts.job.status === Started,
|
|
293
|
+
can_cancel: can_manage &&
|
|
294
|
+
(opts.job.status === Pending || opts.job.status === Started),
|
|
374
295
|
};
|
|
375
296
|
}
|
|
376
297
|
async startJob(opts) {
|
|
377
|
-
|
|
378
|
-
|
|
298
|
+
if (opts.job.status === Started) {
|
|
299
|
+
return opts.job;
|
|
300
|
+
}
|
|
301
|
+
if (opts.job.status !== Pending) {
|
|
379
302
|
throw errValidation({
|
|
380
303
|
field: "job",
|
|
381
|
-
msg:
|
|
304
|
+
msg: msgJobStartInvalid,
|
|
382
305
|
});
|
|
383
306
|
}
|
|
384
|
-
|
|
307
|
+
return await this.setJobStatus(opts.job, Started);
|
|
308
|
+
}
|
|
309
|
+
async completeJob(opts) {
|
|
310
|
+
if (opts.job.status === Completed) {
|
|
385
311
|
return opts.job;
|
|
386
312
|
}
|
|
387
|
-
if (
|
|
313
|
+
if (opts.job.status !== Started) {
|
|
388
314
|
throw errValidation({
|
|
389
315
|
field: "job",
|
|
390
|
-
msg:
|
|
316
|
+
msg: msgJobCompleteInvalid,
|
|
391
317
|
});
|
|
392
318
|
}
|
|
393
|
-
await this.
|
|
394
|
-
id,
|
|
395
|
-
in: {
|
|
396
|
-
status: Started,
|
|
397
|
-
},
|
|
398
|
-
});
|
|
399
|
-
return await this.findJob(id);
|
|
319
|
+
return await this.setJobStatus(opts.job, Completed);
|
|
400
320
|
}
|
|
401
|
-
async
|
|
402
|
-
|
|
403
|
-
if (opts.job.status === Terminated) {
|
|
321
|
+
async cancelJob(opts) {
|
|
322
|
+
if (opts.job.status === Cancelled) {
|
|
404
323
|
return opts.job;
|
|
405
324
|
}
|
|
325
|
+
if (opts.job.status !== Pending && opts.job.status !== Started) {
|
|
326
|
+
throw errValidation({
|
|
327
|
+
field: "job",
|
|
328
|
+
msg: msgJobCancelInvalid,
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
return await this.setJobStatus(opts.job, Cancelled);
|
|
332
|
+
}
|
|
333
|
+
async setJobStatus(job, status) {
|
|
334
|
+
const id = requiredID(job.id, "job", msgJobIDRequired);
|
|
406
335
|
await this.jobs.replaceOneJob({
|
|
407
336
|
id,
|
|
408
|
-
in: {
|
|
409
|
-
status: Terminated,
|
|
410
|
-
is_active: false,
|
|
411
|
-
},
|
|
337
|
+
in: { status },
|
|
412
338
|
});
|
|
413
339
|
return await this.findJob(id);
|
|
414
340
|
}
|
|
415
341
|
async findLocation(id) {
|
|
416
|
-
const out = await this.jobs.findOneLocation({
|
|
417
|
-
id,
|
|
418
|
-
});
|
|
342
|
+
const out = await this.jobs.findOneLocation({ id });
|
|
419
343
|
const location = out.value;
|
|
420
344
|
if (location === undefined) {
|
|
421
345
|
throw errResolveFailed({
|
|
@@ -426,9 +350,7 @@ export class JobsFX {
|
|
|
426
350
|
return location;
|
|
427
351
|
}
|
|
428
352
|
async findJobProgram(id) {
|
|
429
|
-
const out = await this.jobs.findOneJobProgram({
|
|
430
|
-
id,
|
|
431
|
-
});
|
|
353
|
+
const out = await this.jobs.findOneJobProgram({ id });
|
|
432
354
|
const job_program = out.value;
|
|
433
355
|
if (job_program === undefined) {
|
|
434
356
|
throw errResolveFailed({
|
|
@@ -439,9 +361,7 @@ export class JobsFX {
|
|
|
439
361
|
return job_program;
|
|
440
362
|
}
|
|
441
363
|
async findJob(id) {
|
|
442
|
-
const out = await this.jobs.findOneJob({
|
|
443
|
-
id,
|
|
444
|
-
});
|
|
364
|
+
const out = await this.jobs.findOneJob({ id });
|
|
445
365
|
const job = out.value;
|
|
446
366
|
if (job === undefined) {
|
|
447
367
|
throw errResolveFailed({
|
|
@@ -464,109 +384,122 @@ function validateLocationMembers(opts) {
|
|
|
464
384
|
});
|
|
465
385
|
}
|
|
466
386
|
}
|
|
467
|
-
function
|
|
468
|
-
const
|
|
469
|
-
if (
|
|
387
|
+
function validateForemen(opts) {
|
|
388
|
+
const members = new Set(opts.members);
|
|
389
|
+
if (opts.foremen.some((id) => !members.has(id))) {
|
|
470
390
|
throw errValidation({
|
|
471
|
-
field:
|
|
472
|
-
msg:
|
|
391
|
+
field: opts.field,
|
|
392
|
+
msg: opts.msg,
|
|
473
393
|
});
|
|
474
394
|
}
|
|
475
|
-
|
|
395
|
+
}
|
|
396
|
+
function validateLocationAvailable(location, field) {
|
|
397
|
+
if (location.is_archived) {
|
|
476
398
|
throw errValidation({
|
|
477
|
-
field
|
|
478
|
-
msg:
|
|
399
|
+
field,
|
|
400
|
+
msg: msgArchivedLocation,
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
function locationID(location, requested_id, field, msg) {
|
|
405
|
+
const id = requiredID(location.id, field, msgLocationIDRequired);
|
|
406
|
+
if (resolvedID(requested_id) !== id) {
|
|
407
|
+
throw errValidation({ field, msg });
|
|
408
|
+
}
|
|
409
|
+
return id;
|
|
410
|
+
}
|
|
411
|
+
function validateExpectedEnd(opts) {
|
|
412
|
+
if (isZeroDate(opts.end)) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (compareLocalDate(opts.end, opts.start) < 0) {
|
|
416
|
+
throw errValidation({
|
|
417
|
+
field: opts.field,
|
|
418
|
+
msg: msgExpectedEndBeforeStart,
|
|
479
419
|
});
|
|
480
420
|
}
|
|
481
|
-
validateLocationStart({
|
|
482
|
-
start: opts.input,
|
|
483
|
-
location: opts.location,
|
|
484
|
-
field: "in",
|
|
485
|
-
msg: msgJobStartOutsideLocation,
|
|
486
|
-
});
|
|
487
421
|
}
|
|
488
422
|
function validateProgramStart(opts) {
|
|
489
|
-
const
|
|
423
|
+
const timezone = locationTimezone(opts.location, "location");
|
|
424
|
+
const now = localDateParts(opts.now, timezone);
|
|
490
425
|
if (now === undefined) {
|
|
491
426
|
throw errValidation({
|
|
492
|
-
field: "
|
|
427
|
+
field: "location",
|
|
493
428
|
msg: msgTimezoneInvalid,
|
|
494
429
|
});
|
|
495
430
|
}
|
|
496
|
-
|
|
497
|
-
|
|
431
|
+
const current_day = sameCalendarDay(opts.start, now);
|
|
432
|
+
if (!current_day && compareLocalDate(opts.start, now) < 0) {
|
|
498
433
|
throw errValidation({
|
|
499
434
|
field: "in",
|
|
500
435
|
msg: msgProgramStartPast,
|
|
501
436
|
});
|
|
502
437
|
}
|
|
503
|
-
|
|
504
|
-
start: opts.input.job_start_date,
|
|
505
|
-
location: opts.location,
|
|
506
|
-
field: "in",
|
|
507
|
-
msg: msgProgramStartOutsideLocation,
|
|
508
|
-
});
|
|
438
|
+
return current_day;
|
|
509
439
|
}
|
|
510
|
-
function
|
|
511
|
-
if (
|
|
512
|
-
|
|
513
|
-
}
|
|
514
|
-
const start = localDateFromLocation(opts.location);
|
|
515
|
-
const end = opts.location.end_date;
|
|
516
|
-
if (compareLocalDate(end, start) < 0) {
|
|
517
|
-
throw errValidation({
|
|
518
|
-
field: opts.field,
|
|
519
|
-
msg: msgLocationRangeInvalid,
|
|
520
|
-
});
|
|
440
|
+
function validateDuration(v, field) {
|
|
441
|
+
if (!Number.isFinite(v) || !Number.isInteger(v) || v < 0) {
|
|
442
|
+
throw errValidation({ field, msg: msgProgramDurationInvalid });
|
|
521
443
|
}
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
msg: opts.msg,
|
|
527
|
-
});
|
|
444
|
+
}
|
|
445
|
+
function validateRecurrenceRate(v, field) {
|
|
446
|
+
if (!Number.isFinite(v) || !Number.isInteger(v) || v <= 0) {
|
|
447
|
+
throw errValidation({ field, msg: msgRecurrenceRateInvalid });
|
|
528
448
|
}
|
|
529
449
|
}
|
|
530
|
-
function
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
450
|
+
function normalizeRecurrenceEnd(opts) {
|
|
451
|
+
switch (opts.end_type) {
|
|
452
|
+
case RecurrenceEndTypeIndefinite:
|
|
453
|
+
return {
|
|
454
|
+
occurrences_to_end: 0,
|
|
455
|
+
recurrence_end_date: zeroDate(),
|
|
456
|
+
};
|
|
457
|
+
case RecurrenceEndTypeAfterOccurrences:
|
|
458
|
+
if (!Number.isFinite(opts.occurrences_to_end) ||
|
|
459
|
+
!Number.isInteger(opts.occurrences_to_end) ||
|
|
460
|
+
opts.occurrences_to_end <= 0) {
|
|
461
|
+
throw errValidation({
|
|
462
|
+
field: opts.field,
|
|
463
|
+
msg: msgOccurrencesInvalid,
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
return {
|
|
467
|
+
occurrences_to_end: opts.occurrences_to_end,
|
|
468
|
+
recurrence_end_date: zeroDate(),
|
|
469
|
+
};
|
|
470
|
+
case RecurrenceEndTypeAfterDate:
|
|
471
|
+
if (isZeroDate(opts.recurrence_end_date)) {
|
|
472
|
+
throw errRequired({
|
|
473
|
+
field: opts.field,
|
|
474
|
+
msg: msgRecurrenceEndDateRequired,
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
return {
|
|
478
|
+
occurrences_to_end: 0,
|
|
479
|
+
recurrence_end_date: normalizeBackendDate(opts.recurrence_end_date),
|
|
480
|
+
};
|
|
481
|
+
default:
|
|
482
|
+
throw errValidation({
|
|
483
|
+
field: opts.field,
|
|
484
|
+
msg: msgRecurrenceEndTypeInvalid,
|
|
485
|
+
});
|
|
537
486
|
}
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
487
|
+
}
|
|
488
|
+
function sameCalendarDay(a, b) {
|
|
489
|
+
return a.y === b.y && a.m === b.m && a.d === b.d;
|
|
541
490
|
}
|
|
542
491
|
function locationTimezone(location, field) {
|
|
543
|
-
const out = location.
|
|
492
|
+
const out = location.timezone.trim();
|
|
544
493
|
if (out === "") {
|
|
545
|
-
throw errValidation({
|
|
546
|
-
field,
|
|
547
|
-
msg: msgTimezoneInvalid,
|
|
548
|
-
});
|
|
494
|
+
throw errValidation({ field, msg: msgTimezoneInvalid });
|
|
549
495
|
}
|
|
550
496
|
return out;
|
|
551
497
|
}
|
|
552
|
-
function localDateFromLocation(location) {
|
|
553
|
-
return {
|
|
554
|
-
y: location.y,
|
|
555
|
-
m: location.m,
|
|
556
|
-
d: location.d,
|
|
557
|
-
h: location.h,
|
|
558
|
-
min: location.min,
|
|
559
|
-
};
|
|
560
|
-
}
|
|
561
498
|
function localDateParts(now, timezone) {
|
|
562
|
-
const time_zone = timezone.trim();
|
|
563
|
-
if (time_zone === "") {
|
|
564
|
-
return undefined;
|
|
565
|
-
}
|
|
566
499
|
let parts;
|
|
567
500
|
try {
|
|
568
501
|
parts = new Intl.DateTimeFormat("en-CA", {
|
|
569
|
-
timeZone:
|
|
502
|
+
timeZone: timezone,
|
|
570
503
|
year: "numeric",
|
|
571
504
|
month: "numeric",
|
|
572
505
|
day: "numeric",
|
|
@@ -590,13 +523,7 @@ function localDateParts(now, timezone) {
|
|
|
590
523
|
min === undefined) {
|
|
591
524
|
return undefined;
|
|
592
525
|
}
|
|
593
|
-
return {
|
|
594
|
-
y,
|
|
595
|
-
m,
|
|
596
|
-
d,
|
|
597
|
-
h,
|
|
598
|
-
min,
|
|
599
|
-
};
|
|
526
|
+
return { y, m, d, h, min };
|
|
600
527
|
}
|
|
601
528
|
function compareLocalDate(a, b) {
|
|
602
529
|
for (const [left, right] of [
|
|
@@ -644,10 +571,7 @@ function locationAddressInput(location) {
|
|
|
644
571
|
function requiredID(v, field, msg) {
|
|
645
572
|
const out = resolvedID(v);
|
|
646
573
|
if (out === undefined) {
|
|
647
|
-
throw errRequired({
|
|
648
|
-
field,
|
|
649
|
-
msg,
|
|
650
|
-
});
|
|
574
|
+
throw errRequired({ field, msg });
|
|
651
575
|
}
|
|
652
576
|
return out;
|
|
653
577
|
}
|
|
@@ -655,8 +579,8 @@ function normalizeIDs(ids) {
|
|
|
655
579
|
const seen = new Set();
|
|
656
580
|
const out = [];
|
|
657
581
|
for (const raw of ids) {
|
|
658
|
-
const id = raw
|
|
659
|
-
if (id ===
|
|
582
|
+
const id = resolvedID(raw);
|
|
583
|
+
if (id === undefined || seen.has(id)) {
|
|
660
584
|
continue;
|
|
661
585
|
}
|
|
662
586
|
seen.add(id);
|
|
@@ -4,6 +4,7 @@
|
|
|
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 { PunchModeDefault, PunchModeDelete, PunchModeReplace, PunchModeWeekSheet, PunchStatusPending, PunchStatusWorking, PunchTypeIn, PunchTypeOut, PunchTypeRideIn, PunchTypeRideOut, ZeroID, } from "@fabriktor/schema";
|
|
7
|
+
import { zeroDate } from "../core/date.js";
|
|
7
8
|
import { errRequired, errResolveFailed, errValidation, } from "../core/err.js";
|
|
8
9
|
const allWeekDays = [0, 1, 2, 3, 4, 5, 6];
|
|
9
10
|
const minuteStep = 15;
|
|
@@ -361,17 +362,6 @@ function readyToPunchInControls() {
|
|
|
361
362
|
can_ride_out: false,
|
|
362
363
|
};
|
|
363
364
|
}
|
|
364
|
-
function zeroDate() {
|
|
365
|
-
return {
|
|
366
|
-
utc: "",
|
|
367
|
-
y: 0,
|
|
368
|
-
m: 0,
|
|
369
|
-
d: 0,
|
|
370
|
-
h: 0,
|
|
371
|
-
min: 0,
|
|
372
|
-
tz: "",
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
365
|
function cloneDate(v) {
|
|
376
366
|
return {
|
|
377
367
|
utc: v.utc,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabriktor/fx",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.77",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"typescript": "^6.0.3"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@fabriktor/client": "0.0.
|
|
21
|
-
"@fabriktor/schema": "0.0.
|
|
20
|
+
"@fabriktor/client": "0.0.68",
|
|
21
|
+
"@fabriktor/schema": "0.0.50"
|
|
22
22
|
}
|
|
23
23
|
}
|