Skip to content

Commit 3e894bc

Browse files
konpokuzzdhybthu
andauthored
feat(api): added update user profile logic (#1634)
* feat: added weekly updater * Implemented renew weekly failure return * modified for lint check * modified for lint check * Removed admin * temporary commit * Fixed: weekly cover error * modified to pass the lint test * fix: fixed weekly update and permission check * feat: added log for weekly update * updated contest team member limit * debugged for team member limit check * feat(api): add user profile update logic --------- Co-authored-by: zzdhyb <139191187+zzdhybthu@users.noreply.github.com>
1 parent 0d98d93 commit 3e894bc

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

src/hasura/user.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { gql } from "graphql-request";
2+
import { client } from "..";
3+
4+
export const mutation_update_user_profile = async (uuid: string, className?: string, department?: string, realname?: string, student_no?: string, username?: string) => {
5+
const variables: any = { };
6+
if (className !== undefined) variables.class = className;
7+
if (department !== undefined) variables.department = department;
8+
if (realname !== undefined) variables.realname = realname;
9+
if (student_no !== undefined) variables.student_no = student_no;
10+
if (username !== undefined) variables.username = username;
11+
const query: any = await client.request(
12+
gql`
13+
mutation UpdateProfile(
14+
$uuid: uuid!
15+
$class: String
16+
$department: String
17+
$realname: String
18+
$student_no: String
19+
$username: String
20+
) {
21+
update_users_by_pk(
22+
pk_columns: { uuid: $uuid }
23+
_set: {
24+
class: $class
25+
department: $department
26+
username: $username
27+
student_no: $student_no
28+
realname: $realname
29+
}
30+
) {
31+
updated_at
32+
}
33+
}
34+
`,
35+
{ uuid, ...variables}
36+
);
37+
return query.update_users_by_pk.updated_at;
38+
}

src/routes/user.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import authenticate, {
1212
import * as validator from "../helpers/validate";
1313
import { client } from "..";
1414
import { sendMessageVerifyCode } from "../helpers/short_message";
15+
import { mutation_update_user_profile } from "../hasura/user";
16+
17+
1518

1619
const router = express.Router();
1720

@@ -675,10 +678,43 @@ router.post("/edit-profile", authenticate(), async (req, res) => {
675678
} catch (err) {
676679
console.error(err);
677680
return res.status(500).send(err);
681+
};
682+
});
683+
684+
router.post("/update", authenticate(), async(req, res) => {
685+
/**
686+
* @route POST /user/update
687+
* @description 更新用户资料(除email/phone/password外的其他字段)
688+
* @body {className?: string, department?: string, realname?: string, student_no?: string, username?: string}
689+
* @returns 更改状态
690+
*/
691+
const updates = req.body;
692+
if (!updates.className && !updates.department && !updates.realname && !updates.student_no && !updates.username) {
693+
return res.status(422).send("422 Unprocessable Entity: Missing fields to update");
694+
}
695+
const className = updates.className;
696+
const department = updates.department;
697+
const realname = updates.realname;
698+
const student_no = updates.student_no;
699+
const username = updates.username;
700+
if (Object.keys(updates).length === 0) {
701+
return res.status(422).send("422 Unprocessable Entity: No fields to update");
702+
}
703+
try {
704+
const result = mutation_update_user_profile(req.auth.user.uuid, className, department, realname, student_no, username);
705+
if (result) {
706+
return res.status(200).send(result);
707+
} else {
708+
return res.status(500).send("500 Internal Server Error: Failed to update user profile");
709+
}
710+
} catch (err) {
711+
console.error(err);
712+
return res.status(500).send(err);
678713
}
679714
});
680715

681-
router.post("/delete", authenticate(), async (req, res) => {
716+
717+
router.post("/delete", authenticate(), async(req, res) => {
682718
/**
683719
* @route POST /user/delete
684720
* @description 删除用户。先验证请求中的验证码与`verificationToken`中的是否一致,再删除`hasura`中的数据列

0 commit comments

Comments
 (0)