This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/server/logistics.js
Tom Moor 8cbcb77486 Base model refactor (#810)
* Big upgrades

* WIP: Stash

* Stash, 30 flow errors left

* Downgrade mobx

* WIP

* When I understand the difference between class and instance methods

* 💚

* Fixes: File import
Model saving edge cases
pinning and starring docs
Collection editing
Upgrade mobx devtools

* Notification settings saving works

* Disabled settings

* Document mailer

* Working notifications

* Colletion created notification
Ensure not notified for own actions

* Tidy up

* Document updated event only for document creation
Add indexes
Notification setting on user creation

* Commentary

* Fixed: Notification setting on signup

* Fix document move / duplicate stale data
Add BaseModel.refresh method

* Fixes: Title in sidebar not updated after editing document

* 💚

* Improve / restore error handling
Better handle offline errors

* 👕
2018-12-04 22:24:30 -08:00

95 lines
2.1 KiB
JavaScript

// @flow
import Queue from 'bull';
import debug from 'debug';
import mailer from './mailer';
import { Collection, Team } from './models';
import { archiveCollection, archiveCollections } from './utils/zip';
const log = debug('logistics');
const logisticsQueue = new Queue('logistics', process.env.REDIS_URL);
const queueOptions = {
attempts: 2,
removeOnComplete: true,
backoff: {
type: 'exponential',
delay: 60 * 1000,
},
};
async function exportAndEmailCollection(collectionId: string, email: string) {
log('Archiving collection', collectionId);
const collection = await Collection.findById(collectionId);
const filePath = await archiveCollection(collection);
log('Archive path', filePath);
mailer.export({
to: email,
attachments: [
{
filename: `${collection.name} Export.zip`,
path: filePath,
},
],
});
}
async function exportAndEmailCollections(teamId: string, email: string) {
log('Archiving team', teamId);
const team = await Team.findById(teamId);
const collections = await Collection.findAll({
where: { teamId },
order: [['name', 'ASC']],
});
const filePath = await archiveCollections(collections);
log('Archive path', filePath);
mailer.export({
to: email,
attachments: [
{
filename: `${team.name} Export.zip`,
path: filePath,
},
],
});
}
logisticsQueue.process(async job => {
log('Process', job.data);
switch (job.data.type) {
case 'export-collection':
return await exportAndEmailCollection(
job.data.collectionId,
job.data.email
);
case 'export-collections':
return await exportAndEmailCollections(job.data.teamId, job.data.email);
default:
}
});
export const exportCollection = (collectionId: string, email: string) => {
logisticsQueue.add(
{
type: 'export-collection',
collectionId,
email,
},
queueOptions
);
};
export const exportCollections = (teamId: string, email: string) => {
logisticsQueue.add(
{
type: 'export-collections',
teamId,
email,
},
queueOptions
);
};