Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

472 duplicated law #504

Merged
merged 3 commits into from
Dec 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/admin-laws/template.jade
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
- each law in laws
li(data-id=law.id)
a(href="/admin/laws/" + law.id)
span.law-id=law.lawId
| #{' - '}
span.law-title=law.mediaTitle
span.law-id=law.tag.name
- if (law.lawId)
| #{' - '}
span.law-id=law.lawId
- if(law.mediaTitle)
| #{' - '}
span.law-title=law.mediaTitle
26 changes: 13 additions & 13 deletions lib/db-api/law.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,10 @@ function createLaw(data, fn) {
law.save(onsave);

function onsave(err) {
if (!err) return log('Saved law %s', law.id), fn(null, law);
if (err) return log('Found error %s', err), fn(err);

if (11000 == err.code) {
log('Attempt of duplication');
exports.searchOne(law.lawId, fn);
} else {
log('Found error %s', err);
fn(err);
}
log('Saved law %s', law.id);
fn(null, law);
}
};

Expand Down Expand Up @@ -195,18 +190,18 @@ exports.update = function update(id, data, fn) {
};

/**
* Search single law from lawId
* Search single law from _id
*
* @param {String} lawId string to search by `lawId`
* @param {ObjectId} law Id to search by `_id`
* @param {Function} fn callback function
* - 'err' error found while process or `null`
* - 'law' single law object found or `undefined`
* @return {Module} `law` module
* @api public
*/

exports.searchOne = function searchByLawId(lawId, fn) {
var query = { lawId: lawId, deletedAt: null };
exports.searchOne = function searchByLawId(id, fn) {
var query = { _id: id, deletedAt: null };

log('Searching for single law matching %j', query);
Law
Expand All @@ -219,9 +214,14 @@ exports.searchOne = function searchByLawId(lawId, fn) {
return fn(err);
}

if (!law) {
log('Law with id %s not found.', id);
return fn(new Error('Law not found'));
}

log('Delivering law %s', law.id);
fn(null, law);
})
});

return this;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/models/law.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ LawSchema.index({ createdAt: -1 });
LawSchema.index({ closingAt: -1 });
LawSchema.index({ participants: -1 });
LawSchema.index({ tag: -1 });
LawSchema.index({ state: 1, lawId: 1 }, { unique: true, dropDups: true });
LawSchema.index({ lawId: -1 });

/**
* Make Schema `.toObject()` and
Expand Down