Browse Source

feat(User): added name field to user object

Owen Diffey 5 years ago
parent
commit
b9afde8c2d

+ 38 - 1
backend/logic/actions/users.js

@@ -434,7 +434,7 @@ module.exports = {
 					status: 'success',
 					data: {
 						_id: account._id,
-						name: "Name goes here",
+						name: account.name,
 						username: account.username,
 						location: account.location,
 						bio: account.bio,
@@ -517,6 +517,7 @@ module.exports = {
 						address: user.email.address
 					},
 					username: user.username,
+					name: user.name,
 					location: user.location,
 					bio: user.bio
 				};
@@ -661,6 +662,42 @@ module.exports = {
 		});
 	}),
 
+	/**
+	 * Updates a user's name
+	 *
+	 * @param {Object} session - the session object automatically added by socket.io
+	 * @param {String} updatingUserId - the updating user's id
+	 * @param {String} newBio - the new name
+	 * @param {Function} cb - gets called with the result
+	 */
+	updateName: hooks.loginRequired((session, updatingUserId, newName, cb) => {
+		async.waterfall([
+			(next) => {
+				if (updatingUserId === session.userId) return next(null, true);
+				db.models.user.findOne({_id: session.userId}, next);
+			},
+
+			(user, next) => {
+				if (user !== true && (!user || user.role !== 'admin')) return next('Invalid permissions.');
+				db.models.user.findOne({ _id: updatingUserId }, next);
+			},
+
+			(user, next) => {
+				if (!user) return next('User not found.');
+				db.models.user.updateOne({ _id: updatingUserId }, {$set: { name: newName }}, {runValidators: true}, next);
+			}
+		], async (err) => {
+			if (err && err !== true) {
+				err = await utils.getError(err);
+				logger.error("UPDATE_NAME", `Couldn't update name for user "${updatingUserId}" to name "${newName}". "${err}"`);
+				cb({status: 'failure', message: err});
+			} else {
+				logger.success("UPDATE_NAME", `Updated name for user "${updatingUserId}" to name "${newName}".`);
+				cb({ status: 'success', message: 'Name updated successfully' });
+			}
+		});
+	}),
+
 	/**
 	 * Updates a user's location
 	 *

+ 1 - 0
backend/logic/db/schemas/user.js

@@ -30,6 +30,7 @@ module.exports = {
 	liked: [{ type: String }],
 	disliked: [{ type: String }],
 	favoriteStations: [{ type: String }],
+	name: { type: String, default: "" },
 	location: { type: String, default: "" },
 	bio: { type: String, default: "" },
 	createdAt: { type: Date, default: Date.now() }

+ 44 - 0
frontend/components/User/Settings.vue

@@ -6,6 +6,25 @@
 			<h2>Settings</h2>
 			<div class="settingsContainer">
 				<div class="settingsRow">
+					<div class="setting">
+						<label class="label">Name</label>
+						<div class="inputArea">
+							<input
+								v-model="user.name"
+								class="input"
+								type="text"
+								placeholder="Change name"
+							/>
+							<button
+								class="button is-success"
+								@click="changeName()"
+							>
+								<i class="material-icons">
+									save
+								</i>
+							</button>
+						</div>
+					</div>
 					<div class="setting">
 						<label class="label">Username</label>
 						<div class="inputArea">
@@ -25,6 +44,8 @@
 							</button>
 						</div>
 					</div>
+				</div>
+				<div class="settingsRow">
 					<div class="setting">
 						<label class="label">Email</label>
 						<div v-if="user.email" class="inputArea">
@@ -322,6 +343,29 @@ export default {
 				}
 			);
 		},
+		changeName() {
+			const { name } = this.user;
+			if (!validation.isLength(name, 1, 64))
+				return new Toast({
+					content: "Name must have between 1 and 64 characters.",
+					timeout: 8000
+				});
+
+			return this.socket.emit(
+				"users.updateName",
+				this.userId,
+				name,
+				res => {
+					if (res.status !== "success")
+						new Toast({ content: res.message, timeout: 8000 });
+					else
+						new Toast({
+							content: "Successfully changed name",
+							timeout: 4000
+						});
+				}
+			);
+		},
 		changeLocation() {
 			const { location } = this.user;
 			if (!validation.isLength(location, 0, 50))