-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatePackage.js
109 lines (99 loc) · 3.38 KB
/
updatePackage.js
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
import { update, ref } from 'firebase/database';
import { db, getUserID } from '../helpers/fbHelper';
import { logger } from './ConsoleLogger';
import { gaTimingStart, gaTimingEnd } from './gaHelper';
import { validatePackage } from './validatePackageData';
/**
* Checks to see if a value has been updated.
*
* @param {string} key Object key to check.
* @param {object} pkgBefore Package data before edit
* @param {object} pkgAfter Package data after edit.
* @return {boolean} true if the field has been updated.
*/
function _hasValueChanged(key, pkgBefore, pkgAfter) {
if (pkgBefore[key] === undefined && pkgAfter[key] === null) {
return false;
}
if (pkgBefore[key] === pkgAfter[key]) {
return false;
}
return true;
}
/**
* Returns a new object with only the updated properties.
*
* @param {object} pkgBefore Package data before edit.
* @param {object} pkgAfter Package data after edit.
* @return {object} object with only the updated properties.
*/
function _getChanges(pkgBefore, pkgAfter) {
const pkgChanges = {};
if (_hasValueChanged('dateExpected', pkgBefore, pkgAfter)) {
pkgChanges.dateExpected = pkgAfter.dateExpected;
}
if (_hasValueChanged('dateDelivered', pkgBefore, pkgAfter)) {
pkgChanges.dateDelivered = pkgAfter.dateDelivered;
}
if (_hasValueChanged('from', pkgBefore, pkgAfter)) {
pkgChanges.from = pkgAfter.from;
}
if (_hasValueChanged('what', pkgBefore, pkgAfter)) {
pkgChanges.what = pkgAfter.what;
}
if (_hasValueChanged('shipper', pkgBefore, pkgAfter)) {
pkgChanges.shipper = pkgAfter.shipper;
}
if (_hasValueChanged('trackingNumber', pkgBefore, pkgAfter)) {
pkgChanges.trackingNumber = pkgAfter.trackingNumber;
}
if (_hasValueChanged('trackingURL', pkgBefore, pkgAfter)) {
pkgChanges.trackingURL = pkgAfter.trackingURL;
pkgChanges.shipper = pkgAfter.shipper;
}
if (_hasValueChanged('orderURL', pkgBefore, pkgAfter)) {
pkgChanges.orderURL = pkgAfter.orderURL;
}
if (_hasValueChanged('amzOrderID', pkgBefore, pkgAfter)) {
pkgChanges.amzOrderID = pkgAfter.amzOrderID;
}
return pkgChanges;
}
/**
* Updates the package record in the database.
*
* @param {string} kind incoming or delivered
* @param {string} id Package ID
* @param {object} pkgBefore Pre-edit version of package data
* @param {object} pkgAfter Post-edit version of package data
* @return {Promise<null>} Successful update complete
*/
export default async function updatePackage(kind, id, pkgBefore, pkgAfter) {
const _perfName = 'fb_update_package';
const userID = getUserID();
if (!userID) {
throw new Error('Not Authenticated');
}
if (!kind || !id || !pkgBefore || !pkgAfter) {
throw new Error(`Missing or invalid required param.`);
}
gaTimingStart(_perfName);
const changes = _getChanges(pkgBefore, pkgAfter);
const keysChanged = Object.keys(changes);
if (keysChanged.length === 0) {
logger.log('updatePackage', 'Update skipped, nothing changed.');
return;
}
const errors = validatePackage(changes, false);
if (errors.length > 0) {
logger.error('Validation failed.', changes, errors);
throw new Error('Validation failed');
}
changes.dtUpdated = Date.now();
const queryPath = `userData/${userID}/data_v1/${kind}/${id}`;
logger.log('updatePackage', queryPath, changes);
const fbRef = ref(db, queryPath);
const r = await update(fbRef, changes);
gaTimingEnd(_perfName);
return r;
}