Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
feat(users): Add script to change username
Browse files Browse the repository at this point in the history
  • Loading branch information
Blckbrry-Pi committed Mar 28, 2024
1 parent b5c0823 commit 27ef166
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
4 changes: 4 additions & 0 deletions modules/users/module.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ scripts:
public: true
create_user:
name: Create User
update_username:
name: Update Username
description: Update a user's username. Requires user auth in the form of a token.
public: true
authenticate_user:
name: Validate User Token
description: Validate a user token. Throws an error if the token is invalid.
Expand Down
51 changes: 51 additions & 0 deletions modules/users/scripts/update_username.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { ScriptContext, RuntimeError } from "../_gen/scripts/update_username.ts";
import { User } from "../utils/types.ts";

export interface Request {
userToken: string;
username: string;
}

export interface Response {
user: User;
}

export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
await ctx.modules.rateLimit.throttlePublic({ requests: 2, period: 5 * 60 });

// Authenticate user
const { userId } = await ctx.modules.users.authenticateUser({ userToken: req.userToken });

const user = await ctx.db.$transaction(async (db) => {
const userPrevState = await db.user.findFirst({
where: {
id: userId,
},
});

if (!userPrevState) {
throw new RuntimeError("INTERNAL_ERROR", { cause: `User token validated but user doesn't exist` });
}

if (userPrevState.username === req.username) {
return userPrevState;
}

const user = await db.user.update({
where: {
id: userId,
},
data: {
username: req.username,
updatedAt: new Date(),
}
});

return user;
});

return { user };
}
17 changes: 15 additions & 2 deletions modules/users/tests/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { test, TestContext } from "../_gen/test.ts";
import { faker } from "https://deno.land/x/[email protected]/mod.ts";
import { assertEquals } from "https://deno.land/[email protected]/assert/assert_equals.ts";
import { assertExists } from "https://deno.land/[email protected]/assert/assert_exists.ts";
import { assertEquals, assertNotEquals, assertExists } from "https://deno.land/[email protected]/assert/mod.ts";

test("e2e", async (ctx: TestContext) => {
const { user } = await ctx.modules.users.createUser({
Expand All @@ -21,4 +20,18 @@ test("e2e", async (ctx: TestContext) => {
userToken: token.token,
});
assertEquals(user.id, userId);

const newUsername = faker.internet.userName();
const { user: updatedUser } = await ctx.modules.users.updateUsername({
userToken: token.token,
username: newUsername,
});
assertEquals(updatedUser.username, newUsername);

const { users: updatedUsers } = await ctx.modules.users.getUser({
userIds: [user.id],
});
assertEquals(updatedUsers[0].createdAt, user.createdAt);
assertNotEquals(updatedUsers[0].updatedAt, user.updatedAt);
assertEquals(updatedUsers[0].username, newUsername);
});

0 comments on commit 27ef166

Please sign in to comment.