feat: Allow export of collections as sync zip (#1013)

* feat: Allow export of collections as sync zip

* test: Add spec
This commit is contained in:
Tom Moor
2019-08-09 20:37:51 -07:00
committed by GitHub
parent d024d31f66
commit f87b561685
4 changed files with 49 additions and 42 deletions

View File

@ -7,7 +7,7 @@ import { presentCollection, presentUser } from '../presenters';
import { Collection, CollectionUser, Team, Event, User } from '../models';
import { ValidationError, InvalidRequestError } from '../errors';
import { exportCollections } from '../logistics';
import { archiveCollection } from '../utils/zip';
import { archiveCollection, archiveCollections } from '../utils/zip';
import policy from '../policies';
const { authorize } = policy;
@ -168,13 +168,12 @@ router.post('collections.export', auth(), async ctx => {
});
router.post('collections.exportAll', auth(), async ctx => {
const { download = false } = ctx.body;
const user = ctx.state.user;
const team = await Team.findByPk(user.teamId);
authorize(user, 'export', team);
// async operation to create zip archive and email user
exportCollections(user.teamId, user.email);
await Event.create({
name: 'collections.export',
teamId: user.teamId,
@ -182,9 +181,24 @@ router.post('collections.exportAll', auth(), async ctx => {
ip: ctx.request.ip,
});
ctx.body = {
success: true,
};
if (download) {
const collections = await Collection.findAll({
where: { teamId: team.id },
order: [['name', 'ASC']],
});
const filePath = await archiveCollections(collections);
ctx.attachment(`${team.name}.zip`);
ctx.set('Content-Type', 'application/force-download');
ctx.body = fs.createReadStream(filePath);
} else {
// async operation to create zip archive and email user
exportCollections(user.teamId, user.email);
ctx.body = {
success: true,
};
}
});
router.post('collections.update', auth(), async ctx => {