From af9aea019e74a0fa3921d52cb21b5a382933e340 Mon Sep 17 00:00:00 2001 From: EvgenyWas Date: Mon, 10 Jun 2024 18:22:01 +0100 Subject: [PATCH] feat: extend user profile model --- server/models/user/profile.model.ts | 113 +++++++++++++--------------- 1 file changed, 51 insertions(+), 62 deletions(-) diff --git a/server/models/user/profile.model.ts b/server/models/user/profile.model.ts index 48583c5..8ab4a76 100644 --- a/server/models/user/profile.model.ts +++ b/server/models/user/profile.model.ts @@ -2,73 +2,62 @@ import { Schema, model } from 'mongoose'; import { AUTH_PROVIDERS, MAX_USER_SOCIALS, MIN_USER_NAME_LENGTH } from '~/configs/properties'; import { emailValidator, passwordValidator, base64Validator } from '~/utils/validators'; -const schema = new Schema({ - name: { - type: String, - required: true, - minLength: MIN_USER_NAME_LENGTH, - }, - email: { - type: String, - required: true, - immutable: true, - validate: { - validator: (value: string) => emailValidator.safeParse(value).success, +const schema = new Schema( + { + name: { + type: String, + required: true, + minLength: MIN_USER_NAME_LENGTH, }, - }, - password: { - type: String, - validate: { - validator: (value: string) => passwordValidator.safeParse(value).success, + email: { + type: String, + required: true, + immutable: true, + validate: { + validator: (value: string) => emailValidator.safeParse(value).success, + }, }, - }, - avatar: { - type: String, - validate: { - validator: (value?: string) => !base64Validator.safeParse(value).success, - message: 'Avatar in base64 format is forbidden.', + password: { + type: String, + validate: { + validator: (value: string) => passwordValidator.safeParse(value).success, + }, }, - default: '', - }, - description: { - type: String, - default: '', - }, - address: { - type: String, - default: '', - }, - phone: { - type: String, - default: '', - }, - socials: { - type: Array, - validate: { - validator: (value: Array) => value.length <= MAX_USER_SOCIALS, - message: `The maximum number of socials is ${MAX_USER_SOCIALS}`, + avatar: { + type: String, + validate: { + validator: (value?: string) => !base64Validator.safeParse(value).success, + message: 'Avatar in base64 format is forbidden.', + }, + default: '', + }, + description: { + type: String, + default: '', + }, + address: { + type: String, + default: '', + }, + phone: { + type: String, + default: '', + }, + socials: { + type: Array, + validate: { + validator: (value: Array) => value.length <= MAX_USER_SOCIALS, + message: `The maximum number of socials is ${MAX_USER_SOCIALS}`, + }, + default: () => Array(MAX_USER_SOCIALS).fill(''), + }, + auth_provider: { + type: String, + enum: Object.values(AUTH_PROVIDERS), }, - default: () => Array(MAX_USER_SOCIALS).fill(''), - }, - auth_provider: { - type: String, - enum: Object.values(AUTH_PROVIDERS), - }, - createdAt: { - type: Date, - immutable: true, - default: () => new Date(), - }, - updatedAt: { - type: Date, - default: () => new Date(), }, -}); - -schema.pre('save', function (next) { - this.updatedAt = new Date(); - next(); -}); + { timestamps: true }, +); const Profile = model('user_profile', schema);