plankanban#27 back-end refactoring, fix board duplication
parent
8590d4f06c
commit
e4109c825f
@ -1,92 +0,0 @@
|
||||
const util = require('util');
|
||||
const { v4: uuid } = require('uuid');
|
||||
|
||||
const Errors = {
|
||||
PROJECT_NOT_FOUND: {
|
||||
projectNotFound: 'Project not found',
|
||||
},
|
||||
TRELLO_FILE_INVALID: {
|
||||
trelloFileInvalid: 'Trello File invalid',
|
||||
},
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
projectId: {
|
||||
type: 'string',
|
||||
regex: /^[0-9]+$/,
|
||||
required: true,
|
||||
},
|
||||
position: {
|
||||
type: 'number',
|
||||
required: true,
|
||||
},
|
||||
name: {
|
||||
type: 'string',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
projectNotFound: {
|
||||
responseType: 'notFound',
|
||||
},
|
||||
trelloFileInvalid: {
|
||||
responseType: 'badRequest',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const { currentUser } = this.req;
|
||||
|
||||
const project = await Project.findOne(inputs.projectId);
|
||||
if (!project) {
|
||||
throw Errors.PROJECT_NOT_FOUND;
|
||||
}
|
||||
|
||||
const isProjectManager = await sails.helpers.users.isProjectManager(currentUser.id, project.id);
|
||||
if (!isProjectManager) {
|
||||
throw Errors.PROJECT_NOT_FOUND;
|
||||
}
|
||||
|
||||
const upload = util.promisify((options, callback) =>
|
||||
this.req.file('file').upload(options, (error, files) => callback(error, files)),
|
||||
);
|
||||
|
||||
let files;
|
||||
let trelloBoard;
|
||||
try {
|
||||
files = await upload({
|
||||
saveAs: uuid(),
|
||||
maxBytes: null,
|
||||
});
|
||||
trelloBoard = await sails.helpers.boards.loadTrelloFile(files[0]);
|
||||
} catch (error) {
|
||||
throw Errors.TRELLO_FILE_INVALID;
|
||||
}
|
||||
|
||||
const values = {
|
||||
..._.pick(inputs, ['position', 'name']),
|
||||
type: 'kanban',
|
||||
};
|
||||
const { board, boardMembership } = await sails.helpers.boards.createOne(
|
||||
values,
|
||||
currentUser,
|
||||
project,
|
||||
this.req,
|
||||
);
|
||||
|
||||
await sails.helpers.boards.importTrello(currentUser, board, trelloBoard, this.req);
|
||||
|
||||
if (this.req.isSocket) {
|
||||
sails.sockets.join(this.req, `board:${board.id}`);
|
||||
}
|
||||
|
||||
return {
|
||||
item: board,
|
||||
included: {
|
||||
boardMemberships: [boardMembership],
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,152 @@
|
||||
module.exports = {
|
||||
inputs: {
|
||||
user: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
board: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
trelloBoard: {
|
||||
type: 'json',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const trelloToPlankaLabels = {};
|
||||
|
||||
const getTrelloLists = () => inputs.trelloBoard.lists.filter((list) => !list.closed);
|
||||
|
||||
const getUsedTrelloLabels = () => {
|
||||
const result = {};
|
||||
inputs.trelloBoard.cards
|
||||
.map((card) => card.labels)
|
||||
.flat()
|
||||
.forEach((label) => {
|
||||
result[label.id] = label;
|
||||
});
|
||||
|
||||
return Object.values(result);
|
||||
};
|
||||
|
||||
const getTrelloCardsOfList = (listId) =>
|
||||
inputs.trelloBoard.cards.filter((card) => card.idList === listId && !card.closed);
|
||||
|
||||
const getAllTrelloCheckItemsOfCard = (cardId) =>
|
||||
inputs.trelloBoard.checklists
|
||||
.filter((checklist) => checklist.idCard === cardId)
|
||||
.map((checklist) => checklist.checkItems)
|
||||
.flat();
|
||||
|
||||
const getTrelloCommentsOfCard = (cardId) =>
|
||||
inputs.trelloBoard.actions.filter(
|
||||
(action) =>
|
||||
action.type === 'commentCard' &&
|
||||
action.data &&
|
||||
action.data.card &&
|
||||
action.data.card.id === cardId,
|
||||
);
|
||||
|
||||
const getPlankaLabelColor = (trelloLabelColor) =>
|
||||
Label.COLORS.find((color) => color.indexOf(trelloLabelColor) !== -1) || 'desert-sand';
|
||||
|
||||
const importCardLabels = async (plankaCard, trelloCard) => {
|
||||
return Promise.all(
|
||||
trelloCard.labels.map(async (trelloLabel) => {
|
||||
return CardLabel.create({
|
||||
cardId: plankaCard.id,
|
||||
labelId: trelloToPlankaLabels[trelloLabel.id].id,
|
||||
});
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const importTasks = async (plankaCard, trelloCard) => {
|
||||
// TODO find workaround for tasks/checklist mismapping, see issue trello2planka#5
|
||||
return Promise.all(
|
||||
getAllTrelloCheckItemsOfCard(trelloCard.id).map(async (trelloCheckItem) => {
|
||||
return Task.create({
|
||||
cardId: plankaCard.id,
|
||||
position: trelloCheckItem.pos,
|
||||
name: trelloCheckItem.name,
|
||||
isCompleted: trelloCheckItem.state === 'complete',
|
||||
}).fetch();
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const importComments = async (plankaCard, trelloCard) => {
|
||||
const trelloComments = getTrelloCommentsOfCard(trelloCard.id);
|
||||
trelloComments.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime());
|
||||
|
||||
return Promise.all(
|
||||
trelloComments.map(async (trelloComment) => {
|
||||
return Action.create({
|
||||
cardId: plankaCard.id,
|
||||
userId: inputs.user.id,
|
||||
type: 'commentCard',
|
||||
data: {
|
||||
text:
|
||||
`${trelloComment.data.text}\n\n---\n*Note: imported comment, originally posted by ` +
|
||||
`\n${trelloComment.memberCreator.fullName} (${trelloComment.memberCreator.username}) on ${trelloComment.date}*`,
|
||||
},
|
||||
}).fetch();
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const importCards = async (plankaList, trelloList) => {
|
||||
return Promise.all(
|
||||
getTrelloCardsOfList(trelloList.id).map(async (trelloCard) => {
|
||||
const plankaCard = await Card.create({
|
||||
boardId: inputs.board.id,
|
||||
listId: plankaList.id,
|
||||
creatorUserId: inputs.user.id,
|
||||
position: trelloCard.pos,
|
||||
name: trelloCard.name,
|
||||
description: trelloCard.desc || null,
|
||||
}).fetch();
|
||||
|
||||
await importCardLabels(plankaCard, trelloCard);
|
||||
await importTasks(plankaCard, trelloCard);
|
||||
await importComments(plankaCard, trelloCard);
|
||||
|
||||
return plankaCard;
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const importLabels = async () => {
|
||||
return Promise.all(
|
||||
getUsedTrelloLabels().map(async (trelloLabel) => {
|
||||
const plankaLabel = await Label.create({
|
||||
boardId: inputs.board.id,
|
||||
name: trelloLabel.name || null,
|
||||
color: getPlankaLabelColor(trelloLabel.color),
|
||||
}).fetch();
|
||||
|
||||
trelloToPlankaLabels[trelloLabel.id] = plankaLabel;
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const importLists = async () => {
|
||||
return Promise.all(
|
||||
getTrelloLists().map(async (trelloList) => {
|
||||
const plankaList = await List.create({
|
||||
boardId: inputs.board.id,
|
||||
name: trelloList.name,
|
||||
position: trelloList.pos,
|
||||
}).fetch();
|
||||
|
||||
return importCards(plankaList, trelloList);
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
await importLabels();
|
||||
await importLists();
|
||||
},
|
||||
};
|
||||
@ -1,171 +0,0 @@
|
||||
async function importFromTrello(inputs) {
|
||||
const trelloToPlankaLabels = {};
|
||||
|
||||
const getTrelloLists = () => inputs.trelloBoard.lists.filter((list) => !list.closed);
|
||||
const getUsedTrelloLabels = () => {
|
||||
const result = {};
|
||||
inputs.trelloBoard.cards
|
||||
.map((card) => card.labels)
|
||||
.flat()
|
||||
.forEach((label) => {
|
||||
result[label.id] = label;
|
||||
});
|
||||
return Object.values(result);
|
||||
};
|
||||
const getTrelloCardsOfList = (listId) =>
|
||||
inputs.trelloBoard.cards.filter((l) => l.idList === listId && !l.closed);
|
||||
const getAllTrelloCheckItemsOfCard = (cardId) =>
|
||||
inputs.trelloBoard.checklists
|
||||
.filter((c) => c.idCard === cardId)
|
||||
.map((checklist) => checklist.checkItems)
|
||||
.flat();
|
||||
const getTrelloCommentsOfCard = (cardId) =>
|
||||
inputs.trelloBoard.actions.filter(
|
||||
(action) =>
|
||||
action.type === 'commentCard' &&
|
||||
action.data &&
|
||||
action.data.card &&
|
||||
action.data.card.id === cardId,
|
||||
);
|
||||
const getPlankaLabelColor = (trelloLabelColor) =>
|
||||
Label.COLORS.find((c) => c.indexOf(trelloLabelColor) !== -1) || 'desert-sand';
|
||||
|
||||
const importComments = async (trelloCard, plankaCard) => {
|
||||
const trelloComments = getTrelloCommentsOfCard(trelloCard.id);
|
||||
trelloComments.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime());
|
||||
return Promise.all(
|
||||
trelloComments.map(async (trelloComment) => {
|
||||
return sails.helpers.actions.createOne(
|
||||
{
|
||||
type: 'commentCard',
|
||||
data: {
|
||||
text:
|
||||
`${trelloComment.data.text}\n\n---\n*Note: imported comment, originally posted by ` +
|
||||
`\n${trelloComment.memberCreator.fullName} (${trelloComment.memberCreator.username}) on ${trelloComment.date}*`,
|
||||
},
|
||||
},
|
||||
inputs.user,
|
||||
plankaCard,
|
||||
inputs.request,
|
||||
);
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const importTasks = async (trelloCard, plankaCard) => {
|
||||
// TODO find workaround for tasks/checklist mismapping, see issue trello2planka#5
|
||||
return Promise.all(
|
||||
getAllTrelloCheckItemsOfCard(trelloCard.id).map(async (trelloCheckItem) => {
|
||||
return sails.helpers.tasks.createOne(
|
||||
{
|
||||
cardId: plankaCard.id,
|
||||
position: trelloCheckItem.pos,
|
||||
name: trelloCheckItem.name,
|
||||
isCompleted: trelloCheckItem.state === 'complete',
|
||||
},
|
||||
plankaCard,
|
||||
inputs.request,
|
||||
);
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const importCardLabels = async (trelloCard, plankaCard) => {
|
||||
return Promise.all(
|
||||
trelloCard.labels.map(async (trelloLabel) => {
|
||||
return sails.helpers.cardLabels.createOne(
|
||||
trelloToPlankaLabels[trelloLabel.id],
|
||||
plankaCard,
|
||||
inputs.request,
|
||||
);
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const importCards = async (trelloList, plankaList) => {
|
||||
return Promise.all(
|
||||
getTrelloCardsOfList(trelloList.id).map(async (trelloCard) => {
|
||||
const plankaCard = await sails.helpers.cards.createOne(
|
||||
{
|
||||
listId: plankaList.id,
|
||||
position: trelloCard.pos,
|
||||
name: trelloCard.name,
|
||||
description: trelloCard.desc || null,
|
||||
},
|
||||
inputs.user,
|
||||
inputs.board,
|
||||
plankaList,
|
||||
inputs.request,
|
||||
);
|
||||
|
||||
await importCardLabels(trelloCard, plankaCard);
|
||||
await importTasks(trelloCard, plankaCard);
|
||||
await importComments(trelloCard, plankaCard);
|
||||
return plankaCard;
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const importLists = async () => {
|
||||
return Promise.all(
|
||||
getTrelloLists().map(async (trelloList) => {
|
||||
const plankaList = await sails.helpers.lists.createOne(
|
||||
{
|
||||
name: trelloList.name,
|
||||
position: trelloList.pos,
|
||||
},
|
||||
inputs.board,
|
||||
inputs.request,
|
||||
);
|
||||
return importCards(trelloList, plankaList);
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
const importLabels = async () => {
|
||||
return Promise.all(
|
||||
getUsedTrelloLabels().map(async (trelloLabel) => {
|
||||
const plankaLabel = await sails.helpers.labels.createOne(
|
||||
{
|
||||
name: trelloLabel.name || null,
|
||||
color: getPlankaLabelColor(trelloLabel.color),
|
||||
},
|
||||
inputs.board,
|
||||
inputs.request,
|
||||
);
|
||||
trelloToPlankaLabels[trelloLabel.id] = plankaLabel;
|
||||
}),
|
||||
);
|
||||
};
|
||||
|
||||
await importLabels();
|
||||
await importLists();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
user: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
board: {
|
||||
type: 'ref',
|
||||
required: true,
|
||||
},
|
||||
trelloBoard: {
|
||||
type: 'json',
|
||||
required: true,
|
||||
},
|
||||
request: {
|
||||
type: 'ref',
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
await importFromTrello(inputs);
|
||||
|
||||
return {
|
||||
board: inputs.board,
|
||||
};
|
||||
},
|
||||
};
|
||||
@ -1,36 +0,0 @@
|
||||
const fs = require('fs');
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
file: {
|
||||
type: 'json',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const isValidTrelloFile = (content) =>
|
||||
content &&
|
||||
Array.isArray(content.lists) &&
|
||||
Array.isArray(content.cards) &&
|
||||
Array.isArray(content.checklists) &&
|
||||
Array.isArray(content.actions);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(inputs.file.fd, (err, data) => {
|
||||
try {
|
||||
const exp = data && JSON.parse(data);
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else if (isValidTrelloFile(exp)) {
|
||||
resolve(exp);
|
||||
} else {
|
||||
reject(new Error('Invalid Trello File'));
|
||||
}
|
||||
} catch (e) {
|
||||
reject(new Error(e));
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
@ -0,0 +1,38 @@
|
||||
const fs = require('fs').promises;
|
||||
const rimraf = require('rimraf');
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
file: {
|
||||
type: 'json',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
invalidFile: {},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const content = await fs.readFile(inputs.file.fd);
|
||||
const trelloBoard = JSON.parse(content);
|
||||
|
||||
if (
|
||||
!trelloBoard ||
|
||||
!_.isArray(trelloBoard.lists) ||
|
||||
!_.isArray(trelloBoard.cards) ||
|
||||
!_.isArray(trelloBoard.checklists) ||
|
||||
!_.isArray(trelloBoard.actions)
|
||||
) {
|
||||
throw 'invalidFile';
|
||||
}
|
||||
|
||||
try {
|
||||
rimraf.sync(inputs.file.fd);
|
||||
} catch (error) {
|
||||
console.warn(error.stack); // eslint-disable-line no-console
|
||||
}
|
||||
|
||||
return trelloBoard;
|
||||
},
|
||||
};
|
||||
Loading…
Reference in New Issue