Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ SENDGRID_API_KEY=
SENDGRID_FROM_EMAIL=
SENDGRID_FROM_NAME=
SENDGRID_GENERIC_TEMPLATE=
#
# Note! We are using the dev.app.hackjunction.com
# database here instead of the local one
#
#

# Note! We are using the dev.app.hackjunction.com

# database here instead of the local one

#

MONGODB_URI=
FRONTEND_URL=
DEVTOOLS_ENABLED=
Expand Down
1 change: 1 addition & 0 deletions backend/modules/recruitment/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ controller.createRecruitmentProfile = async (
github: userProfile.github,
linkedin: userProfile.linkedin,
portfolio: userProfile.portfolio,
curriculumVitae: userProfile.curriculumVitae,
},
recruitmentOptions: userProfile.recruitmentOptions,
registrations: userProfile.registrations,
Expand Down
107 changes: 56 additions & 51 deletions backend/modules/user-profile/controller.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,111 @@
const _ = require('lodash');
const { UserProfile } = require('./model');
const { NotFoundError } = require('../../common/errors/errors');
const UserProfileHelpers = require('./helpers');
const _ = require('lodash')
const { UserProfile } = require('./model')
const { NotFoundError } = require('../../common/errors/errors')
const UserProfileHelpers = require('./helpers')

const controller = {};
const controller = {}

controller.getUserProfile = userId => {
return UserProfile.findOne({
userId
userId,
}).then(userProfile => {
if (!userProfile) {
throw new NotFoundError(`UserProfile with id ${userId} does not exist`);
throw new NotFoundError(
`UserProfile with id ${userId} does not exist`
)
}
return userProfile;
});
};
return userProfile
})
}

controller.getUserProfiles = userIds => {
return UserProfile.find({
userId: {
$in: userIds
}
});
};
$in: userIds,
},
})
}

controller.queryProfiles = async query => {
const found = await UserProfile.find(query.query)
.sort('updatedAt')
.skip(query.pagination.skip)
.limit(query.pagination.limit);
.limit(query.pagination.limit)

const count = await UserProfile.find(query.query).countDocuments();
return { found, count };
};
const count = await UserProfile.find(query.query).countDocuments()
return { found, count }
}

controller.getUserProfilesPublic = userIds => {
return controller.getUserProfiles(userIds).then(profiles => {
return UserProfile.publicFields(profiles);
});
};
return UserProfile.publicFields(profiles)
})
}

controller.createUserProfile = (data, userId) => {
const userProfile = new UserProfile({
userId,
firstName: data.firstName,
lastName: data.lastName,
email: data.email,
avatar: data.avatar
});
avatar: data.avatar,
})

return userProfile.save();
};
return userProfile.save()
}

controller.updateUserProfile = async (data, userId) => {
const validatedData = await UserProfileHelpers.validate(data);
const validatedData = await UserProfileHelpers.validate(data)
return controller.getUserProfile(userId).then(userProfile => {
return UserProfile.updateAllowed(userProfile, validatedData);
});
};
return UserProfile.updateAllowed(userProfile, validatedData)
})
}

controller.syncRegistration = async registration => {
const data = {
registration: registration._id,
event: registration.event,
status: registration.status
};
status: registration.status,
}
return controller.getUserProfile(registration.user).then(profile => {
if (profile.registrations.length === 0) {
profile.registrations = [data];
profile.registrations = [data]
} else {
profile.registrations = profile.toJSON().registrations.map(r => {
if (r.event.toString() === data.event.toString()) {
return data;
return data
}
return r;
});
return r
})
}

return profile.save();
});
};
return profile.save()
})
}

controller.getUsersByEmail = email => {
return UserProfile.find({ email });
};
return UserProfile.find({ email })
}

controller.searchUsers = terms => {
return UserProfile.find({ $text: { $search: terms } }).limit(25);
};
return UserProfile.find({ $text: { $search: terms } }).limit(25)
}

controller.getRecruiters = () => {
return UserProfile.find({
$nor: [{ recruiterEvents: { $exists: false } }, { recruiterEvents: { $size: 0 } }]
});
};
$nor: [
{ recruiterEvents: { $exists: false } },
{ recruiterEvents: { $size: 0 } },
],
})
}

controller.updateRecruiter = (userId, events, organisation) => {
return UserProfile.findOne({ userId }).then(user => {
user.recruiterEvents = events;
user.recruiterOrganisation = organisation;
return user.save();
});
};
user.recruiterEvents = events
user.recruiterOrganisation = organisation
return user.save()
})
}

module.exports = controller;
module.exports = controller
26 changes: 13 additions & 13 deletions backend/modules/user-profile/helpers.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
const { RegistrationFields } = require('@hackjunction/shared');
const yup = require('yup');
const { RegistrationFields } = require('@hackjunction/shared')
const yup = require('yup')

const UserProfileHelpers = {
validate: data => {
const validations = {};
const validations = {}
Object.keys(data).forEach(field => {
const fieldConfig = RegistrationFields.getField(field);
const fieldConfig = RegistrationFields.getField(field)
if (fieldConfig) {
validations[field] = fieldConfig.validationSchema(false);
validations[field] = fieldConfig.validationSchema(false)
}
});
})

validations['avatar'] = yup
validations.avatar = yup
.string()
.url()
.nullable();
.nullable()

const schema = yup.object().shape(validations);
return schema.validate(data, { stripUnknown: true });
}
};
const schema = yup.object().shape(validations)
return schema.validate(data, { stripUnknown: true })
},
}

module.exports = UserProfileHelpers;
module.exports = UserProfileHelpers
90 changes: 50 additions & 40 deletions backend/modules/user-profile/model.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,50 @@
const mongoose = require('mongoose');
const _ = require('lodash');
const updateAllowedPlugin = require('../../common/plugins/updateAllowed');
const publicFieldsPlugin = require('../../common/plugins/publicFields');
const Shared = require('@hackjunction/shared');
const AuthController = require('../auth/controller');
const { RegistrationFields } = Shared;
const mongoose = require('mongoose')
const _ = require('lodash')
const Shared = require('@hackjunction/shared')
const updateAllowedPlugin = require('../../common/plugins/updateAllowed')
const publicFieldsPlugin = require('../../common/plugins/publicFields')
const AuthController = require('../auth/controller')

const { RegistrationFields } = Shared

const UserProfileSchema = new mongoose.Schema({
userId: {
type: String,
required: true,
unique: true
unique: true,
},
avatar: {
type: String
type: String,
},
registrations: {
type: Array,
required: false,
default: []
default: [],
},
recruiterEvents: {
type: Array,
required: false,
default: [],
set: function(recruiterEvents) {
this._previousRecruiterEvents = this.recruiterEvents;
return recruiterEvents;
}
set(recruiterEvents) {
this._previousRecruiterEvents = this.recruiterEvents
return recruiterEvents
},
},
recruiterOrganisation: {
type: String,
required: false
}
});
required: false,
},
})

/** Build user profile fields based on possible registration questions */
const fields = {};
const fields = {}
_.forOwn(RegistrationFields.getFields(), (value, fieldName) => {
if (value.hasOwnProperty('userProfileConfig')) {
fields[fieldName] = value.userProfileConfig;
fields[fieldName] = value.userProfileConfig
}
});
})

UserProfileSchema.add(fields);
UserProfileSchema.add(fields)

/* // Virtual field to fetch registrations if required
UserProfileSchema.virtual('registrations', {
Expand All @@ -53,43 +54,52 @@ UserProfileSchema.virtual('registrations', {
}); */

UserProfileSchema.post('save', function(doc, next) {
if (_.xor(this._previousRecruiterEvents, this.recruiterEvents).length !== 0) {
if (
_.xor(this._previousRecruiterEvents, this.recruiterEvents).length !== 0
) {
if (this.recruiterEvents.length === 0) {
AuthController.revokeRecruiterPermission(this.userId);
AuthController.revokeRecruiterPermission(this.userId)
} else {
AuthController.grantRecruiterPermission(this.userId);
AuthController.grantRecruiterPermission(this.userId)
}
AuthController.updateMetadata(this.userId, {
recruiterEvents: this.recruiterEvents,
recruiterOrganisation: this.recruiterOrganisation
});
recruiterOrganisation: this.recruiterOrganisation,
})
}
next();
});
next()
})

UserProfileSchema.set('timestamps', true);
UserProfileSchema.set('timestamps', true)

UserProfileSchema.index({
userId: 1
});
userId: 1,
})

UserProfileSchema.index({
firstName: 'text',
lastName: 'text',
email: 'text'
});
email: 'text',
})

UserProfileSchema.plugin(updateAllowedPlugin, {
blacklisted: ['__v', '_id', 'createdAt', 'updatedAt', 'userId']
});
blacklisted: ['__v', '_id', 'createdAt', 'updatedAt', 'userId'],
})

UserProfileSchema.plugin(publicFieldsPlugin, {
fields: ['userId', 'avatar', 'firstName', 'lastName', 'email', 'phoneNumber']
});
fields: [
'userId',
'avatar',
'firstName',
'lastName',
'email',
'phoneNumber',
],
})

const UserProfile = mongoose.model('UserProfile', UserProfileSchema);
const UserProfile = mongoose.model('UserProfile', UserProfileSchema)

module.exports = {
UserProfile,
UserProfileSchema
};
UserProfileSchema,
}
Loading