b9e0668d7d
* First pass (working) collection export to zip * Add export confirmation screen * 👕 * Refactor * Job for team export, move to tmp file, settings UI * Export all collections job * 👕 * Add specs * Clarify UI
95 lines
2.1 KiB
JavaScript
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 mailer = new Mailer();
|
|
const queueOptions = {
|
|
attempts: 2,
|
|
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
|
|
);
|
|
};
|