Skip to content

Commit

Permalink
[RSN-86] Make frontend schemas compliant with backend requests/respon…
Browse files Browse the repository at this point in the history
…ses (#75)

* refactor: make schemas compliant with backend models

* refactor: add appropriate return types in services
  • Loading branch information
raczu authored Jun 23, 2024
1 parent 2b7ad42 commit e51b93c
Show file tree
Hide file tree
Showing 21 changed files with 229 additions and 852 deletions.
7 changes: 5 additions & 2 deletions Client/reasn-client/apps/web/services/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from "@reasn/common/src/schemas/EventResponse";
import { ParameterDto } from "@reasn/common/src/schemas/ParameterDto";
import { TagDto } from "@reasn/common/src/schemas/TagDto";
import { CommentDto } from "@reasn/common/src/schemas/CommentDto";

const baseUrl = `${process.env.REASN_API_URL}/api/v1/events`;

Expand Down Expand Up @@ -129,10 +130,12 @@ export const getEventParticipants = async (slug: string): Promise<any> => {
return response;
};

export const getEventComments = async (slug: string): Promise<any> => {
export const getEventComments = async (slug: string): Promise<CommentDto[]> => {
const url = new URL(`${baseUrl}/${slug}/comments`);

const response = await sendRequest<any>(url, { method: HttpMethod.GET });
const response = await sendRequest<CommentDto[]>(url, {
method: HttpMethod.GET,
});
return response;
};

Expand Down
7 changes: 5 additions & 2 deletions Client/reasn-client/apps/web/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ParticipantDto,
ParticipantDtoMapper,
} from "@reasn/common/src/schemas/ParticipantDto";
import { InterestDto } from "@reasn/common/src/schemas/InterestDto";

const baseUrl = `${process.env.REASN_API_URL}/api/v1`;
const baseUsersUrl = `${baseUrl}/users`;
Expand Down Expand Up @@ -45,11 +46,13 @@ export const updateUser = async (

export const getUsersInterests = async (
params: Record<string, string> = {},
): Promise<any> => {
): Promise<InterestDto[]> => {
const url = new URL(`${baseUsersUrl}/interests`);
url.search = new URLSearchParams(params).toString();

const response = await sendRequest<any>(url, { method: HttpMethod.GET });
const response = await sendRequest<InterestDto[]>(url, {
method: HttpMethod.GET,
});
return response;
};

Expand Down
193 changes: 116 additions & 77 deletions Client/reasn-client/packages/common/__tests__/schemas/CommentDto.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,73 @@ import {
} from "@reasn/common/src/schemas/CommentDto";

describe("CommentDto", () => {
const eventId = 1;
const eventSlug = "event-slug";
const content = "Test Content";
const createdAt = new Date();
const userId = 2;
const username = "username";
const userImageUrl = "url";

describe("fromJson", () => {
it("should create an instance of CommentDto from JSON string", () => {
const json = `{
"EventId": ${eventId},
"Content": "${content}",
"CreatedAt": "${createdAt.toISOString()}",
"UserId": ${userId}
"eventSlug": "${eventSlug}",
"content": "${content}",
"createdAt": "${createdAt.toISOString()}",
"username": "${username}",
"userImageUrl": "${userImageUrl}"
}`;

let comment = CommentDtoMapper.fromJSON(json);
comment = comment as CommentDto;

expect(comment.EventId).toBe(eventId);
expect(comment.Content).toBe(content);
expect(comment.CreatedAt).toEqual(createdAt);
expect(comment.UserId).toBe(userId);
expect(comment.eventSlug).toBe(eventSlug);
expect(comment.content).toBe(content);
expect(comment.createdAt).toEqual(createdAt);
expect(comment.username).toBe(username);
expect(comment.userImageUrl).toBe(userImageUrl);
});

it("should throw an error if the JSON string is empty", () => {
expect(() => CommentDtoMapper.fromJSON("")).toThrow(ModelMappingError);
});

it("should throw an error when providing JSON without each property individually", () => {
const jsonWithoutEventId = `{
"Content": "${content}",
"CreatedAt": "${createdAt.toISOString()}",
"UserId": ${userId}
const jsonWithoutEventSlug = `{
"content": "${content}",
"createdAt": "${createdAt.toISOString()}",
"username": "${username}",
"userImageUrl": "${userImageUrl}"
}`;

const jsonWithoutContent = `{
"EventId": ${eventId},
"CreatedAt": "${createdAt.toISOString()}",
"UserId": ${userId}
"eventSlug": "${eventSlug}",
"createdAt": "${createdAt.toISOString()}",
"username": "${username}",
"userImageUrl": "${userImageUrl}"
}`;

const jsonWithoutCreatedAt = `{
"EventId": ${eventId},
"Content": "${content}",
"UserId": ${userId}
"eventSlug": "${eventSlug}",
"content": "${content}",
"username": "${username}",
"userImageUrl": "${userImageUrl}"
}`;

const jsonWithoutUserId = `{
"EventId": ${eventId},
"Content": "${content}",
"CreatedAt": "${createdAt.toISOString()}"
const jsonWithoutUsername = `{
"eventSlug": "${eventSlug}",
"content": "${content}",
"createdAt": "${createdAt.toISOString()}",
"userImageUrl": "${userImageUrl}"
}`;

expect(() => CommentDtoMapper.fromJSON(jsonWithoutEventId)).toThrow(
const jsonWithoutUserImageUrl = `{
"eventSlug": "${eventSlug}",
"content": "${content}",
"createdAt": "${createdAt.toISOString()}",
"username": "${username}"
}`;

expect(() => CommentDtoMapper.fromJSON(jsonWithoutEventSlug)).toThrow(
ModelMappingError,
);
expect(() => CommentDtoMapper.fromJSON(jsonWithoutContent)).toThrow(
Expand All @@ -66,7 +80,10 @@ describe("CommentDto", () => {
expect(() => CommentDtoMapper.fromJSON(jsonWithoutCreatedAt)).toThrow(
ModelMappingError,
);
expect(() => CommentDtoMapper.fromJSON(jsonWithoutUserId)).toThrow(
expect(() => CommentDtoMapper.fromJSON(jsonWithoutUsername)).toThrow(
ModelMappingError,
);
expect(() => CommentDtoMapper.fromJSON(jsonWithoutUserImageUrl)).toThrow(
ModelMappingError,
);
});
Expand All @@ -75,57 +92,71 @@ describe("CommentDto", () => {
describe("fromObject", () => {
it("should create an instance of CommentDto from an object", () => {
const object = {
EventId: eventId,
Content: content,
CreatedAt: createdAt,
UserId: userId,
eventSlug: eventSlug,
content: content,
createdAt: createdAt,
username: username,
userImageUrl: userImageUrl,
};

let comment = CommentDtoMapper.fromObject(object);
comment = comment as CommentDto;

expect(comment.EventId).toBe(eventId);
expect(comment.Content).toBe(content);
expect(comment.CreatedAt).toEqual(createdAt);
expect(comment.UserId).toBe(userId);
expect(comment.eventSlug).toBe(eventSlug);
expect(comment.content).toBe(content);
expect(comment.createdAt).toEqual(createdAt);
expect(comment.username).toBe(username);
expect(comment.userImageUrl).toBe(userImageUrl);
});

it("should throw an error if the object is invalid", () => {
const object = {
EventId: eventId,
Content: true,
CreatedAt: createdAt,
UserId: null,
eventSlug: eventSlug,
content: true,
createdAt: createdAt,
username: null,
userImageUrl: userImageUrl,
};

const objectWithoutEventId = {
Content: content,
CreatedAt: createdAt,
UserId: userId,
const objectWithoutEventSlug = {
content: content,
createdAt: createdAt,
username: username,
userImageUrl: userImageUrl,
};

const objectWithoutContent = {
EventId: eventId,
CreatedAt: createdAt,
UserId: userId,
eventSlug: eventSlug,
createdAt: createdAt,
username: username,
userImageUrl: userImageUrl,
};

const objectWithoutCreatedAt = {
EventId: eventId,
Content: content,
UserId: userId,
eventSlug: eventSlug,
content: content,
username: username,
userImageUrl: userImageUrl,
};

const objectWithoutUsername = {
eventSlug: eventSlug,
content: content,
createdAt: createdAt,
userImageUrl: userImageUrl,
};

const objectWithoutUserId = {
EventId: eventId,
Content: content,
CreatedAt: createdAt,
const objectWithoutUserImageUrl = {
eventSlug: eventSlug,
content: content,
createdAt: createdAt,
username: username,
};

expect(() => CommentDtoMapper.fromObject(object)).toThrow(
ModelMappingError,
);
expect(() => CommentDtoMapper.fromObject(objectWithoutEventId)).toThrow(
expect(() => CommentDtoMapper.fromObject(objectWithoutEventSlug)).toThrow(
ModelMappingError,
);
expect(() => CommentDtoMapper.fromObject(objectWithoutContent)).toThrow(
Expand All @@ -134,24 +165,29 @@ describe("CommentDto", () => {
expect(() => CommentDtoMapper.fromObject(objectWithoutCreatedAt)).toThrow(
ModelMappingError,
);
expect(() => CommentDtoMapper.fromObject(objectWithoutUserId)).toThrow(
expect(() => CommentDtoMapper.fromObject(objectWithoutUsername)).toThrow(
ModelMappingError,
);
expect(() =>
CommentDtoMapper.fromObject(objectWithoutUserImageUrl),
).toThrow(ModelMappingError);
});

it("should throw an error if date is in incorrect format", () => {
const object = {
EventId: eventId,
Content: content,
CreatedAt: "invalid date",
UserId: userId,
eventSlug: eventSlug,
content: content,
createdAt: "invalid date",
username: username,
userImageUrl: userImageUrl,
};

const objectMissingZ = {
EventId: eventId,
Content: content,
CreatedAt: "2009-06-15T13:45:30.0000000",
UserId: userId,
eventSlug: eventSlug,
content: content,
createdAt: "2009-06-15T13:45:30.0000000",
username: username,
userImageUrl: userImageUrl,
};

expect(() => CommentDtoMapper.fromObject(object)).toThrow(
Expand All @@ -164,44 +200,47 @@ describe("CommentDto", () => {

it("should properly parse date string", () => {
const object = {
EventId: eventId,
Content: content,
CreatedAt: "2009-06-15T13:45:30.0000000-07:00",
UserId: userId,
eventSlug: eventSlug,
content: content,
createdAt: "2009-06-15T13:45:30.0000000-07:00",
username: username,
userImageUrl: userImageUrl,
};

const objectWithoutOffset = {
EventId: eventId,
Content: content,
CreatedAt: "2009-06-15T13:45:30.0000000Z",
UserId: userId,
eventSlug: eventSlug,
content: content,
createdAt: "2009-06-15T13:45:30.0000000Z",
username: username,
userImageUrl: userImageUrl,
};

const objectWithoutMilliseconds = {
EventId: eventId,
Content: content,
CreatedAt: "2009-06-15T13:45:30Z",
UserId: userId,
eventSlug: eventSlug,
content: content,
createdAt: "2009-06-15T13:45:30Z",
username: username,
userImageUrl: userImageUrl,
};

let comment = CommentDtoMapper.fromObject(object);
comment = comment as CommentDto;

expect(comment.CreatedAt).toEqual(
expect(comment.createdAt).toEqual(
new Date("2009-06-15T13:45:30.0000000-07:00"),
);

comment = CommentDtoMapper.fromObject(objectWithoutOffset);
comment = comment as CommentDto;

expect(comment.CreatedAt).toEqual(
expect(comment.createdAt).toEqual(
new Date("2009-06-15T13:45:30.0000000Z"),
);

comment = CommentDtoMapper.fromObject(objectWithoutMilliseconds);
comment = comment as CommentDto;

expect(comment.CreatedAt).toEqual(new Date("2009-06-15T13:45:30Z"));
expect(comment.createdAt).toEqual(new Date("2009-06-15T13:45:30Z"));
});
});
});
Loading

0 comments on commit e51b93c

Please sign in to comment.