-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.sync_function_run.js
More file actions
61 lines (46 loc) · 1.63 KB
/
3.sync_function_run.js
File metadata and controls
61 lines (46 loc) · 1.63 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
function(doc, oldDoc) {
var parts = [];
try {
parts = doc._id.split(":");
} catch (e) {
throw({forbidden: "error: invalid document ID format"});
}
var type = parts[0];
// Handle deletion
if (doc._deleted === true) {
requireRole(["editor", "admin"]);
return;
}else{
// Role check
if (type === "order") {
requireRole(["editor", "admin", "user"]);
} else if (type === "job") {
requireRole(["manager", "editor", "admin"]);
} else {
throw({forbidden: "error: invalid docType"});
}
// CHANNEL FIELD – CHANGE "channels" TO YOUR ACTUAL FIELD NAME BELOW
var ch = getChannels("channels"); // ←←← UPDATE THIS (e.g. "city", "tags", etc.)
if (!ch) {
// ONLY log when something is wrong — helps catch forgotten field names fast
console.log("SYNC FUNCTION WARNING: Field 'channels' is missing, null, empty, or has no valid values in document:", doc._id);
throw({forbidden: "error: required channel field is missing or empty"});
}
channel(ch);
}
}
function getChannels(field) {
var raw = doc[field];
if (raw === undefined || raw === null) {
return false;
}
var list = Array.isArray(raw) ? raw : [raw];
var clean = [];
for (var i = 0; i < list.length; i++) {
var item = list[i];
if (item === null || item === undefined) continue;
var s = String(item).trim();
if (s !== "") clean.push(s);
}
return clean.length > 0 ? clean : false;
}