Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

134 filter by fields #209

Merged
merged 6 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion server/src/data-layer/services/UserDataService.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { additionalInfoMock } from "test-config/mocks/User.mock"
import {
additionalInfoMock,
additionalInfoMockSecond
} from "test-config/mocks/User.mock"
import UserDataService from "./UserDataService"
import { cleanFirestore } from "test-config/TestUtils"

Expand Down Expand Up @@ -65,4 +68,41 @@ describe("UserService integration tests", () => {

expect(users.length).toEqual(2)
})

it("should filter users by membership type", async () => {
await userService.createUserData(TEST_UID_1, additionalInfoMock)
await userService.createUserData("testUser2", additionalInfoMock)
await userService.createUserData("testUser3", additionalInfoMockSecond)

const filteredMemberUsers = await userService.getFilteredUsers({
membership: "member"
})
expect(filteredMemberUsers.length).toEqual(2)
expect(filteredMemberUsers[0].membership).toEqual("member")
expect(filteredMemberUsers[1].membership).toEqual("member")

const filteredAdminUsers = await userService.getFilteredUsers({
membership: "admin"
})
expect(filteredAdminUsers.length).toEqual(1)
})

it("should filter users by first name", async () => {
await userService.createUserData(TEST_UID_1, additionalInfoMock)
await userService.createUserData("testUser2", additionalInfoMock)
await userService.createUserData("testUser3", additionalInfoMockSecond)

const filteredNameUsers = await userService.getFilteredUsers({
first_name: "first"
})
expect(filteredNameUsers.length).toEqual(2)
expect(filteredNameUsers[0].first_name).toEqual("first")
expect(filteredNameUsers[1].first_name).toEqual("first")

const filteredNameUser = await userService.getFilteredUsers({
first_name: "third"
})
expect(filteredNameUser.length).toEqual(1)
expect(filteredNameUser[0].first_name).toEqual("third")
})
})
30 changes: 29 additions & 1 deletion server/src/data-layer/services/UserDataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,36 @@ export default class UserDataService {
return snapshot.exists
}

/**
* Retrieves a list of filtered users based on the provided filters.
*
* @param filters - An object containing the filters to apply on the users.
* @returns filteredUsers - A promise that resolves to an array of filtered users.
*/
public async getFilteredUsers(filters: Partial<UserAdditionalInfo>) {
// TODO
let users: FirebaseFirestore.Query<
UserAdditionalInfo,
FirebaseFirestore.DocumentData
> = FirestoreCollections.users

/** Apply filters to the query based on the provided filter properties */
if (filters.membership) {
users = users.where("membership", "==", filters.membership)
} else if (filters.first_name) {
users = users.where("first_name", "==", filters.first_name)
} else if (filters.last_name) {
users = users.where("last_name", "==", filters.last_name)
}

/** Execute the query and retrieve the snapshot */
const snapshot = await users.get()

/** Map the snapshot documents to an array of filtered users */
const filteredUsers = snapshot.docs.map((user) => {
return { ...user.data(), uid: user.id }
})

return filteredUsers
}

// Update
Expand Down
20 changes: 20 additions & 0 deletions server/src/test-config/mocks/User.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,23 @@ export const additionalInfoMock: UserAdditionalInfo = {
returning: true,
university_year: "2nd"
}

export const additionalInfoMockSecond: UserAdditionalInfo = {
date_of_birth: dateToFirestoreTimeStamp(new Date(1000000000)),
does_freestyle: true,
does_racing: true,
does_ski: false,
gender: "non-ternary",
emergency_name: "emergency",
emergency_phone: "111",
emergency_relation: "relation",
first_name: "third",
last_name: "fourth",
membership: "admin",
dietary_requirements: "halal",
faculty: "science",
university: "uoa",
student_id: "125366427",
returning: true,
university_year: "3rd"
}
Loading