Added auto incrementing ticket number

This commit is contained in:
John Lyon-Smith
2018-04-06 16:50:40 -07:00
parent 1c222f06e9
commit 1f869ef4cc
4 changed files with 85 additions and 47 deletions

View File

@@ -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,17 +16,23 @@ export class DB {
}
connect(mongoUri, isProduction) {
return mongoose.connect(mongoUri, { useMongoClient: true, config: { autoIndex: !isProduction } }).then((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.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)
})
@@ -38,13 +44,15 @@ export class DB {
}
lookupToken(token, done) {
this.User.findOne({ 'loginToken': token }).then((user) => {
this.User.findOne({ loginToken: token })
.then((user) => {
if (!user) {
done(null, false)
} else {
done(null, user)
}
}).catch((err) => {
})
.catch((err) => {
done(err)
})
}

View File

@@ -0,0 +1,6 @@
import { Schema } from "mongoose"
export const counterSchema = new Schema({
_id: { type: String, required: true },
seq: { type: Number, default: 0 },
})

View File

@@ -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"

View File

@@ -1,12 +1,16 @@
import { Schema } from 'mongoose'
import { regExpPattern } from 'regexp-pattern'
import { Schema } from "mongoose"
export let workItemSchema = new Schema({
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}`'
}},
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 },
@@ -15,7 +19,26 @@ export let workItemSchema = new Schema({
address: String,
photos: [Schema.Types.ObjectId],
details: String,
}, { timestamps: true, id: false })
ticketNumber: { type: Number, default: 0 },
},
{ 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()