Skip to content

Commit

Permalink
refactor: remove unused mongoTimestamp plugin (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
karrui authored Aug 11, 2020
1 parent 74234aa commit 61a3229
Showing 1 changed file with 1 addition and 40 deletions.
41 changes: 1 addition & 40 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PackageMode } from '@opengovsg/formsg-sdk/dist/types'
import aws from 'aws-sdk'
import crypto from 'crypto'
import { SessionOptions } from 'express-session'
import { ConnectionOptions, Schema } from 'mongoose'
import { ConnectionOptions } from 'mongoose'
import nodemailer from 'nodemailer'
import directTransport from 'nodemailer-direct-transport'
import Mail from 'nodemailer/lib/mailer'
Expand Down Expand Up @@ -81,7 +81,6 @@ type Config = {
// Functions
configureAws: () => Promise<void>
otpGenerator: () => string
mongoTimestamp: (schema: Schema, options: any) => void
}

// Enums
Expand Down Expand Up @@ -409,43 +408,6 @@ const otpGenerator = () => {
return value.join('')
}

// TODO(#2505): Remove this once no models rely on this and use mongoose's own
// timestamps.
/**
* Generates created and last modified fields for several mongo models.
* Taken from mongoose-utilities's timestamp function.
* Should really be inside a utils.js file.
* @param schema Mongoose schema
* @param options Dictionary of created and modified
*/
const mongoTimestamp = (schema: Schema, options: any) => {
options || (options = {})

// Options
let fields = {}
let createdPath = options.createdPath || 'created'
let modifiedPath = options.modifiedPath || 'modified'

// Add paths to schema if not present
if (!schema.paths[createdPath]) {
fields[modifiedPath] = { type: Date }
}
if (!schema.paths[createdPath]) {
fields[createdPath] = {
type: Date,
default: Date.now,
}
}

schema.add(fields)

// Update the modified timestamp on save
schema.pre('save', function (next) {
this[modifiedPath] = new Date()
return next()
})
}

const config: Config = {
app: appConfig,
db: dbConfig,
Expand All @@ -468,7 +430,6 @@ const config: Config = {
adminBannerContent,
configureAws,
otpGenerator,
mongoTimestamp,
}

export = config

0 comments on commit 61a3229

Please sign in to comment.