Skip to content

Commit

Permalink
Merge pull request #118 from nzzdev/release-4.1.1
Browse files Browse the repository at this point in the history
Release 4.1.1
  • Loading branch information
benib authored Jun 18, 2018
2 parents fbabd0a + 336591f commit 0b61951
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nzz/q-server",
"version": "4.1.0",
"version": "4.1.1",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
75 changes: 42 additions & 33 deletions plugins/core/base/routes/admin/migration.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const Boom = require('boom');
const Bounce = require('bounce');
const fetch = require('node-fetch');

const statusUpdated = 'updated';
const statusNotUpdated = 'not updated';
const statusFailed = 'failed';
const Boom = require("boom");
const Bounce = require("bounce");
const fetch = require("node-fetch");

const statusUpdated = "updated";
const statusNotUpdated = "not updated";
const statusFailed = "failed";

module.exports = {
path: '/admin/migration/{tool}/{id?}',
method: 'GET',
config: {
auth: 'q-auth',
path: "/admin/migration/{tool}/{id?}",
method: "GET",
config: {
auth: "q-auth",
cors: {
credentials: true
},
description: 'Executes migration of items in database for specified tool or for a single item with the specified id respectively',
tags: ['api']
description:
"Executes migration of items in database for specified tool or for a single item with the specified id respectively",
tags: ["api"]
},
handler: async (request, h) => {
const tool = request.params.tool;
Expand All @@ -30,13 +30,20 @@ module.exports = {
if (request.params.id) {
const ignoreInactive = true;
try {
const item = await request.methods.db.item.getById(request.params.id, ignoreInactive);
const item = await request.server.methods.db.item.getById(
request.params.id,
ignoreInactive
);
} catch (err) {
Bounce.rethrow(err, 'system');
Bounce.rethrow(err, "system");
return err;
}

const migrationStatus = await migrateItem(item, toolBaseUrl, server.app.db);
const migrationStatus = await migrateItem(
item,
toolBaseUrl,
request.server.app.db
);
return {
status: migrationStatus.status
};
Expand All @@ -45,46 +52,49 @@ module.exports = {

let migrationStatuses = items.map(async item => {
return await migrateItem(item, toolBaseUrl, server.app.db);
})
});

migrationStatuses = await Promise.all(migrationStatuses);

const stats = migrationStatuses.reduce((groupedStats, migrationStatus) => {
const status = migrationStatus.status;
if(!groupedStats[status]) {
groupedStats[status] = [];
}
groupedStats[status].push(migrationStatus.id);
return groupedStats;
}, {});

const stats = migrationStatuses.reduce(
(groupedStats, migrationStatus) => {
const status = migrationStatus.status;
if (!groupedStats[status]) {
groupedStats[status] = [];
}
groupedStats[status].push(migrationStatus.id);
return groupedStats;
},
{}
);

return stats;
}
}
}
};

function migrateItem(item, toolBaseUrl, db) {
return new Promise(async (resolve, reject) => {
const body = {
item: item
}
};

try {
const response = await fetch(`${toolBaseUrl}/migration`, {
method: 'POST',
method: "POST",
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json'
"Content-Type": "application/json"
}
})
});

if (response.status === 200) {
const json = await response.json();
await saveItem(json.item, db);
return resolve({
id: item._id,
status: statusUpdated
});
});
}

if (response.status === 304) {
Expand All @@ -95,7 +105,6 @@ function migrateItem(item, toolBaseUrl, db) {
}

throw new Error(`item ${item._id} could not be migrated`);

} catch (e) {
resolve({
id: item._id,
Expand Down

0 comments on commit 0b61951

Please sign in to comment.