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.
outline/server/exporter.js

61 lines
1.3 KiB
JavaScript

// @flow
import debug from "debug";
import mailer from "./mailer";
import { Collection, Team } from "./models";
import { createQueue } from "./utils/queue";
const log = debug("exporter");
const exporterQueue = createQueue("exporter");
const queueOptions = {
attempts: 2,
removeOnComplete: true,
backoff: {
type: "exponential",
delay: 60 * 1000,
},
};
async function exportAndEmailCollections(teamId: string, email: string) {
log("Archiving team", teamId);
const { archiveCollections } = require("./utils/zip");
const team = await Team.findByPk(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,
},
],
});
}
exporterQueue.process(async function exportProcessor(job) {
log("Process", job.data);
switch (job.data.type) {
case "export-collections":
return await exportAndEmailCollections(job.data.teamId, job.data.email);
default:
}
});
export const exportCollections = (teamId: string, email: string) => {
exporterQueue.add(
{
type: "export-collections",
teamId,
email,
},
queueOptions
);
};