merchi_sdk_js 0.6.0 → 0.7.0
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/package.json +4 -1
- package/src/job.js +2 -0
- package/src/job_note.js +24 -1
- package/src/job_operation_log.js +65 -0
- package/src/merchi.js +9 -0
- package/src/notification.js +1 -0
- package/src/pricing/__fixtures__/conditional_field_hidden.json +112 -0
- package/src/pricing/__fixtures__/conditional_field_visible.json +113 -0
- package/src/pricing/__fixtures__/group_product.json +130 -0
- package/src/pricing/__fixtures__/non_selectable_number_field.json +108 -0
- package/src/pricing/__fixtures__/quantity_break_discount.json +104 -0
- package/src/pricing/__fixtures__/simple_selectable_field.json +110 -0
- package/src/pricing/discount.js +14 -0
- package/src/pricing/estimate.js +172 -0
- package/src/pricing/golden.test.js +54 -0
- package/src/pricing/index.js +5 -0
- package/src/pricing/inventory.js +73 -0
- package/src/pricing/pricing.test.js +77 -0
- package/src/pricing/round.js +19 -0
- package/src/pricing/visibility.js +90 -0
- package/src/user.js +2 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
function collectSelectedOptionIds(selections) {
|
|
2
|
+
const ids = [];
|
|
3
|
+
const addFrom = (fieldValues) => {
|
|
4
|
+
for (const key of Object.keys(fieldValues)) {
|
|
5
|
+
const sel = fieldValues[Number(key)];
|
|
6
|
+
if (sel && sel.selectedOptionIds) ids.push(...sel.selectedOptionIds);
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
addFrom(selections.fieldValues || {});
|
|
10
|
+
for (const group of selections.groups || []) addFrom(group.fieldValues || {});
|
|
11
|
+
return ids;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Shared machinery for field- and option-level conditional visibility. Mirrors
|
|
15
|
+
// the server's `check_selected_by_fullfilled` recursion: a `selectedBy` list is
|
|
16
|
+
// fulfilled when any of its trigger option ids is currently selected AND that
|
|
17
|
+
// selecting option is itself fulfilled (its own `selectedBy` and its owning
|
|
18
|
+
// field's `selectedBy`), with a `checked` accumulator to break cycles.
|
|
19
|
+
function buildResolver(rules, selections) {
|
|
20
|
+
const allFields = [...rules.fields, ...rules.groupFields];
|
|
21
|
+
const optionsById = new Map();
|
|
22
|
+
const fieldByOptionId = new Map();
|
|
23
|
+
for (const f of allFields) {
|
|
24
|
+
for (const o of f.options) {
|
|
25
|
+
optionsById.set(o.id, o);
|
|
26
|
+
fieldByOptionId.set(o.id, f);
|
|
27
|
+
if (o.originalId != null) {
|
|
28
|
+
optionsById.set(o.originalId, o);
|
|
29
|
+
fieldByOptionId.set(o.originalId, f);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const selectedOptionIds = collectSelectedOptionIds(selections);
|
|
35
|
+
|
|
36
|
+
const sameOption = (a, b) => {
|
|
37
|
+
const oa = optionsById.get(a);
|
|
38
|
+
return oa != null && oa === optionsById.get(b);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const isFulfilled = (selectedBy, checked) => {
|
|
42
|
+
if (!selectedBy || selectedBy.length === 0) return true;
|
|
43
|
+
for (const triggerId of selectedBy) {
|
|
44
|
+
for (const selId of selectedOptionIds) {
|
|
45
|
+
if (!sameOption(triggerId, selId)) continue;
|
|
46
|
+
if (checked.includes(selId)) return true;
|
|
47
|
+
const opt = optionsById.get(selId);
|
|
48
|
+
const field = fieldByOptionId.get(selId);
|
|
49
|
+
const nextChecked = [...checked, selId];
|
|
50
|
+
if (
|
|
51
|
+
isFulfilled(opt.selectedBy, nextChecked) &&
|
|
52
|
+
isFulfilled(field.selectedBy, nextChecked)
|
|
53
|
+
) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Group fields only materialise as variations when the job actually has
|
|
62
|
+
// groups (mirrors the server: a non-group job exposes no group fields).
|
|
63
|
+
const hasGroups = Boolean(selections.groups && selections.groups.length > 0);
|
|
64
|
+
const fieldsToConsider = hasGroups ? allFields : rules.fields;
|
|
65
|
+
|
|
66
|
+
return { allFields, fieldsToConsider, isFulfilled };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function resolveVisibleFields(rules, selections) {
|
|
70
|
+
const { fieldsToConsider, isFulfilled } = buildResolver(rules, selections);
|
|
71
|
+
const visible = new Set();
|
|
72
|
+
for (const f of fieldsToConsider) {
|
|
73
|
+
if (isFulfilled(f.selectedBy, [])) visible.add(f.id);
|
|
74
|
+
}
|
|
75
|
+
return visible;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Option ids whose own `selectedBy` is fulfilled (i.e. options that should be
|
|
79
|
+
// visible/selectable). Mirrors the server's per-option
|
|
80
|
+
// `isVisible = check_selected_by_fullfilled(option.selected_by)`.
|
|
81
|
+
export function resolveVisibleOptionIds(rules, selections) {
|
|
82
|
+
const { fieldsToConsider, isFulfilled } = buildResolver(rules, selections);
|
|
83
|
+
const visible = new Set();
|
|
84
|
+
for (const f of fieldsToConsider) {
|
|
85
|
+
for (const o of f.options) {
|
|
86
|
+
if (isFulfilled(o.selectedBy, [])) visible.add(o.id);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return visible;
|
|
90
|
+
}
|
package/src/user.js
CHANGED
|
@@ -18,6 +18,7 @@ import { UserCompany } from './user_company.js';
|
|
|
18
18
|
import { Reminder } from './reminder.js';
|
|
19
19
|
import { InternalTag } from './internal_tag.js';
|
|
20
20
|
import { DomainTag } from './domain_tag.js';
|
|
21
|
+
import { JobOperationLog } from './job_operation_log.js';
|
|
21
22
|
|
|
22
23
|
export function User() {
|
|
23
24
|
this.resource = '/users';
|
|
@@ -62,6 +63,7 @@ export function User() {
|
|
|
62
63
|
addPropertyTo(this, 'tags', DomainTag);
|
|
63
64
|
addPropertyTo(this, 'internalTags', InternalTag);
|
|
64
65
|
addPropertyTo(this, 'reminders', Reminder);
|
|
66
|
+
addPropertyTo(this, 'jobOperationLogs', JobOperationLog);
|
|
65
67
|
|
|
66
68
|
this.create = function (success, error, embed, as_domain) {
|
|
67
69
|
var self = this,
|