diff --git a/src/models/activeModel.ts b/src/models/activeModel.ts index be95939..ca4fb39 100644 --- a/src/models/activeModel.ts +++ b/src/models/activeModel.ts @@ -1,4 +1,3 @@ -import cast from '../utils/cast' import Model from './model' import StaticModel from './staticModel' import StaticModelValidator from '../validation/validators/staticModelValidator' @@ -8,6 +7,7 @@ import { addActiveRow, getActiveRow, getActiveTable, setActiveRow } from '../dat import { ErrorMessages, GenericValidatorOptions } from '../types/validation/validator' import { Request } from 'express' import { TableRow } from '../types/data/tables' +import { utils } from '..' import { ValidationSchema, ValidationScheme } from '../types/validation/validationSchema' abstract class ActiveModel extends Model implements ActiveModelInterface { @@ -132,14 +132,14 @@ abstract class ActiveModel extends Model implements ActiveModelInterface { if (attributeConstructor.prototype instanceof ActiveModel) { (this.data[attribute] as ActiveModel).assignAttributes(data[attribute]) } else if (attributeConstructor.prototype instanceof StaticModel) { - const id = cast(data[attribute], Number) + const id = utils.cast(data[attribute], Number) if (this.data[attribute] === undefined || (this.data[attribute] as StaticModel).data.id !== id) { // TODO: Chnage to somthing that is not any this.data[attribute] = (attributeConstructor as any).find(id) } } else { - this.data[attribute] = cast(data[attribute], attributeConstructor as NumberConstructor|StringConstructor|BooleanConstructor) + this.data[attribute] = utils.cast(data[attribute], attributeConstructor as NumberConstructor|StringConstructor|BooleanConstructor) } } } @@ -150,6 +150,8 @@ abstract class ActiveModel extends Model implements ActiveModelInterface { activeModelAttributes.forEach(activeModelAttribute => (this.data[activeModelAttribute] as ActiveModel)._save(req, dataInterface)) + if ('updatedAt' in this.modelSchema) this.data['updatedAt'] = utils.getCurrentDate() + dataInterface(req, this.tableName, this.data.id, this.attributes()) } diff --git a/src/utils/addLeadingZeros.ts b/src/utils/addLeadingZeros.ts new file mode 100644 index 0000000..fc62a24 --- /dev/null +++ b/src/utils/addLeadingZeros.ts @@ -0,0 +1,5 @@ +const addLeadingZeros = (num: number, length: number): string => { + return ('0'.repeat(length) + num).slice(-length) +} + +export default addLeadingZeros \ No newline at end of file diff --git a/src/utils/formatDate.ts b/src/utils/formatDate.ts new file mode 100644 index 0000000..b19e8ea --- /dev/null +++ b/src/utils/formatDate.ts @@ -0,0 +1,43 @@ +import addLeadingZeros from './addLeadingZeros' + +const monthNames = ['January', 'February', 'March', 'April', 'May', 'June', + 'July', 'August', 'September', 'October', 'November', 'December' +] + +const hourToTwelveHour = (hour: number): number => { + const twelveHour = hour % 12 + + if (twelveHour === 0) { + return 12 + } else { + return twelveHour + } +} + +const amOrPm = (hour: number): string => { + if (hour <= 12) { + return 'am' + } else { + return 'pm' + } +} + +const datePart = (date: Date): string => { + return `${date.getDate()} ${monthNames[date.getMonth()]} ${date.getFullYear()}` +} + +const timePart = (date: Date): string => { + return `${hourToTwelveHour(date.getHours())}:${addLeadingZeros(date.getMinutes(), 2)}${amOrPm(date.getHours())}` +} + +const formatDate = (date: Date, withTime: false): string => { + const dateString = datePart(date) + + if (withTime) { + return `${dateString}, ${timePart(date)}` + } else { + return dateString + } +} + +export default formatDate diff --git a/src/utils/getUpdatedAt.ts b/src/utils/getCurrentDate.ts similarity index 57% rename from src/utils/getUpdatedAt.ts rename to src/utils/getCurrentDate.ts index 85b75f0..71b4b0e 100644 --- a/src/utils/getUpdatedAt.ts +++ b/src/utils/getCurrentDate.ts @@ -1,8 +1,8 @@ -const getUpdatedAt = (): string => { +const getCurrentDate = (): string => { let currentDate = new Date() const offset = currentDate.getTimezoneOffset() currentDate = new Date(currentDate.getTime() - (offset*60*1000)) - return currentDate.toISOString().split('T')[0] + return currentDate.toISOString() } -export default getUpdatedAt \ No newline at end of file +export default getCurrentDate \ No newline at end of file diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 7c5f049..d18248c 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,11 +1,15 @@ +import addLeadingZeros from './addLeadingZeros' import cast from './cast' -import getUpdatedAt from './getUpdatedAt' +import formatDate from './formatDate' +import getCurrentDate from './getCurrentDate' import numberToCurrency from './numberToCurrency' import pluralise from './pluralise' export default { + addLeadingZeros: addLeadingZeros, cast: cast, - getUpdatedAt: getUpdatedAt, + formatDate: formatDate, + getCurrentDate: getCurrentDate, pluralise: pluralise, numberToCurrency: numberToCurrency } \ No newline at end of file