Last active
April 15, 2021 12:10
-
-
Save RabbitWithoutaHat/283dbd5b6a0ad504f0159fb2d8244fe6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var COLLECTION_NAME_LINE_ITEM = "line-items"; | |
var LINEITEM_FIELD_OPTIONS = "options"; | |
var OPTION_TYPE_GROUP = "group"; | |
var OPTION_ADD = "add"; | |
var OPTION_PRICE = "price"; | |
var OPTION_PERCENTAGE = "percentage"; | |
function mapOptions(option) { | |
if (option.type === OPTION_TYPE_GROUP) { | |
return { | |
...option, | |
elements: option.elements.map(mapOptions), | |
}; | |
} | |
if (option.options) { | |
return { | |
...option, | |
options: option.options.map((item) => { | |
const { modifiers, ...rest } = item; | |
if (modifiers.price && modifiers.price.operation === OPTION_ADD) { | |
return { | |
...rest, | |
price: modifiers.price.value, | |
mod: OPTION_PRICE, | |
}; | |
} | |
if ( | |
modifiers.price && | |
modifiers.price.operation === OPTION_PERCENTAGE | |
) { | |
return { | |
...rest, | |
percentage: modifiers.price.value, | |
mod: OPTION_PERCENTAGE, | |
}; | |
} | |
return rest; | |
}), | |
}; | |
} | |
return option; | |
} | |
var lineItemCursor = db.getCollection(COLLECTION_NAME_LINE_ITEM).find({ | |
[LINEITEM_FIELD_OPTIONS]: { $exists: true }, | |
$where: "this." + LINEITEM_FIELD_OPTIONS + ".length > 0", | |
}); | |
while (lineItemCursor.hasNext()) { | |
var el = lineItemCursor.next(); | |
if ( | |
!el[LINEITEM_FIELD_OPTIONS] || | |
(Array.isArray(el[LINEITEM_FIELD_OPTIONS]) && | |
el[LINEITEM_FIELD_OPTIONS].length === 0) | |
) { | |
continue; | |
} | |
var optionGroupElements = el[LINEITEM_FIELD_OPTIONS].map(mapOptions); | |
db.getCollection(COLLECTION_NAME_LINE_ITEM).updateOne( | |
{ _id: el._id }, | |
{ $set: { [LINEITEM_FIELD_OPTIONS]: optionGroupElements } } | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var COLLECTION_NAME_LINE_ITEM = "line-items"; | |
var LINEITEM_FIELD_OPTIONS = "options"; | |
var OPTION_TYPE_GROUP = "group"; | |
var OPTION_ADD = "add"; | |
var OPTION_PERCENTAGE = "percentage"; | |
function mapOptions(option) { | |
if (option.type === OPTION_TYPE_GROUP) { | |
return { | |
...option, | |
elements: option.elements.map(mapOptions), | |
}; | |
} | |
if (option.options) { | |
return { | |
...option, | |
options: option.options.map((item) => { | |
const { price, percentage, mod, ...rest } = item; | |
if (price || percentage) { | |
return { | |
...rest, | |
modifiers: { | |
price: { | |
operation: | |
item.mod && item.mod === OPTION_PERCENTAGE | |
? OPTION_PERCENTAGE | |
: OPTION_ADD, | |
value: percentage || price, | |
}, | |
}, | |
}; | |
} | |
return { | |
...item, | |
modifiers: item.modifiers || {}, | |
}; | |
}), | |
}; | |
} | |
return option; | |
} | |
var lineItemCursor = db.getCollection(COLLECTION_NAME_LINE_ITEM).find({ | |
[LINEITEM_FIELD_OPTIONS]: { $exists: true }, | |
$where: "this." + LINEITEM_FIELD_OPTIONS + ".length > 0", | |
}); | |
while (lineItemCursor.hasNext()) { | |
var el = lineItemCursor.next(); | |
if ( | |
!el[LINEITEM_FIELD_OPTIONS] || | |
(Array.isArray(el[LINEITEM_FIELD_OPTIONS]) && | |
el[LINEITEM_FIELD_OPTIONS].length === 0) | |
) { | |
continue; | |
} | |
var optionGroupElements = el[LINEITEM_FIELD_OPTIONS].map(mapOptions); | |
db.getCollection(COLLECTION_NAME_LINE_ITEM).updateOne( | |
{ _id: el._id }, | |
{ $set: { [LINEITEM_FIELD_OPTIONS]: optionGroupElements } } | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment