Skip to content

Commit

Permalink
fix: email not being send
Browse files Browse the repository at this point in the history
  • Loading branch information
Joabesv committed Jul 20, 2024
1 parent a866a2a commit 8c27c31
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions app/api/users/complete/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ 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) {
throw new errors.NotFound('Usuário não encontrado')
}

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)
}
Expand Down
24 changes: 15 additions & 9 deletions app/models/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -38,7 +39,7 @@ const Model = module.exports = Schema({

devices: [{
phone: String,
token: {
token: {
type: String,
required: true
},
Expand All @@ -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)
Expand All @@ -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()
}
})
Expand Down

0 comments on commit 8c27c31

Please sign in to comment.