From 1f869ef4ccd6c7032b6909f508438e6893beba0d Mon Sep 17 00:00:00 2001 From: John Lyon-Smith Date: Fri, 6 Apr 2018 16:50:40 -0700 Subject: [PATCH] Added auto incrementing ticket number --- server/src/database/DB.js | 62 ++++++++++++++----------- server/src/database/schemas/counter.js | 6 +++ server/src/database/schemas/index.js | 9 ++-- server/src/database/schemas/workItem.js | 55 +++++++++++++++------- 4 files changed, 85 insertions(+), 47 deletions(-) create mode 100644 server/src/database/schemas/counter.js diff --git a/server/src/database/DB.js b/server/src/database/DB.js index acdc209..7c637cf 100644 --- a/server/src/database/DB.js +++ b/server/src/database/DB.js @@ -1,10 +1,10 @@ -import mongoose from 'mongoose' -import mongodb from 'mongodb' -import Grid from 'gridfs-stream' -import merge from 'mongoose-merge-plugin' -import autobind from 'autobind-decorator' -import * as Schemas from './schemas' -import util from 'util' +import mongoose from "mongoose" +import mongodb from "mongodb" +import Grid from "gridfs-stream" +import merge from "mongoose-merge-plugin" +import autobind from "autobind-decorator" +import * as Schemas from "./schemas" +import util from "util" Grid.mongo = mongoose.mongo @@ -16,20 +16,26 @@ export class DB { } connect(mongoUri, isProduction) { - return mongoose.connect(mongoUri, { useMongoClient: true, config: { autoIndex: !isProduction } }).then((connection) => { - this.connection = connection + return mongoose + .connect(mongoUri, { + useMongoClient: true, + config: { autoIndex: !isProduction }, + }) + .then((connection) => { + this.connection = connection - this.gridfs = Grid(connection.db) - this.gridfs.findOneAsync = util.promisify(this.gridfs.findOne) - this.gridfs.removeAsync = util.promisify(this.gridfs.remove) + this.gridfs = Grid(connection.db) + this.gridfs.findOneAsync = util.promisify(this.gridfs.findOne) + this.gridfs.removeAsync = util.promisify(this.gridfs.remove) - this.User = connection.model('User', Schemas.userSchema) - this.WorkItem = connection.model('WorkItem', Schemas.workItemSchema) - this.Activity = connection.model('Activity', Schemas.activitySchema) - this.Team = connection.model('Team', Schemas.teamSchema) + this.User = connection.model("User", Schemas.userSchema) + this.WorkItem = connection.model("WorkItem", Schemas.workItemSchema) + this.Activity = connection.model("Activity", Schemas.activitySchema) + this.Team = connection.model("Team", Schemas.teamSchema) + this.Counter = connection.model("Counter", Schemas.counterSchema) - return Promise.resolve(this) - }) + return Promise.resolve(this) + }) } newObjectId(s) { @@ -38,14 +44,16 @@ export class DB { } lookupToken(token, done) { - this.User.findOne({ 'loginToken': token }).then((user) => { - if (!user) { - done(null, false) - } else { - done(null, user) - } - }).catch((err) => { - done(err) - }) + this.User.findOne({ loginToken: token }) + .then((user) => { + if (!user) { + done(null, false) + } else { + done(null, user) + } + }) + .catch((err) => { + done(err) + }) } } diff --git a/server/src/database/schemas/counter.js b/server/src/database/schemas/counter.js new file mode 100644 index 0000000..75a4f0f --- /dev/null +++ b/server/src/database/schemas/counter.js @@ -0,0 +1,6 @@ +import { Schema } from "mongoose" + +export const counterSchema = new Schema({ + _id: { type: String, required: true }, + seq: { type: Number, default: 0 }, +}) diff --git a/server/src/database/schemas/index.js b/server/src/database/schemas/index.js index 3fff8d6..551fdc8 100644 --- a/server/src/database/schemas/index.js +++ b/server/src/database/schemas/index.js @@ -1,4 +1,5 @@ -export { workItemSchema } from './workItem' -export { userSchema } from './user' -export { teamSchema } from './team' -export { activitySchema } from './activity' +export { workItemSchema } from "./workItem" +export { userSchema } from "./user" +export { teamSchema } from "./team" +export { activitySchema } from "./activity" +export { counterSchema } from "./counter" diff --git a/server/src/database/schemas/workItem.js b/server/src/database/schemas/workItem.js index 84f0fe8..d82232c 100644 --- a/server/src/database/schemas/workItem.js +++ b/server/src/database/schemas/workItem.js @@ -1,21 +1,44 @@ -import { Schema } from 'mongoose' -import { regExpPattern } from 'regexp-pattern' +import { Schema } from "mongoose" -export let workItemSchema = new Schema({ - _id: { type: Schema.Types.ObjectId, required: true, auto: true }, - workItemType: { type: String, required: true, enum: { - values: [ 'order', 'inspection', 'complaint'], - message: 'enum validator failed for path `{PATH}` with value `{VALUE}`' - }}, - number: Number, - location: { - type: { type: String }, - coordinates: [Number], +export const workItemSchema = new Schema( + { + _id: { type: Schema.Types.ObjectId, required: true, auto: true }, + workItemType: { + type: String, + required: true, + enum: { + values: ["order", "inspection", "complaint"], + message: "enum validator failed for path `{PATH}` with value `{VALUE}`", + }, + }, + number: Number, + location: { + type: { type: String }, + coordinates: [Number], + }, + address: String, + photos: [Schema.Types.ObjectId], + details: String, + ticketNumber: { type: Number, default: 0 }, }, - address: String, - photos: [ Schema.Types.ObjectId ], - details: String, -}, { timestamps: true, id: false }) + { timestamps: true, id: false } +) + +workItemSchema.pre("save", async function(next) { + try { + const counter = await this.model("Counter").findByIdAndUpdate( + { _id: "ticketNumber" }, + { $inc: { seq: 1 } }, + { new: true, upsert: true } + ) + + this.ticketNumber = counter.seq + } catch (error) { + console.error(error) + } + + next() +}) workItemSchema.methods.toClient = function() { return this.toObject()