createTraining: async (root, { input }, context) => {
const { user } = context;
const coach = await Coach.findOne({ user });
const { coverPhoto, promotionalVideo, ...properties } = input;
async function uploadFile(file, path) {
const { stream, filename } = await file;
const photoFilename = getUploadPath(path, filename);
await storage.Save({
stream: coverPhotoStream,
filename: photoFilename
});
}
await Promise.all([
uploadFile(coverPhoto, 'training_cover'),
uploadFile(promotionalVideo, 'training_videos')
]);
const training = new Training({
coach,
coverPhoto: coverPhotoFilename,
promotionalVideo: videoFilename,
...properties
});
await trainingSave();
return { training };
}