Created
April 1, 2016 02:18
-
-
Save lrezende/6b9b00e213fc2cd2efb44bf8f13abf39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clientSchema.pre('validate', function (next) { | |
// set code automatically | |
// generate all possible codes | |
// this is a fixed list from A/001 to Z/100 | |
let _all_codes = new Set() | |
for (let _letter of 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') { | |
for (let i = 1; i <= 100; i++) { | |
_all_codes.add(_letter + '/' + sprintf('%03d', i)); | |
} | |
} | |
// get all codes from db | |
var _db_codes = new Set(); | |
mongoose.models['Client'] | |
.find() | |
.select('-_id code') | |
.sort('code') | |
.lean() | |
.exec(function(err, codes) { | |
if (err) { | |
console.log(err) | |
} | |
// | |
console.log(_all_codes) | |
}) | |
// get next code | |
// *1 return the first gap and set the code | |
// this.code = 'D/078' | |
next(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment