From 8c27c31512ea23623d55d955df58ac3bb965b6ce Mon Sep 17 00:00:00 2001 From: Joabesv Date: Sat, 20 Jul 2024 16:30:21 -0300 Subject: [PATCH] fix: email not being send --- app/api/users/complete/func.js | 6 +++--- app/models/users.js | 24 +++++++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/app/api/users/complete/func.js b/app/api/users/complete/func.js index 9ba2aa1..e152e94 100644 --- a/app/api/users/complete/func.js +++ b/app/api/users/complete/func.js @@ -3,7 +3,7 @@ const app = require('@/app') const errors = require('@/errors') const Fields = require('../fields') -module.exports = async(context) => { +module.exports = async (context) => { const { user } = context if (!user) { @@ -11,10 +11,10 @@ module.exports = async(context) => { } user.set(_.pick(context.body, Fields.update)) - + // Save try { - await user.save() + await user.save(context.body.email) } catch (e) { app.helpers.validate.mongoError(e) } diff --git a/app/models/users.js b/app/models/users.js index ffcac66..4629600 100644 --- a/app/models/users.js +++ b/app/models/users.js @@ -2,6 +2,7 @@ const _ = require('lodash') const Schema = require('mongoose').Schema const jwt = require('jsonwebtoken') const app = require('@/app') +const errors = require('@/errors') const Model = module.exports = Schema({ oauth: { @@ -15,7 +16,7 @@ const Model = module.exports = Schema({ ra: { type: Number, unique: true, - partialFilterExpression: { ra: { $exists: true }} + partialFilterExpression: { ra: { $exists: true } } }, email: { type: String, @@ -24,7 +25,7 @@ const Model = module.exports = Schema({ message: props => `${props.value} não é um e-mail válido` }, unique: true, - partialFilterExpression: { email: { $exists: true }} + partialFilterExpression: { email: { $exists: true } } }, confirmed: { type: Boolean, @@ -38,7 +39,7 @@ const Model = module.exports = Schema({ devices: [{ phone: String, - token: { + token: { type: String, required: true }, @@ -53,16 +54,16 @@ Model.virtual('isFilled').get(function () { return this.ra && this.email }) -Model.method('addDevice', function(device) { +Model.method('addDevice', function (device) { this.devices.unshift(device) this.devices = _.uniqBy(this.devices, 'deviceId') }) -Model.method('removeDevice', function(deviceId) { - this.devices = _.remove(this.devices, { deviceId }) +Model.method('removeDevice', function (deviceId) { + this.devices = _.remove(this.devices, { deviceId }) }) -Model.method('sendNotification', async function(title, body) { +Model.method('sendNotification', async function (title, body) { const sendNotification = app.helpers.notification.sendNotification const devicesTokens = this.devices.map(device => device.token) @@ -85,8 +86,13 @@ Model.method('sendConfirmation', async function () { app.agenda.now('sendConfirmation', this.toObject({ virtuals: true })) }) -Model.pre('save', async function () { - if(this.isFilled && !this.confirmed) { +Model.pre('save', async function (email) { + if (this.email === email) { + throw new errors.Conflict('E-mail já cadastrado, realize seu login via Facebook ou entre em contato com nosso suporte') + } + + + if ((this.isFilled && !this.confirmed)) { this.sendConfirmation() } })