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

USER09 - refactor the endpoint to get the list of users #250

Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dist/

# generated types
.astro/
coverage/

# dependencies
node_modules/
Expand Down
33 changes: 1 addition & 32 deletions src/data/firestore/common/get-docs-paginated.spec.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,14 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import {
type FirestoreDataConverter,
Timestamp,
} from "firebase-admin/firestore";
import { firestore as adminFirestore } from "firebase-admin";
import type {
PaginatedResponse,
PaginationParams,
} from "~/models/pagination/pagination.type";
import type { FirestoreDocument } from "~/data/model-document-mapper.ts";
import QueryDocumentSnapshot = adminFirestore.QueryDocumentSnapshot;
import getDocsPaginated from "./get-docs-paginated";
import { firestore } from "~/firebase/server";
import { testConverter, type TestModel } from "./test-model";

const testCollection = "get-paginated-test";

type TestModel = {
id?: string;
name: string;
lastUpdate: Date;
};

type TestDoc = FirestoreDocument<TestModel>;

const testConverter: FirestoreDataConverter<TestModel, TestDoc> = {
toFirestore: (company: TestModel): TestDoc => {
return {
...company,
lastUpdate: Timestamp.fromDate(company.lastUpdate),
};
},
fromFirestore: (snapshot: QueryDocumentSnapshot): TestModel => {
const data = snapshot.data();
return {
id: snapshot.id,
...data,
lastUpdate: data.lastUpdate.toDate(),
} as TestModel;
},
};

describe("Get docs paginated", () => {
const modelsToSave: TestModel[] = [
{
Expand Down
4 changes: 2 additions & 2 deletions src/data/firestore/common/get-docs-paginated.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
type DocumentData,
type FirestoreDataConverter,
getFirestore,
} from "firebase-admin/firestore";
import { firestore } from "~/firebase/server";
import type {
PaginatedResponse,
PaginationParams,
Expand Down Expand Up @@ -65,7 +65,7 @@ const getDocsPaginated = async <
},
};
}
const collectionRef = getFirestore().collection(collection);
const collectionRef = firestore.collection(collection);
let query = collectionRef
.withConverter(converter)
.orderBy(params.orderBy, params.orderDirection);
Expand Down
31 changes: 31 additions & 0 deletions src/data/firestore/common/test-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
QueryDocumentSnapshot,
Timestamp,
type FirestoreDataConverter,
} from "firebase-admin/firestore";
import type { FirestoreDocument } from "~/data/model-document-mapper";

export type TestModel = {
id?: string;
name: string;
lastUpdate: Date;
};

export type TestDoc = FirestoreDocument<TestModel>;

export const testConverter: FirestoreDataConverter<TestModel, TestDoc> = {
toFirestore: (company: TestModel): TestDoc => {
return {
...company,
lastUpdate: Timestamp.fromDate(company.lastUpdate),
};
},
fromFirestore: (snapshot: QueryDocumentSnapshot): TestModel => {
const data = snapshot.data();
return {
id: snapshot.id,
...data,
lastUpdate: data.lastUpdate.toDate(),
} as TestModel;
},
};
198 changes: 198 additions & 0 deletions src/data/firestore/user.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import type {
PaginatedResponse,
PaginationParams,
} from "~/models/pagination/pagination.type";
import { firestore } from "~/firebase/server";
import { testConverter } from "./common/test-model";
import type { User } from "~/models/user/user.type";
import { getUsersPaginated } from "./user";

const testCollection = "users" as const;

describe("Get docs paginated", () => {
const modelsToSave: User[] = [
{
displayName: "Name 1",
email: "[email protected]",
organizer: true,
},
{
displayName: "Name 2",
email: "[email protected]",
},
{
displayName: "Name 3",
email: "[email protected]",
organizer: true,
},
{
displayName: "Name 4",
email: "[email protected]",
},
{
displayName: "Name 5",
email: "[email protected]",
},
{
displayName: "Name 6",
email: "[email protected]",
},
{
displayName: "Name 7",
email: "[email protected]",
},
];

beforeEach(async () => {
const batch = firestore.batch();
modelsToSave.forEach((model) => {
batch.create(firestore.collection(testCollection).doc(), model);
});
await batch.commit();
});

afterEach(async () => {
const docs = await firestore
.collection(testCollection)
.withConverter(testConverter)
.listDocuments();
const batch = firestore.batch();
docs.forEach((doc) => {
batch.delete(doc);
});
await batch.commit();
});

test("should get first offset ordered", async () => {
const params: PaginationParams<User> = {
offset: 0,
limit: 2,
orderBy: "displayName",
orderDirection: "desc",
};

const result = await getUsersPaginated(params);

expect(result).toMatchObject({
status: "success",
data: {
data: expect.any(Array),
total: modelsToSave.length,
offset: 0,
limit: 2,
totalPages: Math.ceil(modelsToSave.length / params.limit),
},
});
const response = result.data as PaginatedResponse<User>;
expect(response.data).toHaveLength(params.limit);
});

test("should get second offset ordered", async () => {
const params: PaginationParams<User> = {
offset: 2,
limit: 2,
orderBy: "displayName",
orderDirection: "desc",
};

const result = await getUsersPaginated(params);

expect(result).toMatchObject({
status: "success",
data: {
data: expect.any(Array),
total: modelsToSave.length,
offset: 2,
limit: 2,
totalPages: Math.ceil(modelsToSave.length / params.limit),
},
});
const response = result.data as PaginatedResponse<User>;
expect(response.data).toHaveLength(params.limit);
});

test("should get last page ordered", async () => {
const params: PaginationParams<User> = {
offset: 6,
limit: 3,
orderBy: "displayName",
orderDirection: "desc",
};

const result = await getUsersPaginated(params);

expect(result).toMatchObject({
status: "success",
data: {
data: expect.any(Array),
total: modelsToSave.length,
offset: 6,
limit: 3,
totalPages: Math.ceil(modelsToSave.length / params.limit),
},
});
const response = result.data as PaginatedResponse<User>;
expect(response.data).toHaveLength(1);
expect(response.data[0].displayName).toEqual(modelsToSave[0].displayName);
});

// *********** Error cases *********** //

test("should return an error if offset is less than 1", async () => {
const params: PaginationParams<User> = {
offset: -1,
limit: 2,
orderBy: "displayName",
orderDirection: "asc",
};

const result = await getUsersPaginated(params);

expect(result).toMatchObject({
status: "error",
data: {
code: `${testCollection}/search-error:invalid-offset`,
message: `Offset must be a positive integer`,
},
});
});

test("should return an error if limit is less than 1", async () => {
const params: PaginationParams<User> = {
offset: 0,
limit: 0,
orderBy: "displayName",
orderDirection: "asc",
};

const result = await getUsersPaginated(params);

expect(result).toMatchObject({
status: "error",
data: {
code: `${testCollection}/search-error:invalid-limit`,
message: `Limit must be a positive integer`,
},
});
});

test("should return an error if offset is out of range", async () => {
const params: PaginationParams<User> = {
offset: 10,
limit: 2,
orderBy: "displayName",
orderDirection: "asc",
};

const result = await getUsersPaginated(params);

expect(result).toMatchObject({
status: "error",
data: {
code: `${testCollection}/search-error:invalid-offset`,
message: `Offset is greater than the total number of documents`,
},
});
});
});
31 changes: 31 additions & 0 deletions src/data/firestore/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { User } from "~/models/user/user.type";
import type { FirestoreDocument } from "../model-document-mapper";
import type { FirestoreDataConverter } from "firebase-admin/firestore";
import type { PaginationParams } from "~/models/pagination/pagination.type";
import getDocsPaginated from "./common/get-docs-paginated";

const collection = "users" as const;

type UserDoc = FirestoreDocument<User>;

const converter: FirestoreDataConverter<User, UserDoc> = {
toFirestore: (doc: User): UserDoc => {
delete doc.id;
return {
...doc,
};
},
fromFirestore: (snapshot: FirebaseFirestore.QueryDocumentSnapshot): User => {
const data = snapshot.data();
return {
id: snapshot.id,
...data,
} as User;
},
};

const getUsersPaginated = async (params: PaginationParams<User>) => {
return getDocsPaginated(collection, converter, params);
};

export { getUsersPaginated };
5 changes: 5 additions & 0 deletions src/models/base-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type BaseModel = {
id?: string;
};

export type { BaseModel };
5 changes: 3 additions & 2 deletions src/models/user/user.type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
type User = {
id?: string;
import type { BaseModel } from "../base-model";

type User = BaseModel & {
displayName: string;
email: string;
organizer?: boolean;
Expand Down
Loading