Prevent duplicate board labels

43de3b8 did prevent empty labels with the same color, but we also want
to prevent label with the same non-empty name and same color because
the rationale is identical.
This commit is contained in:
Maxime Quandalle 2015-10-16 17:49:25 +02:00
parent 15ebfa63c6
commit 6dedf673d5
2 changed files with 31 additions and 42 deletions

View file

@ -135,29 +135,26 @@ Boards.mutations({
},
addLabel(name, color) {
// If label with the same name and color already exists we don't want to
// create another one because they would be indistinguishable in the UI
// (they would still have different `_id` but that is not exposed to the
// user).
if (!this.getLabel(name, color)) {
const _id = Random.id(6);
// If an empty label of a given color already exists we don't want to create
// an other one because they would be indistinguishable in the UI (they
// would still have different `_id` but that is not exposed to the user).
if (name === '' && this.getLabel(name, color)) {
return {};
}
return { $push: {labels: { _id, name, color }}};
}
},
editLabel(labelId, name, color) {
if (!this.getLabel(name, color)) {
const labelIndex = this.labelIndex(labelId);
if (name === '' && this.getLabel(name, color)) {
return {};
}
return {
$set: {
[`labels.${labelIndex}.name`]: name,
[`labels.${labelIndex}.color`]: color,
},
};
}
},
removeLabel(labelId) {

View file

@ -28,7 +28,8 @@ Meteor.methods({
throw new Meteor.Error('error-json-schema');
}
// 2. check parameters are ok from a business point of view (exist & authorized)
// 2. check parameters are ok from a business point of view (exist &
// authorized)
const list = Lists.findOne(listId);
if (!list) {
throw new Meteor.Error('error-list-doesNotExist');
@ -48,6 +49,7 @@ Meteor.methods({
createdAt: dateOfImport,
dateLastActivity: dateOfImport,
description: trelloCard.desc,
labelIds: [],
listId: list._id,
sort: sortIndex,
title: trelloCard.name,
@ -65,23 +67,12 @@ Meteor.methods({
// 5. map labels - create missing ones
trelloCard.labels.forEach((currentLabel) => {
const color = currentLabel.color;
const name = currentLabel.name;
const existingLabel = list.board().getLabel(name, color);
let labelId = undefined;
if (existingLabel) {
labelId = existingLabel._id;
} else {
let labelCreated = list.board().addLabel(name, color);
// XXX currently mutations return no value so we have to fetch the label we just created
// waiting on https://github.com/mquandalle/meteor-collection-mutations/issues/1 to remove...
labelCreated = list.board().getLabel(name, color);
labelId = labelCreated._id;
}
const { name, color } = currentLabel;
// `addLabel` won't create dublicate labels (ie labels with the same name
// and color) so here it is used more in a "enforceLabelExistence" way.
list.board().addLabel(name, color);
const { _id: labelId } = list.board().getLabel(name, color);
if (labelId) {
if (!cardToCreate.labelIds) {
cardToCreate.labelIds = [];
}
cardToCreate.labelIds.push(labelId);
}
});
@ -99,7 +90,8 @@ Meteor.methods({
system: 'Trello',
url: trelloCard.url,
},
// we attribute the import to current user, not the one from the original card
// we attribute the import to current user, not the one from the original
// card
userId: Meteor.userId(),
});