Last active
January 11, 2021 10:00
-
-
Save silveur/214d6566145d28965c215e2e07824228 to your computer and use it in GitHub Desktop.
Updating a CG listing
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
const editListing = async ({ config, user, options = {} }) => { | |
const configRef = config._id; | |
const listingRef = options._id; | |
const item = await Item.findOne({ configRef, "listings._id": listingRef }).exec(); | |
if (!item) throw new Error("Item not found"); | |
const listing = item.listings.find(l => l._id.equals(listingRef)); | |
if (!listing.id) listing.id = new Date().getTime(); | |
if (options.prices && options.prices.sale !== undefined) { | |
listing.prices = options.prices; | |
let changeStr = ""; | |
if (options.prices.sale !== listing.prices.sale) changeStr += `Sale price: ${options.prices.sale} `; | |
if (options.prices.cost !== listing.prices.cost) changeStr += `Cost price: ${options.prices.cost} `; | |
if (options.prices.compare !== listing.prices.compare) changeStr += `Compare price: ${options.prices.compare} `; | |
if (changeStr) | |
listing.logs.unshift({ | |
user: { ref: user, name: `${user.getName()}` }, | |
description: `Price update - ${changeStr}`, | |
action: "update", | |
date: new Date() | |
}); | |
} | |
if (options.options !== undefined) listing.options = options.options; | |
if (options.categories !== undefined) listing.categories = options.categories; | |
if (options.comments !== undefined) listing.comments = options.comments; | |
if (options.privateComments !== undefined) listing.privateComments = options.privateComments; | |
if (options.available !== undefined) listing.available = options.available; // check date validity | |
if (options.location !== undefined) listing.location = options.location; | |
if (options.status !== undefined) listing.status = options.status; | |
if (options.posted !== undefined) listing.posted = options.posted; | |
if (options.preventDiscogsListing !== undefined) listing.preventDiscogsListing = options.preventDiscogsListing; | |
if (options.supplierCode !== undefined) listing.supplierCode = options.supplierCode; | |
if (options.onePerCustomer !== undefined) listing.onePerCustomer = options.onePerCustomer; | |
if (options.taxDefinition !== undefined) listing.taxDefinition = options.taxDefinition; | |
if (options.stock && options.stock.quantity !== listing.stock.quantity) { | |
await new Promise((res, rej) => { | |
listing.updateStockQuantity({ item, config, quantity: options.stock.quantity, user }, (err, results) => { | |
if (err) rej(err); | |
else res(results); | |
}); | |
}); | |
} | |
let message; | |
if (listing.discogsId) | |
try { | |
await editDiscogsListing({ listing, item, config }); | |
message = `Listing updated. Discogs update: Ok`; | |
} catch (e) { | |
`Listing updated. Discogs update: ${e.toString()}`; | |
} | |
item.markModified("listings"); | |
await item.save(); | |
return { listings: item.listings, item: item.getDataForApi(config), listing, message }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment