Skip to content

Instantly share code, notes, and snippets.

@maenthoven
Created January 7, 2020 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maenthoven/346caf5d122c61a8015427bb341080e0 to your computer and use it in GitHub Desktop.
Save maenthoven/346caf5d122c61a8015427bb341080e0 to your computer and use it in GitHub Desktop.
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Promise = require('bluebird');
mongoose.Promise = Promise;
const CalendarEntrySchema = new Schema({
url: String,
game: {
id: Number,
name: String
},
type: {
type: String,
uppercase: true,
default: 'RELEASE',
enum: ['RELEASE', 'EVENT']
},
imageSrc: String,
startDate: {type: Date, required: true, index: true},
endDate: Date,
tagLine: {type: String, required: true, index: {unique: true}},
priority: {type: Number, default: 0},
additionalInfo: Object
}, {timestamps: true});
const CalendarEntry = mongoose.model('CalendarEntry', CalendarEntrySchema, 'CalendarEntry');
module.exports = CalendarEntry;
const ical = require('ical-generator');
function saveCalendarICal(data, items) {
const icalConfig = {
domain: 'opencritic.com',
prodId: {
company: 'OpenCritic.com',
product: data.title
},
name: 'OpenCritic Gaming Calendar'
};
const events = items.filter(item => item.type === 'RELEASE')
.map(item => {
item.startDate = new Date(item.startDate);
return {
start: item.startDate,
end: new Date(item.startDate.getTime() + 3600000),
summary: item.tagLine + ' Release',
url: item.url,
allDay: true,
description: item.description
}
}).concat(
items.filter(item => item.type === 'EVENT')
.map(item => {
item.startDate = new Date(item.startDate);
item.endDate = new Date(item.endDate);
return {
start: item.startDate,
end: new Date(item.endDate.getTime() + 3600000),
summary: item.tagLine,
url: item.url
}
}));
const calendar = ical(icalConfig);
return calendar.events(events).toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment