Forráskód Böngészése

feat(User): added name field to user object

Kristian Vos 5 éve
szülő
commit
b2f5a8f15f

+ 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() }

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

@@ -4,6 +4,22 @@
 		<main-header />
 		<div class="container">
 			<!--Implement Validation-->
+			<label class="label">Name</label>
+			<div class="control is-grouped">
+				<p class="control is-expanded has-icon has-icon-right">
+					<input
+						v-model="user.name"
+						class="input"
+						type="text"
+						placeholder="Change name"
+					/>
+				</p>
+				<p class="control">
+					<button class="button is-success" @click="changeName()">
+						Save changes
+					</button>
+				</p>
+			</div>
 			<label class="label">Username</label>
 			<div class="control is-grouped">
 				<p class="control is-expanded has-icon has-icon-right">
@@ -307,6 +323,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))