1 | export var ActivationStatus;
|
2 | (function (ActivationStatus) {
|
3 | ActivationStatus[ActivationStatus["SUCCESS"] = 0] = "SUCCESS";
|
4 | ActivationStatus[ActivationStatus["WARNING"] = 1] = "WARNING";
|
5 | ActivationStatus[ActivationStatus["ERROR"] = 2] = "ERROR";
|
6 | })(ActivationStatus || (ActivationStatus = {}));
|
7 | export function buildSalesPackages(packages, products, addons) {
|
8 | if (!packages || packages.length < 1) {
|
9 | return [];
|
10 | }
|
11 | return packages.map(function (pkg) { return buildSalesPackageFromPackage(pkg, products, addons); });
|
12 | }
|
13 | function buildSalesPackageFromPackage(pkg, products, addons) {
|
14 | if (!pkg) {
|
15 | return null;
|
16 | }
|
17 | var salesProducts = buildSalesProducts(pkg, products, addons);
|
18 |
|
19 | var addonsWithNoParent = buildSalesAddonsWithNoParent(pkg, products, addons);
|
20 | return createSalesPackage(pkg, salesProducts, addonsWithNoParent);
|
21 | }
|
22 | export function buildSalesProducts(pkg, products, addons) {
|
23 | if (!(products && products.length && pkg.products && pkg.products.length)) {
|
24 | return [];
|
25 | }
|
26 | var productMap = new Map();
|
27 | products.forEach(function (product) { return productMap.set(product.productId, product); });
|
28 | return pkg.products
|
29 | .map(function (productId) {
|
30 | var productMatch = productMap.get(productId);
|
31 | var addonsInPackage = filterAddonsByAddonKeys(addons, pkg.addon_keys);
|
32 | return productMatch ? buildSalesProductFromProduct(productMatch, addonsInPackage) : null;
|
33 | })
|
34 | .filter(function (productItem) { return productItem; });
|
35 | }
|
36 | export function filterAddonsByAddonKeys(addons, addonKeys) {
|
37 | if (!addons || addons.length === 0 || !addonKeys || addonKeys.length === 0) {
|
38 | return [];
|
39 | }
|
40 | var addonIdSet = addonKeys.reduce(function (keys, key) { return keys.add(key.addonId); }, new Set());
|
41 | return addons.filter(function (addon) { return addonIdSet.has(addon.addonId); });
|
42 | }
|
43 | function buildSalesAddonsWithNoParent(pkg, products, addons) {
|
44 | if (!addons || addons.length < 1 || !pkg.addon_keys) {
|
45 | return [];
|
46 | }
|
47 | var productMap = new Map();
|
48 | products.forEach(function (product) { return productMap.set(product.productId, product); });
|
49 | var addonsMap = new Map();
|
50 | addons.forEach(function (addon) { return addonsMap.set(addon.addonId, addon); });
|
51 | var addonsWithNoParent = pkg.addon_keys.map(function (addonKey) {
|
52 | if (pkg.products && pkg.products.some(function (productId) { return productId === addonKey.appId; })) {
|
53 | return null;
|
54 | }
|
55 | var parentProduct = productMap.get(addonKey.appId);
|
56 | var parentName = parentProduct ? parentProduct.name : null;
|
57 | var parentIcon = parentProduct ? parentProduct.iconUrl : null;
|
58 | return createSalesAddon(addonsMap.get(addonKey.addonId), parentName, parentIcon);
|
59 | }).filter(function (addonItem) { return addonItem; });
|
60 | return addonsWithNoParent;
|
61 | }
|
62 | function buildSalesProductFromProduct(product, addons) {
|
63 | if (!product) {
|
64 | return null;
|
65 | }
|
66 | if (!addons || addons.length < 1) {
|
67 | return createSalesProduct(product, null);
|
68 | }
|
69 | var productAddons = addons.filter(function (addon) { return addon.appId === product.productId; });
|
70 | var salesAddons = createSalesAddons(productAddons);
|
71 | return createSalesProduct(product, salesAddons);
|
72 | }
|
73 | function createSalesPackage(pkg, salesProducts, addonsWithNoParent) {
|
74 | return {
|
75 | formattedPrices: pkg.formattedPricingList.join(', '),
|
76 | packageId: pkg.solution_id,
|
77 | name: pkg.name,
|
78 | icon: pkg.icon,
|
79 | productIds: pkg.products,
|
80 | products: salesProducts || [],
|
81 | addonsWithNoParent: addonsWithNoParent || [],
|
82 | totalAddons: pkg.addon_keys ? pkg.addon_keys.length : 0
|
83 | };
|
84 | }
|
85 | function createSalesProduct(product, salesAddons) {
|
86 | return {
|
87 | productId: product.productId,
|
88 | name: product.name,
|
89 | tagline: product.tagline || '',
|
90 | icon: product.iconUrl,
|
91 | addons: salesAddons || []
|
92 | };
|
93 | }
|
94 | function createSalesAddons(addons) {
|
95 | return addons.map(function (addon) { return createSalesAddon(addon); });
|
96 | }
|
97 | function createSalesAddon(addon, parentName, parentIcon) {
|
98 | return {
|
99 | parentId: addon.appId,
|
100 | addonId: addon.addonId,
|
101 | name: addon.title,
|
102 | tagline: addon.tagline || '',
|
103 | icon: addon.icon || '',
|
104 | parentIcon: parentIcon || '',
|
105 | parentName: parentName || ''
|
106 | };
|
107 | }
|
108 | function createSalesPackageWithStatus(pkg, products, addons, activationStatus) {
|
109 | return {
|
110 | formattedPrices: pkg.formattedPrices,
|
111 | packageId: pkg.packageId,
|
112 | name: pkg.name,
|
113 | icon: pkg.icon,
|
114 | productIds: pkg.productIds,
|
115 | products: products || [],
|
116 | addonsWithNoParent: addons || [],
|
117 | activationStatus: activationStatus || null,
|
118 | totalAddons: pkg.totalAddons
|
119 | };
|
120 | }
|
121 | function createSalesProductWithStatus(product, addons, activationStatus) {
|
122 | return {
|
123 | productId: product.productId,
|
124 | name: product.name,
|
125 | tagline: product.tagline,
|
126 | icon: product.icon,
|
127 | addons: addons || [],
|
128 | activationStatus: activationStatus || null
|
129 | };
|
130 | }
|
131 | function createSalesAddonWithStatus(addon, activationStatus) {
|
132 | return {
|
133 | parentId: addon.parentId,
|
134 | addonId: addon.addonId,
|
135 | name: addon.name,
|
136 | tagline: addon.tagline,
|
137 | icon: addon.icon,
|
138 | parentIcon: addon.parentIcon,
|
139 | parentName: addon.parentName,
|
140 | activationStatus: activationStatus || null
|
141 | };
|
142 | }
|
143 | export function addStatusesToSalesPackages(salesPackages, statuses) {
|
144 | if (!statuses || statuses.length === 0) {
|
145 | return salesPackages;
|
146 | }
|
147 | var activationStatusMap = new Map();
|
148 | statuses.forEach(function (status) { return activationStatusMap.set(status.packageItemId, status.status); });
|
149 | return salesPackages.map(function (salesPackage) {
|
150 | var newProducts = salesPackage.products.map(function (product) { return addStatusesToSalesProduct(product, activationStatusMap); });
|
151 | var newAddonsWithNoParent = salesPackage.addonsWithNoParent.map(function (addon) { return addStatusesToSalesAddon(addon, activationStatusMap); });
|
152 |
|
153 | var productStatuses = newProducts
|
154 | .map(function (product) { return product.activationStatus.status; });
|
155 | var addonStatuses = newProducts
|
156 | .map(function (product) { return product.addons ? product.addons.map(function (addon) { return addon.activationStatus.status; }) : []; })
|
157 | .reduce(function (statusList, reducedList) { return reducedList.concat(statusList); }, []);
|
158 | var addonsWithNoParentStatuses = newAddonsWithNoParent
|
159 | .map(function (addon) { return addon.activationStatus ? addon.activationStatus.status : null; });
|
160 | var allStatuses = productStatuses.concat(addonStatuses, addonsWithNoParentStatuses);
|
161 | var packageStatus = convertNumberToActivationStatus(Math.max.apply(Math, allStatuses));
|
162 | var statusNotification = createStatusNotificationItemForPackage(packageStatus);
|
163 | return createSalesPackageWithStatus(salesPackage, newProducts, newAddonsWithNoParent, statusNotification);
|
164 | });
|
165 | }
|
166 | function addStatusesToSalesProduct(product, activationStatusMap) {
|
167 | var productStatus = activationStatusMap.get(product.productId);
|
168 | var statusNotification = createStatusNotificationItemForPackageItem(productStatus);
|
169 | if (!product.addons) {
|
170 | return createSalesProductWithStatus(product, null, statusNotification);
|
171 | }
|
172 | var newSalesAddons = product.addons.map(function (addon) { return addStatusesToSalesAddon(addon, activationStatusMap); });
|
173 | return createSalesProductWithStatus(product, newSalesAddons, statusNotification);
|
174 | }
|
175 | function addStatusesToSalesAddon(addon, activationStatusMap) {
|
176 | var addonStatus = activationStatusMap.get(addon.addonId);
|
177 | var addonStatusNotification = createStatusNotificationItemForPackageItem(addonStatus);
|
178 | return createSalesAddonWithStatus(addon, addonStatusNotification);
|
179 | }
|
180 | function createStatusNotificationItemForPackageItem(status) {
|
181 | switch (status) {
|
182 | case ActivationStatus.SUCCESS:
|
183 | return { icon: 'check_circle', status: status, text: 'Activated' };
|
184 | case ActivationStatus.WARNING:
|
185 | return { icon: 'warning', status: status, text: 'Already Activated' };
|
186 | default:
|
187 | return { icon: 'error', status: status, text: 'Activation Errors' };
|
188 | }
|
189 | }
|
190 | function createStatusNotificationItemForPackage(status) {
|
191 | switch (status) {
|
192 | case ActivationStatus.SUCCESS:
|
193 | return { icon: 'check_circle', status: status, text: 'Activated' };
|
194 | case ActivationStatus.WARNING:
|
195 | return { icon: 'warning', status: status, text: 'Activation Warnings' };
|
196 | default:
|
197 | return { icon: 'error', status: status, text: 'Activation Errors Occured' };
|
198 | }
|
199 | }
|
200 | function convertNumberToActivationStatus(number) {
|
201 | switch (number) {
|
202 | case 0:
|
203 | return ActivationStatus.SUCCESS;
|
204 | case 1:
|
205 | return ActivationStatus.WARNING;
|
206 | default:
|
207 | return ActivationStatus.ERROR;
|
208 | }
|
209 | }
|