Skip to content

Commit

Permalink
Merge pull request #8 from RubenRaMar/feature/figureController
Browse files Browse the repository at this point in the history
Refactor getFigures controller queries
  • Loading branch information
RubenRaMar authored Dec 30, 2023
2 parents f759614 + 0d2c326 commit 90f30f4
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 69 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"no-unused-vars": "warn",
"no-console": "error",
"no-duplicate-imports": "error",
"no-implicit-coercion": "off",
"@typescript-eslint/consistent-type-definitions": [
"error",
"interface"
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Authorization: Bearer <token>
"title": "Figure 1",
"character": "Character 1",
"franchise": "Franchise 1",
"purchased": true,
"isPurchased": true,
"manufacturer": "Manufacturer 1",
"material": "Material 1",
"size": 10,
Expand All @@ -66,7 +66,7 @@ Authorization: Bearer <token>
"title": "Figure 2",
"character": "Character 2",
"franchise": "Franchise 2",
"purchased": false,
"isPurchased": false,
"manufacturer": "Manufacturer 2",
"material": "Material 2",
"size": 15,
Expand Down Expand Up @@ -99,7 +99,7 @@ Authorization: Bearer <token>
"title": "Jimei Palace Enel the god of thunder One Piece",
"character": "Enel",
"franchise": "One Piece",
"purchased": true,
"isPurchased": true,
"manufacturer": "Jimei Palace",
"material": "Resin + LED",
"size": 62,
Expand Down Expand Up @@ -151,7 +151,7 @@ Authorization: Bearer <token>
"title": "Updated Figure",
"character": "Updated Character",
"franchise": "Updated Franchise",
"purchased": true,
"isPurchased": true,
"manufacturer": "Updated Manufacturer",
"material": "Updated Material",
"size": 20,
Expand Down Expand Up @@ -188,7 +188,7 @@ Authorization: Bearer <token>
"title": "Dragon Ball Fat Buu BT Studio",
"character": "Fat Buu",
"franchise": "Dragon Ball",
"purchased": false,
"isPurchased": false,
"manufacturer": "BT Studio",
"material": "Resin",
"size": 30,
Expand All @@ -208,7 +208,7 @@ Authorization: Bearer <token>
"title": "Dragon Ball Fat Buu BT Studio",
"character": "Fat Buu",
"franchise": "Dragon Ball",
"purchased": false,
"isPurchased": false,
"manufacturer": "BT Studio",
"material": "Resin",
"size": 30,
Expand Down
2 changes: 1 addition & 1 deletion components.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- title: string;
- character: string;
- franchise: string;
- purchased: boolean;
- isPurchased: boolean;
- manufacturer: string;
- material: string;
- size: number;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"start": "node dist/",
"start:dev": "node --watch dist/",
"start:dev": "nodemon nodemon dist/",
"build": "tsc",
"build:dev": "tsc --watch",
"test:dev": "jest --watch",
Expand Down
4 changes: 2 additions & 2 deletions src/database/models/Figure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ const figureSchema = new Schema({
type: Number,
required: true,
},
user: {
userId: {
type: Types.ObjectId,
ref: "User",
required: true,
},
purchased: {
isPurchased: {
type: Boolean,
required: true,
},
Expand Down
30 changes: 15 additions & 15 deletions src/mocks/figures/figuresMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,82 @@ export const figuresMock: FigureIdData[] = [
title: "Goku SS4",
character: "Goku",
franchise: "Dragon Ball",
purchased: true,
isPurchased: true,
manufacturer: "Break Studio",
material: "Resin",
size: 31,
weight: 2.67,
price: 95,
image:
"https://www.kaionation.com/wp-content/uploads/2023/05/Image_20230506143056-681x1024.jpg",
user: new Types.ObjectId("6494430a47f8ed0069ec52ac"),
userId: new Types.ObjectId("6494430a47f8ed0069ec52ac"),
},
{
_id: new Types.ObjectId("a1b2c3d4e5f6a7b8c9d0e1f2"),
title: "Naruto 8thdays",
character: "Naruto",
franchise: "Naruto",
purchased: false,
isPurchased: false,
manufacturer: "Studio Kakashi Hatake",
material: "Resina",
size: 65,
weight: 5.23,
price: 245.99,
image: "https://www.kaionation.com/wp-content/uploads/2023/04/aa-43.jpg",
user: new Types.ObjectId("6494430a47f8ed0069ec52ac"),
userId: new Types.ObjectId("6494430a47f8ed0069ec52ac"),
},
{
_id: new Types.ObjectId("abcdef123456789012345678"),
title: "Nika Luffy",
character: "Luffy",
franchise: "One Piece",
purchased: true,
isPurchased: true,
manufacturer: "ATT Studio",
material: "Resina",
size: 41,
weight: 3.32,
price: 269.9,
image:
"https://www.kaionation.com/wp-content/uploads/2023/05/44-2-641x1024.webp",
user: new Types.ObjectId("6494430a47f8ed0069ec52ac"),
userId: new Types.ObjectId("6494430a47f8ed0069ec52ac"),
},
{
_id: new Types.ObjectId("0123456789abcdefabcdef12"),
title: "Metal Cooler",
character: "Cooler",
franchise: "Dragon Ball",
purchased: true,
isPurchased: true,
manufacturer: "S.H. Figuarts Tamashii Nations Bandai Spirits",
material: "ABS",
size: 14.5,
weight: 0.6,
price: 99.9,
image:
"https://www.global-freaks.com/196309-pdt_540/dragon-ball-z-metal-cooler-sh-figuarts-tamashii-nations-bandai-spirits.jpg",
user: new Types.ObjectId("6494430a47f8ed0069ec52ac"),
userId: new Types.ObjectId("6494430a47f8ed0069ec52ac"),
},
{
_id: new Types.ObjectId("f0e1d2c3b4a5968778695a4b"),
title: "Enel the god of thunder",
character: "Enel",
franchise: "One Piece",
purchased: true,
isPurchased: true,
manufacturer: "Jimei Palace",
material: "Resin + LED",
size: 62,
weight: 9.87,
price: 1_599,
image:
"https://kurogami.com/med/img/productos/37/70/FIGURA_SABER_ALTER_FATE_STAY_NIGHT_HEAVENS_FEEL_POP_UP_PARADE_1.webp",
user: new Types.ObjectId("6494430a47f8ed0069ec52ac"),
userId: new Types.ObjectId("6494430a47f8ed0069ec52ac"),
},
];

export const requestFiguresMock: RequestFigureData = {
title: "Goku SS4",
character: "Goku",
franchise: "Dragon Ball",
purchased: true,
isPurchased: true,
manufacturer: "Break Studio",
material: "Resin",
size: 31,
Expand All @@ -101,29 +101,29 @@ export const requestFiguresUdgrateMock: RequestUpdateFigureStructure = {
title: "Goku SS4",
character: "Goku",
franchise: "Dragon Ball",
purchased: true,
isPurchased: true,
manufacturer: "Break Studio",
material: "Resin",
size: 31,
weight: 2.67,
price: 95,
image:
"https://www.kaionation.com/wp-content/uploads/2023/05/Image_20230506143056-681x1024.jpg",
user: "646fc50910c8e8c5b17d54a7",
userId: "646fc50910c8e8c5b17d54a7",
};

export const badRequestFiguresUdgrateMock: RequestUpdateFigureStructure = {
id: "5f234c24c2a7753123456789",
title: "Goku SS4",
character: "Goku",
franchise: "Dragon Ball",
purchased: true,
isPurchased: true,
manufacturer: "Break Studio",
material: "Resin",
size: 31,
weight: 2.67,
price: 95,
image:
"https://www.kaionation.com/wp-content/uploads/2023/05/Image_20230506143056-681x1024.jpg",
user: "646fc50910c8e8c5b17d54a7",
userId: "646fc50910c8e8c5b17d54a7",
};
10 changes: 6 additions & 4 deletions src/mocks/user/userMoks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ export const userCredentialsMock: UserCredentials = {
password: "Gines123",
};

export const userCredientialsHashMock = {
export const userCredientialsHashMock: UserCredentials = {
...userCredentialsMock,
password: "$2y$10$D65bZwWfxmMyif9KFITP6./83OwYCwS4claM4rGXtcG0ZW43xlamy",
};

export const userErrorCredientialsMock = {
export const userErrorCredientialsMock: UserCredentials = {
...userCredentialsMock,
password: "",
};

export const userIdMock = "6494430a47f8ed0069ec52ac";

const tokenPayload: JwtPayload = {
sub: "6494430a47f8ed0069ec52ac",
name: "Gines",
sub: userIdMock,
name: userCredentialsMock.username,
};

export const tokenMock = jwt.sign(tokenPayload, process.env.JWT_SECRET!, {
Expand Down
4 changes: 2 additions & 2 deletions src/schema/figureShema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const figureShema = {
price: Joi.number().required(),
size: Joi.number().required(),
weight: Joi.number().required(),
purchased: Joi.boolean().required(),
isPurchased: Joi.boolean().required(),
id: Joi.string(),
user: Joi.string(),
userId: Joi.string(),
}),
};

Expand Down
8 changes: 4 additions & 4 deletions src/server/controllers/figure/__test__/getFigures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ beforeEach(() => {

describe("Given a getFigures middleware", () => {
const userId = "1a2b3c4d5e6f7a8b9c0d1e2f";
const query = {
skip: "1",
const query: CustomRequest["query"] = {
page: "1",
limit: "10",
filter: "true",
purchased: "true",
};

const req: CustomRequestBody = {
Expand All @@ -33,7 +33,7 @@ describe("Given a getFigures middleware", () => {
const next = jest.fn();

const figuresWithUserId = figuresMock.filter(
(figure) => figure.user.toString() === userId
(figure) => figure.userId.toString() === userId
);

describe("When it invoked with a user id", () => {
Expand Down
33 changes: 22 additions & 11 deletions src/server/controllers/figure/figureController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
privateMessageList,
} from "../../utils/responseData/responseData.js";
import CustomError from "../../Classes/CustomError/CustomError.js";
import { type RequestUpdateFigureStructure } from "../../../types.js";

export const getFigures = async (
req: CustomRequest,
Expand All @@ -15,30 +16,40 @@ export const getFigures = async (
) => {
const {
userId,
query: { limit, skip, filter },
query: { limit, page, purchased },
} = req;

const newLimit = Number(limit);
const newSkip = Number(skip);
const defaultLimit = 12;
const defaultpage = 0;

let figureQuery = {};
const newLimit = limit ? +limit : defaultLimit;
const newSkip = page ? +page * newLimit : defaultpage;
const isPurchased = purchased === "true";

type FigureQueryOptions = {
[key in keyof RequestUpdateFigureStructure]?: RequestUpdateFigureStructure[key];
};

let figureQueryOptions: FigureQueryOptions = {};

if (userId) {
figureQuery = { user: userId };
figureQueryOptions = { ...figureQueryOptions, userId };
}

if (filter === "true") {
figureQuery = { ...figureQuery, purchased: "false" };
if (purchased) {
figureQueryOptions = { ...figureQueryOptions, isPurchased };
}

try {
const figures = await Figure.find(figureQuery)
const figures = await Figure.find(figureQueryOptions)
.sort({ _id: -1 })
.skip(newSkip)
.limit(newLimit)
.exec();

const length = await Figure.where(figureQuery).countDocuments().exec();
const length = await Figure.where(figureQueryOptions)
.countDocuments()
.exec();

res.status(statusCodeList.ok).json({ figures, length });
} catch (error: unknown) {
Expand Down Expand Up @@ -102,7 +113,7 @@ export const addFigure = async (

const addedFigure = await Figure.create({
...body,
user: new Types.ObjectId(userId),
userId: new Types.ObjectId(userId),
});

if (!addedFigure) {
Expand All @@ -128,7 +139,7 @@ export const updateFigure = async (

const updatedFigure = await Figure.findByIdAndUpdate(body.id, {
...body,
user: new Types.ObjectId(userId),
userId: new Types.ObjectId(userId),
_id: new Types.ObjectId(body.id),
}).exec();

Expand Down
6 changes: 3 additions & 3 deletions src/server/controllers/figure/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export interface CustomRequest extends Request {
figureId: string;
};
query: {
skip: string;
limit: string;
filter: string;
page?: string;
limit?: string;
purchased?: string;
};
body: RequestFigureData;
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/controllers/user/__test__/registerUser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe("Given a registerUser controller", () => {
privateMessageList.registerError
);

User.create = jest.fn().mockRejectedValue(expectedError);
User.create = jest.fn().mockReturnValue(undefined);

await registerUser(
req as UserCredentialsRequest,
Expand Down
Loading

0 comments on commit 90f30f4

Please sign in to comment.