Skip to content

Commit dc92161

Browse files
committed
you get no or array of channels
1 parent aa0b241 commit dc92161

1 file changed

Lines changed: 47 additions & 29 deletions

File tree

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,61 @@
11
function(doc, oldDoc) {
2-
var parts;
32

3+
var parts = [];
4+
45
try {
5-
parts = doc._id.split("-");
6+
parts = doc._id.split(":");
67
} catch (e) {
7-
throw({forbidden: "Invalid document ID format"});
8+
throw({forbidden: "error: invalid document ID format"});
89
}
910

10-
// Handle tombstone (deleted document)
11-
if (doc._deleted === true) {
11+
var type = parts[0];
12+
13+
// Handle deletion
14+
if (doc._deleted === true) {
15+
1216
requireRole(["editor", "admin"]);
13-
return;
17+
return;
18+
19+
}else{
20+
21+
// Role check
22+
if (type === "order") {
23+
requireRole(["editor", "admin", "user"]);
24+
} else if (type === "job") {
25+
requireRole(["manager", "editor", "admin"]);
26+
} else {
27+
throw({forbidden: "error: invalid docType"});
28+
}
29+
30+
// CHANNEL FIELD – CHANGE "channels" TO YOUR ACTUAL FIELD NAME BELOW
31+
var ch = getChannels("channels"); // ←←← UPDATE THIS (e.g. "city", "tags", etc.)
32+
33+
if (!ch) {
34+
// ONLY log when something is wrong — helps catch forgotten field names fast
35+
console.log("SYNC FUNCTION WARNING: Field 'channels' is missing, null, empty, or has no valid values in document:", doc._id);
36+
throw({forbidden: "error: required channel field is missing or empty"});
37+
}
38+
39+
channel(ch);
1440
}
41+
}
1542

16-
var docType = parts[0];
17-
18-
if (docType === "order") {
19-
requireRole(["editor", "admin", "user"]);
20-
21-
} else if (docType === "job") {
22-
requireRole(["manager", "editor", "admin"]);
23-
24-
} else {
25-
throw({forbidden: "Invalid docType: " + docType});
26-
}
43+
function getChannels(field) {
44+
var raw = doc[field];
2745

28-
// Validate and assign channels
29-
if (!doc.channels || !Array.isArray(doc.channels) || doc.channels.length === 0) {
30-
throw({forbidden: "Document must have non-empty 'channels' array"});
46+
if (raw === undefined || raw === null) {
47+
return false;
3148
}
3249

33-
channel(doc.channels);
34-
}
50+
var list = Array.isArray(raw) ? raw : [raw];
51+
var clean = [];
3552

36-
// Improved fieldCheck – but actually you don't need it for channels if you validate above
37-
// Keep it only if you reuse it for other fields
38-
function requireField(value, fieldName) {
39-
if (value === undefined || value === null || value === "" ||
40-
(typeof value === "number") ||
41-
(Array.isArray(value) && value.length === 0)) {
42-
throw({forbidden: "Field '" + fieldName + "' is invalid or missing"});
53+
for (var i = 0; i < list.length; i++) {
54+
var item = list[i];
55+
if (item === null || item === undefined) continue;
56+
var s = String(item).trim();
57+
if (s !== "") clean.push(s);
4358
}
59+
60+
return clean.length > 0 ? clean : false;
61+
}

0 commit comments

Comments
 (0)