Skip to content

Commit

Permalink
fix: use custom logic for handling sequence ids
Browse files Browse the repository at this point in the history
  • Loading branch information
sabhas committed May 1, 2023
1 parent d2f011e commit dba53de
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 93 deletions.
82 changes: 4 additions & 78 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"jsonwebtoken": "^8.5.1",
"ldapjs": "2.3.3",
"mongoose": "^6.0.12",
"mongoose-sequence": "^5.3.1",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"rate-limiter-flexible": "2.4.1",
Expand All @@ -80,7 +79,6 @@
"@types/jest": "^26.0.24",
"@types/jsonwebtoken": "^8.5.5",
"@types/ldapjs": "^2.2.4",
"@types/mongoose-sequence": "^3.0.6",
"@types/morgan": "^1.9.3",
"@types/multer": "^1.4.7",
"@types/node": "^15.12.2",
Expand Down
2 changes: 1 addition & 1 deletion api/public/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ paths:
examples:
'Example 1':
value: [{clientId: someClientID1234, clientSecret: someRandomCryptoString, accessTokenExpiration: 86400}, {clientId: someOtherClientID, clientSecret: someOtherRandomCryptoString, accessTokenExpiration: 86400}]
summary: 'Admin only task. Returns the list of all the clients *'
summary: 'Admin only task. Returns the list of all the clients'
tags:
- Client
security:
Expand Down
15 changes: 15 additions & 0 deletions api/src/model/Counter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import mongoose, { Schema } from 'mongoose'

const CounterSchema = new Schema({
id: {
type: String,
required: true,
unique: true
},
seq: {
type: Number,
required: true
}
})

export default mongoose.model('Counter', CounterSchema)
17 changes: 12 additions & 5 deletions api/src/model/Group.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import mongoose, { Schema, model, Document, Model } from 'mongoose'
import { Schema, model, Document, Model } from 'mongoose'
import { GroupDetailsResponse } from '../controllers'
import User, { IUser } from './User'
import { AuthProviderType } from '../utils'
const AutoIncrement = require('mongoose-sequence')(mongoose)
import { AuthProviderType, getSequenceNextValue } from '../utils'

export const PUBLIC_GROUP_NAME = 'Public'

Expand Down Expand Up @@ -44,6 +43,10 @@ const groupSchema = new Schema<IGroupDocument>({
required: true,
unique: true
},
groupId: {
type: Number,
unique: true
},
description: {
type: String,
default: 'Group description.'
Expand All @@ -59,9 +62,13 @@ const groupSchema = new Schema<IGroupDocument>({
users: [{ type: Schema.Types.ObjectId, ref: 'User' }]
})

groupSchema.plugin(AutoIncrement, { inc_field: 'groupId' })

// Hooks
groupSchema.pre('save', async function () {
if (this.isNew) {
this.groupId = await getSequenceNextValue('groupId')
}
})

groupSchema.post('save', function (group: IGroup, next: Function) {
group.populate('users', 'id username displayName -_id').then(function () {
next()
Expand Down
15 changes: 12 additions & 3 deletions api/src/model/Permission.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import mongoose, { Schema, model, Document, Model } from 'mongoose'
const AutoIncrement = require('mongoose-sequence')(mongoose)
import { Schema, model, Document, Model } from 'mongoose'
import { PermissionDetailsResponse } from '../controllers'
import { getSequenceNextValue } from '../utils'

interface GetPermissionBy {
user?: Schema.Types.ObjectId
Expand All @@ -23,6 +23,10 @@ interface IPermissionModel extends Model<IPermission> {
}

const permissionSchema = new Schema<IPermissionDocument>({
permissionId: {
type: Number,
unique: true
},
path: {
type: String,
required: true
Expand All @@ -39,7 +43,12 @@ const permissionSchema = new Schema<IPermissionDocument>({
group: { type: Schema.Types.ObjectId, ref: 'Group' }
})

permissionSchema.plugin(AutoIncrement, { inc_field: 'permissionId' })
// Hooks
permissionSchema.pre('save', async function () {
if (this.isNew) {
this.permissionId = await getSequenceNextValue('permissionId')
}
})

// Static Methods
permissionSchema.static('get', async function (getBy: GetPermissionBy): Promise<
Expand Down
20 changes: 16 additions & 4 deletions api/src/model/User.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import mongoose, { Schema, model, Document, Model } from 'mongoose'
const AutoIncrement = require('mongoose-sequence')(mongoose)
import { Schema, model, Document, Model } from 'mongoose'
import bcrypt from 'bcryptjs'
import { AuthProviderType } from '../utils'
import { AuthProviderType, getSequenceNextValue } from '../utils'

export interface UserPayload {
/**
Expand Down Expand Up @@ -66,6 +65,10 @@ const userSchema = new Schema<IUserDocument>({
required: true,
unique: true
},
id: {
type: Number,
unique: true
},
password: {
type: String,
required: true
Expand Down Expand Up @@ -107,7 +110,16 @@ const userSchema = new Schema<IUserDocument>({
}
]
})
userSchema.plugin(AutoIncrement, { inc_field: 'id' })
// userSchema.plugin(AutoIncrement, { inc_field: 'id' })

// Hooks
userSchema.pre('save', async function (next) {
if (this.isNew) {
this.id = await getSequenceNextValue('id')
}

next()
})

// Static Methods
userSchema.static('hashPassword', (password: string): string => {
Expand Down
15 changes: 15 additions & 0 deletions api/src/utils/getSequenceNextValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Counter from '../model/Counter'

export const getSequenceNextValue = async (seqName: string) => {
const seqDoc = await Counter.findOne({ id: seqName })
if (!seqDoc) {
await Counter.create({ id: seqName, seq: 1 })
return 1
}

seqDoc.seq += 1

await seqDoc.save()

return seqDoc.seq
}
1 change: 1 addition & 0 deletions api/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export * from './getCertificates'
export * from './getDesktopFields'
export * from './getPreProgramVariables'
export * from './getRunTimeAndFilePath'
export * from './getSequenceNextValue'
export * from './getServerUrl'
export * from './getTokensFromDB'
export * from './instantiateLogger'
Expand Down

0 comments on commit dba53de

Please sign in to comment.