Skip to content

Commit

Permalink
Merge pull request #497 from hopetambala/PLATFORM-583_2
Browse files Browse the repository at this point in the history
PLATFORM-583: New signup process on puente-manage
  • Loading branch information
hopetambala authored Jul 21, 2022
2 parents 1b3d457 + 3fce700 commit cbb1122
Show file tree
Hide file tree
Showing 9 changed files with 30,392 additions and 7,479 deletions.
4 changes: 3 additions & 1 deletion .env.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
PARSE_ENV=dev
PARSE_APP_ID="myAppId"
PARSE_JAVASCRIPT_KEY=_PLACEHOLDER_
PARSE_SERVER_URL="http://localhost:1337/parse"
PARSE_SERVER_URL="http://localhost:1337/parse"
PUENTE_SMS_EMAIL_API_URL=http://puentes-messaging-microservice.com
PUENTE_MANAGE_URL=https://sample-puente-manage.app/"
7 changes: 7 additions & 0 deletions _tests_/integration/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ describe('role testing', () => {
password: 'dracarys',
email: '[email protected]',
organization: 'got',
restParams: {
runMessaging: false,
},
};
return cloudFunctions.signup(credentials).then((result) => {
const jsonString = JSON.stringify(result);
Expand Down Expand Up @@ -59,6 +62,10 @@ describe('role testing', () => {
email: '[email protected]',
organization: 'got',
phonenumber: 1234567890,
restParams: {
runMessaging: false,
path: 'email',
},
};
return cloudFunctions.signup(credentials).then((result) => {
const jsonString = JSON.stringify(result);
Expand Down
8 changes: 8 additions & 0 deletions _tests_/integration/roles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ describe('role testing', () => {
password: 'leia',
email: '[email protected]',
organization: 'star-wars',
restParams: {
runMessaging: false,
path: 'email',
},
};
return cloudFunctions.signup(credentials).then((result) => {
const jsonString = JSON.stringify(result);
Expand All @@ -94,6 +98,10 @@ describe('role testing', () => {
email: '[email protected]',
organization: 'star-wars',
phonenumber: '1234567373',
restParams: {
runMessaging: false,
path: 'email',
},
};
return cloudFunctions.signup(credentials).then((result) => {
const jsonString = JSON.stringify(result);
Expand Down
48 changes: 34 additions & 14 deletions cloud/src/definer/auth.definer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const services = require('../services');

/** ******************************************
SIGN UP
Receives user attributes and registers user
Expand All @@ -12,27 +14,37 @@ Input Paramaters:
role - role of user within organization
******************************************* */
Parse.Cloud.define('signup', (request) => new Promise((resolve, reject) => {
const {
firstname,
lastname,
password,
email,
phonenumber,
organization,
restParams,
} = request.params;


const user = new Parse.User();
user.set('firstname', String(request.params.firstname));
user.set('lastname', String(request.params.lastname));
// user.set('username', String(request.params.username));
user.set('password', String(request.params.password));
if (String(request.params.email) !== '') {
user.set('email', String(request.params.email));
user.set('firstname', String(firstname));
user.set('lastname', String(lastname));
user.set('password', String(password));
if (String(email) !== '') {
user.set('email', String(email));
}
user.set('organization', String(request.params.organization));
user.set('phonenumber', String(request.params.phonenumber));
user.set('organization', String(organization));
user.set('phonenumber', String(phonenumber));

if (request.params.phonenumber) {
user.set('username', String(request.params.phonenumber));
if (phonenumber) {
user.set('username', String(phonenumber));
} else {
user.set('username', String(request.params.email));
user.set('username', String(email));
}

let userRole = '';
// Query to count number of users for the organization passed into function
const userQuery = new Parse.Query(Parse.User);
userQuery.equalTo('organization', String(request.params.organization));
userQuery.equalTo('organization', String(organization));
userQuery.count().then((results) => {
// first user signed up, gets admin accesss
if (results === 0) {
Expand All @@ -46,10 +58,18 @@ Parse.Cloud.define('signup', (request) => new Promise((resolve, reject) => {
userRole = 'contributor';
}
return user;
}).then((userUpdated) => {
}).then(async (userUpdated) => {
// sign up user
userUpdated.signUp().then((result) => {
userUpdated.signUp().then(async (result) => {
console.log(`User created successfully with name ${result.get('username')} and email: ${result.get('email')}`); // eslint-disable-line
const userObject = {
objectId: result.id,
firstname,
email,
phonenumber,
};
if (restParams) await services.messaging.sendMessage(restParams, userObject);

const acl = new Parse.ACL();
acl.setPublicReadAccess(true);
acl.setWriteAccess(result, true);
Expand Down
1 change: 0 additions & 1 deletion cloud/src/services/batch/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,5 @@ const Batch = {
});
},
};
// export default batch;

module.exports = Batch;
2 changes: 2 additions & 0 deletions cloud/src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const Batch = require('./batch/batch.js');
const Roles = require('./roles/roles.js');
const Aggregate = require('./aggregate/aggregate.js');
const Post = require('./post/post.js');
const Messaging = require('./messaging/messaging.js');

const services = {};

services.batch = Batch;
services.roles = Roles;
services.aggregate = Aggregate;
services.post = Post;
services.messaging = Messaging;

module.exports = services;
80 changes: 80 additions & 0 deletions cloud/src/services/messaging/messaging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const axios = require('axios').default;

const { PUENTE_SMS_EMAIL_API_URL: url } = process.env;


/**
* Too tightly coupled with just signup and password reset
*/

async function sendEmail(apiURL, restParamsData, parseObject) {
const { emailSubject, type } = restParamsData;
const { email: emailAddress, objectId, firstname } = parseObject;

const payload = {
emailSubject,
objectId,
userFullName: firstname,
emailsToSendTo: [
emailAddress,
],
type, // signup, passwordReset,default
};

return axios.post(`${apiURL}/email`, payload);
}

async function sendText(apiURL, restParamsData, parseObject) {
const { textTo: phoneNumber, type } = restParamsData;
const { objectId, firstname } = parseObject;

let textBody = '';

if (type === 'signup') textBody = `Hello ${firstname}! Verify your Puente account at\nhttps://puente-manage.vercel.app/account/verify?objectId=${objectId}`;
if (type === 'passwordReset') textBody = `Hello ${firstname}! Reset your Puente password at\nhttps://puente-manage.vercel.app/account/reset?objectId=${objectId}`;

const payload = {
textTo: phoneNumber,
textBody,
};

return axios.post(`${apiURL}/text`, payload);
}


const Messaging = {
/**
* Performs a query based on the parameter defined in a column
*
* @example
* sendMessage(
* 'text',
* {
* "textTo": "1234567",
* "textBody": "Text"
* }
* )
*
* @param {string} path which endpoint to hit
* @param {string} restParamData Data consisting of which
* @returns Results of Query
*/
sendMessage: async function sendMessage(restParams, parseObject) {
const {
runMessaging,
path,
data,
} = restParams;

if (!runMessaging) return null;

try {
const response = path === 'email' ? await sendEmail(url, data, parseObject) : await sendText(url, data, parseObject);
return response;
} catch (e) {
return e;
}
},
};

module.exports = Messaging;
Loading

0 comments on commit cbb1122

Please sign in to comment.