a
This commit is contained in:
parent
35601dbb33
commit
c2882887c9
@ -11,13 +11,13 @@ module.exports = function (sequelize, DataTypes) {
|
|||||||
},
|
},
|
||||||
phone: {
|
phone: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false,
|
|
||||||
unique: true
|
unique: true
|
||||||
|
// allowNull: false, //Tiene que poderse dar de alta usuarios solo con el correo electronico para que siga funcionando las invitaciones por web como hasta ahora
|
||||||
},
|
},
|
||||||
email: {
|
email: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
|
unique: true,
|
||||||
// allowNull: false,
|
// allowNull: false,
|
||||||
// unique: true,
|
|
||||||
// validate: { isEmail: true }
|
// validate: { isEmail: true }
|
||||||
},
|
},
|
||||||
password: {
|
password: {
|
||||||
|
|||||||
@ -4,44 +4,34 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
|
const { generateService, parseParamsToFindOptions } = require('../../helpers/service.helper');
|
||||||
const models = require('../../core/models');
|
const models = require('../../core/models');
|
||||||
|
const Sequelize = require('sequelize');
|
||||||
|
|
||||||
const extraMethods = {
|
const extraMethods = {
|
||||||
|
|
||||||
_getOrCreateUser: async (params, context) => {
|
_getOrCreateUser: async (dataUser) => {
|
||||||
let {user} = context;
|
|
||||||
|
|
||||||
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><< USER');
|
let result = null;
|
||||||
console.log(user);
|
|
||||||
if (user.id) {
|
|
||||||
return user;
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
return models.User.findOrCreate({
|
|
||||||
where: {
|
|
||||||
phone: user.phone
|
|
||||||
},
|
|
||||||
defaults: {
|
|
||||||
email: user.email,
|
|
||||||
name: user.name,
|
|
||||||
surname: user.surname,
|
|
||||||
entityId: user.entityId,
|
|
||||||
// password: crypto.createHash('sha512').update(user.phone).digest('hex'),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.spread(function (resultUser, created) {
|
|
||||||
user = resultUser;
|
|
||||||
console.log('aaaaaaaaaaaaaaaaaaaaaNOSE QUE PASAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
|
|
||||||
console.log(user);
|
|
||||||
return user;
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (dataUser.userResult) {
|
||||||
|
result = { user: dataUser.userResult, isCreated: false};
|
||||||
|
} else {
|
||||||
|
await models.User.findOrCreate({
|
||||||
|
where: {
|
||||||
|
[Sequelize.or] : [{ iphone: dataUser.phone }, { email: dataUser.email }]
|
||||||
|
},
|
||||||
|
defaults: {
|
||||||
|
phone: dataUser.phone,
|
||||||
|
email: dataUser.email,
|
||||||
|
name: dataUser.name,
|
||||||
|
surname: dataUser.surname,
|
||||||
|
entityId: dataUser.entityId,
|
||||||
|
// password: crypto.createHash('sha512').update(user.phone).digest('hex'),
|
||||||
|
}
|
||||||
|
}).then(([user, created]) => {
|
||||||
|
user = user.toJSON();
|
||||||
|
result = {user: user, isCreated: created}});
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -110,7 +110,16 @@ const extraControllers = {
|
|||||||
createInscription: async(req, res, next) => {
|
createInscription: async(req, res, next) => {
|
||||||
const params = extractParamsFromRequest(req, res, {});
|
const params = extractParamsFromRequest(req, res, {});
|
||||||
|
|
||||||
let context = {
|
let dataUser = {
|
||||||
|
id: '0939bb2a-d33d-4290-ac81-fc9faa1c015e',
|
||||||
|
phone: '+34686621059',
|
||||||
|
name: 'aaaaaaaaaaaa',
|
||||||
|
surname: 'bbbbbbb',
|
||||||
|
email: 'lqdvi@lqdvi.com',
|
||||||
|
userResult: null,
|
||||||
|
}
|
||||||
|
|
||||||
|
let dataInscription = {
|
||||||
eventId : params.params.id,
|
eventId : params.params.id,
|
||||||
encodedInvitationCode : req.body.encodedInvitationCode,
|
encodedInvitationCode : req.body.encodedInvitationCode,
|
||||||
code: null,
|
code: null,
|
||||||
@ -121,25 +130,17 @@ const extraControllers = {
|
|||||||
assistants : null, // aforo
|
assistants : null, // aforo
|
||||||
tickets : null, // nº de inscripciones
|
tickets : null, // nº de inscripciones
|
||||||
type : (req.body.code) ? 'reserva' : 'libre',
|
type : (req.body.code) ? 'reserva' : 'libre',
|
||||||
userId: '0939bb2a-d33d-4290-ac81-fc9faa1c015e',
|
|
||||||
user: {
|
|
||||||
phone: '+34686621059',
|
|
||||||
name: 'aaaaaaaaaaaa',
|
|
||||||
surname: 'bbbbbbb',
|
|
||||||
email: 'lqdvi@lqdvi.com',
|
|
||||||
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
context.code = context.encodedInvitationCode; //Buffer.from(context.encodedInvitationCode, 'base64').toString('ascii');
|
dataInscription.code = dataInscription.encodedInvitationCode; //Buffer.from(dataInscription.encodedInvitationCode, 'base64').toString('ascii');
|
||||||
|
|
||||||
//Si viene codigo invitacion _getLevelAndPartner()---------------------
|
//Si viene codigo invitacion _getLevelAndPartner()---------------------
|
||||||
if (context.code) {
|
if (dataInscription.code) {
|
||||||
try {
|
try {
|
||||||
context.reservation = await eventReservationService._getReservaByCode(context.eventId, context.code);
|
dataInscription.reservation = await eventReservationService._getReservaByCode(dataInscription.eventId, dataInscription.code);
|
||||||
if (context.reservation) {
|
if (dataInscription.reservation) {
|
||||||
context.reservation = await context.reservation.toJSON();
|
dataInscription.reservation = await dataInscription.reservation.toJSON();
|
||||||
context.event = context.reservation.Event;
|
dataInscription.event = dataInscription.reservation.Event;
|
||||||
} else {
|
} else {
|
||||||
// No se ha encontrado
|
// No se ha encontrado
|
||||||
return handleResultResponse("Código de reserva no encontrado", null, params, res, httpStatus.NOT_FOUND);
|
return handleResultResponse("Código de reserva no encontrado", null, params, res, httpStatus.NOT_FOUND);
|
||||||
@ -150,9 +151,9 @@ const extraControllers = {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
try {
|
try {
|
||||||
context.event = await eventService._getEvent(context.eventId);
|
dataInscription.event = await eventService._getEvent(dataInscription.eventId);
|
||||||
if (context.event) {
|
if (dataInscription.event) {
|
||||||
context.event = await context.event.toJSON();
|
dataInscription.event = await dataInscription.event.toJSON();
|
||||||
} else {
|
} else {
|
||||||
// No se ha encontrado
|
// No se ha encontrado
|
||||||
return handleResultResponse("Evento no encontrado", null, params, res, httpStatus.NOT_FOUND);
|
return handleResultResponse("Evento no encontrado", null, params, res, httpStatus.NOT_FOUND);
|
||||||
@ -162,49 +163,61 @@ const extraControllers = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('>>>>>>>>>>>>>>>>>>>>>>>esta es la reserva y el evento a la que pertenece');
|
||||||
|
console.log(dataInscription.reservation);
|
||||||
|
console.log(dataInscription.event);
|
||||||
|
|
||||||
|
//creamos o recuperamos el usuario teniendo en cuenta que pude venir por APP o WEB
|
||||||
|
//si viene por web se tendra en cuenta el email y si viene por APP el phone para buscar
|
||||||
console.log('esta es la reserva y el evento a la que pertenece');
|
|
||||||
console.log(context.reservation);
|
|
||||||
console.log(context.event);
|
|
||||||
|
|
||||||
|
|
||||||
//creamos o recuperamos el usuario .then(_getOrCreateUser)-------------
|
|
||||||
try {
|
try {
|
||||||
context.user = await userService._getOrCreateUser(params, context);
|
dataUser.userResult = await userService._getOrCreateUser(dataUser);
|
||||||
context.user = await context.user.toJSON();
|
if (!dataUser.userResult) {
|
||||||
console.log('creamos o recuperamos el usuario');
|
// No se ha encontrado
|
||||||
console.log(context.user);
|
return handleResultResponse("No se ha podido crear o encontrar el usuario dado", null, params, res, httpStatus.NOT_FOUND);
|
||||||
|
};
|
||||||
} catch(error) {
|
} catch(error) {
|
||||||
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res);
|
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log('>>>>>>>>>>>>>>>>>>>>>>este es el usuario que quiere inscribirse');
|
||||||
|
console.log(dataUser.userResult);
|
||||||
|
|
||||||
|
|
||||||
inscriptionService._existsInscription(params, context)
|
try {
|
||||||
|
|
||||||
|
//Comprobamos que el usuario no tenga ya inscripcion para ese evento
|
||||||
|
dataInscription.inscription = await inscriptionService._getInscription(dataInscription.event.id, dataUser.userResult.user.id);
|
||||||
|
if (dataInscription.inscription) {
|
||||||
|
console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>inscription existe, revisar si es con codigo vip y cambiarlo');
|
||||||
|
console.log(dataInscription.inscription);
|
||||||
|
return handleResultResponse(result, null, params, res, (result === null) ? httpStatus.NOT_FOUND : httpStatus.OK);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const source = (dataUser.userResult.isCreated) ? 'web' : 'app';
|
||||||
|
if (dataInscription.reservation)
|
||||||
|
dataInscription.inscription = await inscriptionService._createInscription(dataInscription.event.id, dataUser.userResult.user.id, dataInscription.type, true, source, dataInscription.reservation.id )
|
||||||
|
|
||||||
|
else
|
||||||
|
dataInscription.inscription = await inscriptionService._createInscription(dataInscription.event.id, dataUser.userResult.user.id, dataInscription.type, true, source, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
return handleErrorResponse(MODULE_NAME, 'createInscription', error, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
return handleResultResponse("FIN", null, params, res, httpStatus.NOT_FOUND);
|
||||||
|
/*
|
||||||
.then(inscriptionService._getInscriptionCount)
|
.then(inscriptionService._getInscriptionCount)
|
||||||
.then(function () { if (context.code) {return eventReservationService._updateReservation } else { return evenService._updateEvent }})
|
.then(function () { if (dataInscription.code) {return eventReservationService._updateReservation } else { return evenService._updateEvent }})
|
||||||
.then(inscriptionService._createInscription)
|
.then(inscriptionService._createInscription)
|
||||||
.catch(_handleError);
|
.catch(_handleError);
|
||||||
//_createInscription(user, congressId, invitationCode, source, callback);
|
//_createInscription(user, congressId, invitationCode, source, callback);
|
||||||
|
|
||||||
// const result = await eventService.createIncription(params, { user: req.user, lapse: 'pass' });
|
// const result = await eventService.createIncription(params, { user: req.user, lapse: 'pass' });
|
||||||
// return handleResultResponse(result, result.count, params, res);
|
// return handleResultResponse(result, result.count, params, res);
|
||||||
|
*/
|
||||||
},
|
},
|
||||||
|
|
||||||
/*
|
|
||||||
findOne2: async (req, res, next) => {
|
|
||||||
const params = extractParamsFromRequest(req, res, { includeAll: false });
|
|
||||||
|
|
||||||
try {
|
|
||||||
const result = await eventService.fetchOne(params, { user: req.user });
|
|
||||||
return handleResultResponse(result, null, params, res, (result === null) ? httpStatus.NOT_FOUND : httpStatus.OK);
|
|
||||||
} catch (error) {
|
|
||||||
handleErrorResponse(MODULE_NAME, 'findOne', error, res)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
*/
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -7,21 +7,12 @@ const models = require('../../core/models');
|
|||||||
|
|
||||||
const extraMethods = {
|
const extraMethods = {
|
||||||
|
|
||||||
_existsInscription: (params, context) => {
|
_getInscription: (eventId, userId) => {
|
||||||
|
return models.EventInscription.findOne({
|
||||||
return new Promise (function (resolve, reject) {
|
where: {
|
||||||
models.EventInscription.count({
|
eventId: eventId,
|
||||||
where: params,
|
userId: userId
|
||||||
|
},
|
||||||
}).then(function (count) {
|
|
||||||
if (count > 0) {
|
|
||||||
reject({ message: 'Ya existe una inscripción para ese email' })
|
|
||||||
} else {
|
|
||||||
resolve(true);
|
|
||||||
}
|
|
||||||
}).catch(function (error) {
|
|
||||||
reject(error)
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -43,20 +34,20 @@ const extraMethods = {
|
|||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
_createInscription: (params, context) => {
|
_createInscription: (eventId, userId, type, valid, source, reservationId) => {
|
||||||
|
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
database.models.Inscription.create({
|
models.EventInscription.create({
|
||||||
eventId: context.eventId,
|
eventId: eventId,
|
||||||
type: context.type,
|
userId: userId,
|
||||||
source: 'web',
|
type: type,
|
||||||
userId: context.user.id,
|
source: source,
|
||||||
//valid: !(tickets > assistants),
|
valid: valid,
|
||||||
reservationId: (context.code) ? context.reservation.id : null
|
// valid: !(tickets > assistants),
|
||||||
|
reservationId: reservationId
|
||||||
})
|
})
|
||||||
.then(function (result) {
|
.then(function (result) {
|
||||||
inscription = result.dataValues;
|
inscription = result.dataValues;
|
||||||
inscription.user = context.user;
|
|
||||||
resolve(inscription);
|
resolve(inscription);
|
||||||
})
|
})
|
||||||
.catch(function (error) {
|
.catch(function (error) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user