From 9383b00c27e76339b2184096e0bbafce4917b7d6 Mon Sep 17 00:00:00 2001 From: DAB0mB Date: Sat, 23 Apr 2016 14:16:14 +0300 Subject: [PATCH] Step 3.10: Mock initial data in chats and messages collections --- api/server/bootstrap.js | 66 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 api/server/bootstrap.js diff --git a/api/server/bootstrap.js b/api/server/bootstrap.js new file mode 100644 index 0000000..e1463b4 --- /dev/null +++ b/api/server/bootstrap.js @@ -0,0 +1,66 @@ +import Moment from 'moment'; +import { Meteor } from 'meteor/meteor'; +import { Chats, Messages } from './collections'; + +Meteor.startup(function() { + if (Chats.find().count() !== 0) return; + + Messages.remove({}); + + const messages = [ + { + text: 'You on your way?', + timestamp: Moment().subtract(1, 'hours').toDate() + }, + { + text: 'Hey, it\'s me', + timestamp: Moment().subtract(2, 'hours').toDate() + }, + { + text: 'I should buy a boat', + timestamp: Moment().subtract(1, 'days').toDate() + }, + { + text: 'Look at my mukluks!', + timestamp: Moment().subtract(4, 'days').toDate() + }, + { + text: 'This is wicked good ice cream.', + timestamp: Moment().subtract(2, 'weeks').toDate() + } + ]; + + messages.forEach((m) => { + Messages.insert(m); + }); + + const chats = [ + { + name: 'Ethan Gonzalez', + picture: 'https://randomuser.me/api/portraits/thumb/men/1.jpg' + }, + { + name: 'Bryan Wallace', + picture: 'https://randomuser.me/api/portraits/thumb/lego/1.jpg' + }, + { + name: 'Avery Stewart', + picture: 'https://randomuser.me/api/portraits/thumb/women/1.jpg' + }, + { + name: 'Katie Peterson', + picture: 'https://randomuser.me/api/portraits/thumb/women/2.jpg' + }, + { + name: 'Ray Edwards', + picture: 'https://randomuser.me/api/portraits/thumb/men/2.jpg' + } + ]; + + chats.forEach((chat) => { + const message = Messages.findOne({ chatId: { $exists: false } }); + chat.lastMessage = message; + const chatId = Chats.insert(chat); + Messages.update(message._id, { $set: { chatId } }); + }); +}); \ No newline at end of file