Last active
August 29, 2015 14:27
-
-
Save haveanicedavid/73640ee89cc9978b9a10 to your computer and use it in GitHub Desktop.
This file contains 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
Meteor.methods({ | |
/* | |
* Push an event to a specific variant | |
* params: | |
* variantId - the id of the variant to which we are adding a product event | |
* eventDoc - An object containing the information for the event | |
*/ | |
createProductEvent: function(variantId, eventDoc) { | |
var Products, product; | |
check(variantId, String); | |
check(eventDoc, { | |
title: String, | |
location: Match.Optional({ | |
address1: Match.Optional(String), | |
address2: Match.Optional(String), | |
city: Match.Optional(String), | |
region: Match.Optional(String), | |
postal: Match.Optional(String), | |
country: Match.Optional(String), | |
coords: Match.Optional({ | |
x: Number, | |
y: Number | |
}), | |
metafields: Match.Optional(Object) | |
}), | |
description: Match.Optional(String) | |
}); | |
if (!ReactionCore.hasPermission('createProductEvent')) { | |
throw new Meteor.Error(403, "Access Denied"); | |
} | |
this.unblock(); | |
_.defaults(eventDoc, { | |
_id: Random.id(), | |
createdAt: new Date() | |
}); | |
Products = ReactionCore.Collections.Products; | |
product = Products.findOne({ | |
"variants._id": variantId | |
}); | |
if (product !== null ? product.variants : void 0) { | |
return Products.update({ | |
"_id": product._id, | |
"variants._id": variantId | |
}, { | |
$push: { | |
"variants.$.events": eventDoc | |
} | |
}, { | |
validate: false | |
}); | |
} else { | |
throw new Meteor.Error(400, "Variant " + variantId + " not found"); | |
} | |
}, | |
/* | |
* Check inventory availability for a specific variant | |
* Params: | |
* productId, | |
* variantId, | |
* reservationRequest, | |
* <optional> quantity | |
* | |
* returns array of available variant ids or an empty array if none available | |
*/ | |
checkInventoryAvailability: function(productId, variantId, reservationRequest, quantity) { | |
var Products, product; | |
var result = []; | |
check(productId, String); | |
check(variantId, String); | |
check(reservationRequest, { | |
startTime: Date, | |
endTime: Date | |
}); | |
check(quantity, Match.Optional(Number)); | |
quantity = quantity || 1; | |
requestDates = dateRangeToArray(reservationRequest); | |
product = ReactionCore.Collections.Products.findOne({ | |
"product._id": productId | |
}); | |
var variantsToCheck = product.variants.filter(function(variant) { | |
return variant.parentId === variantId; | |
}); | |
for ( var i = 0; i < variantsToCheck.length; i++ ) { | |
var firstDay = variantsToCheck[i].unavailableDates[0]; | |
var lastDay = variantsToCheck[i].unavailableDates.slice(-1); | |
if (requestDates.indexOf(firstDay) !== -1 || requestDates.indexOf(lastDay !== -1)) { | |
result.push(variantsToCheck[i]._id); | |
} | |
} | |
return result; | |
} | |
}); | |
function dateRangeToArray (reservationRequest) { | |
requestDates = []; | |
iter = moment(reservationRequest.startTime) | |
.twix(reservationRequest.endTime, {allDay: true}) | |
.iterate('days'); | |
while ( iter.hasNext() ){ | |
requestDates.push(iter.next().toDate()); | |
} | |
return requestDates; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment