-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (173 loc) · 6.08 KB
/
Copy pathscript.js
File metadata and controls
192 lines (173 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const toGoogleCalendarTimeRange = (
input,
isAllDay = false,
durationInMinutes = 30
) => {
const formatDateToGoogle = (dt) =>
dt.toISOString().replace(/[-:]/g, "").split(".")[0] + "Z";
const formatDateOnly = (dt) =>
dt.toISOString().slice(0, 10).replace(/-/g, "");
if (typeof input === "string") {
const startDate = new Date(input);
if (isAllDay) {
const endDate = new Date(startDate.getTime() + 86400000); // Add 24 hours
return `${formatDateOnly(startDate)}/${formatDateOnly(endDate)}`;
} else {
const endDate = new Date(startDate.getTime() + durationInMinutes * 60000);
return `${formatDateToGoogle(startDate)}/${formatDateToGoogle(endDate)}`;
}
}
if (input && typeof input === "object" && typeof input.label === "string") {
const [month, day, year] = input.label.split("/");
const startDate = new Date(`${year}-${month}-${day}T00:00:00`);
const endDate = new Date(startDate.getTime() + 86400000);
return `${formatDateOnly(startDate)}/${formatDateOnly(endDate)}`;
}
throw new Error("Date Value invalid");
};
const buildEventName = (record, object, fieldId) => {
const recordFields = Object.values(record.fields ?? {}) ?? [];
const fieldName = recordFields.find(
(field) => field.id === fieldId
)?.display_name;
let recordName = recordFields.find(
(field) => field.name === "display_name"
)?.value;
if (!recordName && recordFields.length) {
recordName = `${
recordFields.find((field) => field.name === "first_name")?.value
} ${recordFields.find((field) => field.name === "last_name")?.value}`;
}
if (!recordName && !fieldName && !object.object_name && !object.name) {
return "";
}
if (record.activityId) {
const recordNameResult = (recordName || record.display_name || "").replace(
/[^a-zA-Z0-9 _-]/g,
""
);
const objectNameResult = (object.object_name || object.name || "").replace(
/[^a-zA-Z0-9 _-]/g,
""
);
return `${objectNameResult}+Activity${
recordNameResult ? "+-+" + recordNameResult : ""
}`;
}
const objectNameResult = (object.object_name || object.name || "").replace(
/[^a-zA-Z0-9 _-]/g,
""
);
const recordNameResult = (recordName || "").replace(/[^a-zA-Z0-9 _-]/g, "");
const fieldNameResult = (fieldName || "").replace(/[^a-zA-Z0-9 _-]/g, "");
return `${objectNameResult}${
recordNameResult ? "+-+" + recordNameResult : ""
}${fieldNameResult ? "+-+" + fieldNameResult : ""}`;
};
const getAppBaseUrl = () => {
if (this.applicationPath.includes("integration")) {
return "https://v2.integration.kizen.dev";
} else if (this.applicationPath.includes("staging")) {
return "https://v2.staging.kizen.com";
} else if (this.applicationPath.includes("go")) {
return "https://go.kizen.com";
}
return "https://fmo.kizen.com";
};
const buildEventDetails = (record, object, activityId) => {
const baseUrl = getAppBaseUrl();
if (!object.id || !baseUrl) {
return "";
} else if (
activityId &&
object.id === this.currentBusiness.client_object.id &&
record.id
) {
return `View activity in Kizen: ${baseUrl}/client/${record.id}/details?view_scheduled_activity_id=${activityId}`;
} else if (activityId && record.id) {
return `View activity in Kizen: ${baseUrl}/custom-objects/${object.id}/${record.id}/details?view_scheduled_activity_id=${activityId}`;
} else if (activityId) {
return "";
} else if (object.id === this.currentBusiness.client_object.id) {
return `View record in Kizen: ${baseUrl}/client/${record.id}/details`;
}
return `View record in Kizen: ${baseUrl}/custom-objects/${object.id}/${record.id}/details`;
};
const getRecordUrl = (
objectId = "",
recordId = "",
fieldNames = "",
fieldIds = ""
) =>
`/records/${objectId}/${recordId}?field_names=${fieldNames}&field_ids=${fieldIds}`;
const getObjectUrl = (objectId = "") => `/custom-objects/${objectId}`;
const getActivtyUrl = (activityId = "") => `/activities/${activityId}`;
const getActivityFieldUrl = (objectId = "", fieldId = "") =>
`/activities/${objectId}/fields/${fieldId}`;
const getScheduledActivityUrl = (activityId) =>
`/activities/scheduled-activity/${activityId}`;
let record;
let activity = { name: "" };
let object = this.args.objectId
? await this.get(getObjectUrl(this.args.objectId))
: {};
if (this.args.activityId) {
const scheduledActivity = await this.get(
getScheduledActivityUrl(this.args.activityId)
);
if (scheduledActivity) {
activity = (await this.get(
getActivtyUrl(scheduledActivity.activity_object.id)
)) ?? { name: "" };
object = {
...object,
object_name: activity?.name || "",
};
}
switch (this.args.fieldId) {
case "due_datetime":
record = {
id: this.args.entityId,
display_name: "Due Date",
};
break;
case "due_datetime_my_time":
record = { id: this.args.entityId, display_name: "Due Date" };
break;
case "original_due_datetime":
record = { id: this.args.entityId, display_name: "Original Due Date" };
break;
default:
const fetchedRecord =
this.args.objectId && this.args.entityId
? (await this.get(
getRecordUrl(
this.args.objectId,
this.args.entityId,
"name",
this.args.fieldId
)
)) ?? {}
: {};
record = fetchedRecord;
break;
}
record = { ...record, activityId: this.args.activityId };
} else {
record =
(await this.get(
getRecordUrl(
this.args.objectId,
this.args.entityId,
"name",
this.args.fieldId
)
)) ?? {};
}
const isAllDayEvent = this.args.fieldType === "date";
const dateRange = toGoogleCalendarTimeRange(this.args.value, isAllDayEvent);
const eventName = buildEventName(record, object, this.args.fieldId);
const eventDetails = buildEventDetails(record, object, this.args.activityId);
this.openWindow(
`https://calendar.google.com/calendar/render?action=TEMPLATE&text=${eventName}&details=${eventDetails}&dates=${dateRange}&ctz=${this.currentBusiness.timezone.name}`
);