Added auto incrementing ticket number
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
import mongoose from 'mongoose'
|
import mongoose from "mongoose"
|
||||||
import mongodb from 'mongodb'
|
import mongodb from "mongodb"
|
||||||
import Grid from 'gridfs-stream'
|
import Grid from "gridfs-stream"
|
||||||
import merge from 'mongoose-merge-plugin'
|
import merge from "mongoose-merge-plugin"
|
||||||
import autobind from 'autobind-decorator'
|
import autobind from "autobind-decorator"
|
||||||
import * as Schemas from './schemas'
|
import * as Schemas from "./schemas"
|
||||||
import util from 'util'
|
import util from "util"
|
||||||
|
|
||||||
Grid.mongo = mongoose.mongo
|
Grid.mongo = mongoose.mongo
|
||||||
|
|
||||||
@@ -16,20 +16,26 @@ export class DB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
connect(mongoUri, isProduction) {
|
connect(mongoUri, isProduction) {
|
||||||
return mongoose.connect(mongoUri, { useMongoClient: true, config: { autoIndex: !isProduction } }).then((connection) => {
|
return mongoose
|
||||||
this.connection = connection
|
.connect(mongoUri, {
|
||||||
|
useMongoClient: true,
|
||||||
|
config: { autoIndex: !isProduction },
|
||||||
|
})
|
||||||
|
.then((connection) => {
|
||||||
|
this.connection = connection
|
||||||
|
|
||||||
this.gridfs = Grid(connection.db)
|
this.gridfs = Grid(connection.db)
|
||||||
this.gridfs.findOneAsync = util.promisify(this.gridfs.findOne)
|
this.gridfs.findOneAsync = util.promisify(this.gridfs.findOne)
|
||||||
this.gridfs.removeAsync = util.promisify(this.gridfs.remove)
|
this.gridfs.removeAsync = util.promisify(this.gridfs.remove)
|
||||||
|
|
||||||
this.User = connection.model('User', Schemas.userSchema)
|
this.User = connection.model("User", Schemas.userSchema)
|
||||||
this.WorkItem = connection.model('WorkItem', Schemas.workItemSchema)
|
this.WorkItem = connection.model("WorkItem", Schemas.workItemSchema)
|
||||||
this.Activity = connection.model('Activity', Schemas.activitySchema)
|
this.Activity = connection.model("Activity", Schemas.activitySchema)
|
||||||
this.Team = connection.model('Team', Schemas.teamSchema)
|
this.Team = connection.model("Team", Schemas.teamSchema)
|
||||||
|
this.Counter = connection.model("Counter", Schemas.counterSchema)
|
||||||
|
|
||||||
return Promise.resolve(this)
|
return Promise.resolve(this)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
newObjectId(s) {
|
newObjectId(s) {
|
||||||
@@ -38,14 +44,16 @@ export class DB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lookupToken(token, done) {
|
lookupToken(token, done) {
|
||||||
this.User.findOne({ 'loginToken': token }).then((user) => {
|
this.User.findOne({ loginToken: token })
|
||||||
if (!user) {
|
.then((user) => {
|
||||||
done(null, false)
|
if (!user) {
|
||||||
} else {
|
done(null, false)
|
||||||
done(null, user)
|
} else {
|
||||||
}
|
done(null, user)
|
||||||
}).catch((err) => {
|
}
|
||||||
done(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 { workItemSchema } from "./workItem"
|
||||||
export { userSchema } from './user'
|
export { userSchema } from "./user"
|
||||||
export { teamSchema } from './team'
|
export { teamSchema } from "./team"
|
||||||
export { activitySchema } from './activity'
|
export { activitySchema } from "./activity"
|
||||||
|
export { counterSchema } from "./counter"
|
||||||
|
|||||||
@@ -1,21 +1,44 @@
|
|||||||
import { Schema } from 'mongoose'
|
import { Schema } from "mongoose"
|
||||||
import { regExpPattern } from 'regexp-pattern'
|
|
||||||
|
|
||||||
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: {
|
_id: { type: Schema.Types.ObjectId, required: true, auto: true },
|
||||||
values: [ 'order', 'inspection', 'complaint'],
|
workItemType: {
|
||||||
message: 'enum validator failed for path `{PATH}` with value `{VALUE}`'
|
type: String,
|
||||||
}},
|
required: true,
|
||||||
number: Number,
|
enum: {
|
||||||
location: {
|
values: ["order", "inspection", "complaint"],
|
||||||
type: { type: String },
|
message: "enum validator failed for path `{PATH}` with value `{VALUE}`",
|
||||||
coordinates: [Number],
|
},
|
||||||
|
},
|
||||||
|
number: Number,
|
||||||
|
location: {
|
||||||
|
type: { type: String },
|
||||||
|
coordinates: [Number],
|
||||||
|
},
|
||||||
|
address: String,
|
||||||
|
photos: [Schema.Types.ObjectId],
|
||||||
|
details: String,
|
||||||
|
ticketNumber: { type: Number, default: 0 },
|
||||||
},
|
},
|
||||||
address: String,
|
{ timestamps: true, id: false }
|
||||||
photos: [ Schema.Types.ObjectId ],
|
)
|
||||||
details: String,
|
|
||||||
}, { 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() {
|
workItemSchema.methods.toClient = function() {
|
||||||
return this.toObject()
|
return this.toObject()
|
||||||
|
|||||||
Reference in New Issue
Block a user