Skip to content

Commit

Permalink
make routes more descriptive
Browse files Browse the repository at this point in the history
  • Loading branch information
choden-dev committed Mar 10, 2024
1 parent 8a0715b commit f8964ac
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 73 deletions.
2 changes: 1 addition & 1 deletion client/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
Expand Down
34 changes: 19 additions & 15 deletions client/src/models/__generated__/schema.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 45 additions & 33 deletions common/__generated__/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 32 additions & 13 deletions server/src/data-layer/services/UserDataService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { additionalInfoMock } from "test-config/mocks/User.mock"
import UserDataService from "./UserDataService"
import { cleanFirestore } from "test-config/TestUtils"

const TEST_UID_1 = "testUser"

describe("UserService integration tests", () => {
let userService: UserDataService

Expand All @@ -14,33 +16,50 @@ describe("UserService integration tests", () => {
})

it("should add a user", async () => {
await userService.createUserData("testUser", additionalInfoMock)
const user = await userService.getUserData("testUser")
await userService.createUserData(TEST_UID_1, additionalInfoMock)
const user = await userService.getUserData(TEST_UID_1)

expect(user).toEqual({ ...additionalInfoMock, uid: TEST_UID_1 })
})

it("should know if a user has a document", async () => {
let result = await userService.userDataExists(TEST_UID_1)
expect(result).toEqual(false)

await userService.createUserData(TEST_UID_1, additionalInfoMock)
result = await userService.userDataExists(TEST_UID_1)
expect(result).toEqual(true)

expect(user).toEqual(additionalInfoMock)
await userService.deleteUserData(TEST_UID_1)
result = await userService.userDataExists(TEST_UID_1)
expect(result).toEqual(false)
})

it("edit a user", async () => {
await userService.createUserData("testUser", additionalInfoMock)
await userService.editUserData("testUser", { does_racing: false })
const user = await userService.getUserData("testUser")
await userService.createUserData(TEST_UID_1, additionalInfoMock)
await userService.editUserData(TEST_UID_1, { does_racing: false })
const user = await userService.getUserData(TEST_UID_1)

expect(user).toEqual({ ...additionalInfoMock, does_racing: false })
expect(user).toEqual({
...additionalInfoMock,
does_racing: false,
uid: TEST_UID_1
})
})

it("should delete a user", async () => {
await userService.createUserData("testUser", additionalInfoMock)
await userService.createUserData(TEST_UID_1, additionalInfoMock)

await userService.deleteUserData("testUser")
const user = await userService.getUserData("testUser")
await userService.deleteUserData(TEST_UID_1)
const user = await userService.getUserData(TEST_UID_1)

expect(user).not.toEqual(additionalInfoMock)
expect(user).not.toEqual({ ...additionalInfoMock, uid: TEST_UID_1 })
expect(user).toEqual(undefined)
})

it("should get all users", async () => {
await userService.createUserData("testUser", additionalInfoMock)
await userService.createUserData("testUser1", additionalInfoMock)
await userService.createUserData(TEST_UID_1, additionalInfoMock)
await userService.createUserData("testUser2", additionalInfoMock)

const users = await userService.getAllUserData()

Expand Down
7 changes: 7 additions & 0 deletions server/src/data-layer/services/UserDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ export default class UserDataService {

public async getUserData(uid: string) {
const userDoc = await FirestoreCollections.users.doc(uid).get()
const data = userDoc.data()
if (data === undefined) return undefined
return { ...userDoc.data(), uid }
}

public async userDataExists(uid: string) {
const snapshot = await FirestoreCollections.users.doc(uid).get()
return snapshot.exists
}

public async getFilteredUsers(filters: Partial<UserAdditionalInfo>) {
// TODO
}
Expand Down
17 changes: 13 additions & 4 deletions server/src/middleware/__generated__/routes.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions server/src/service-layer/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import UserDataService from "data-layer/services/UserDataService"
import {
CreateUserRequestBody,
EditUsersRequestBody,
SelfRequestModel
} from "service-layer/request-models/UserRequests"
Expand All @@ -8,11 +9,12 @@ import {
Body,
Controller,
Get,
Post,
Route,
Security,
SuccessResponse,
Request
Request,
Patch,
Put
} from "tsoa"

@Route("users")
Expand All @@ -37,15 +39,23 @@ export class UsersController extends Controller {
}

@SuccessResponse("200", "Created")
@Security("jwt")
@Post()
public async createUser(@Body() requestBody: { id: string }): Promise<void> {
this.setStatus(200) // set return status 200
@Security("jwt", ["admin"])
@Put("create")
public async createUser(
@Body() requestBody: CreateUserRequestBody
): Promise<void> {
const { uid, user } = requestBody
if (await new UserDataService().userDataExists(uid)) {
this.setStatus(409)
return
}
await new UserDataService().createUserData(uid, user)
this.setStatus(200)
}

@SuccessResponse("200", "Edited")
@Security("jwt", ["admin"])
@Post("bulk-edit")
@Patch("bulk-edit")
public async editUsers(
@Body() requestBody: EditUsersRequestBody
): Promise<void> {
Expand Down
5 changes: 5 additions & 0 deletions server/src/service-layer/request-models/UserRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export interface EditUsersRequestBody {
users: { uid: string; updatedInformation: UserAdditionalInfo }[]
}

export interface CreateUserRequestBody {
uid: string
user: UserAdditionalInfo
}

export interface SelfRequestModel {
user?: UserRecord
}

0 comments on commit f8964ac

Please sign in to comment.