Added auto incrementing ticket number
This commit is contained in:
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
6
server/src/database/schemas/counter.js
Normal file
6
server/src/database/schemas/counter.js
Normal file
@@ -0,0 +1,6 @@
|
||||
import { Schema } from "mongoose"
|
||||
|
||||
export const counterSchema = new Schema({
|
||||
_id: { type: String, required: true },
|
||||
seq: { type: Number, default: 0 },
|
||||
})
|
||||
@@ -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"
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user