Skip to content

Commit

Permalink
Rework mock api internal data storage
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Oct 19, 2023
1 parent 2ae69e4 commit 4ffa9b3
Show file tree
Hide file tree
Showing 19 changed files with 263 additions and 180 deletions.
10 changes: 10 additions & 0 deletions src/frontend/tests/mockApi/categories/categories.data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"id": 1,
"name": "Bakery"
},
{
"id": 2,
"name": "Meat"
}
]
20 changes: 14 additions & 6 deletions src/frontend/tests/mockApi/categories/categories.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ export const getAll = () => db.category.getAll();
export const getProductsCount = (categoryId: number) =>
db.product.count({
where: {
category: {
id: { equals: categoryId },
},
categoryId: { equals: categoryId },
},
});

Expand Down Expand Up @@ -39,11 +37,21 @@ export const update = (id: number, body: CategoryFormData) => {
};

export const deleteOne = (id: number) => {
const products = db.product.findMany({
where: {
categoryId: { equals: id },
},
});

db.note.deleteMany({
where: {
productId: { in: products.map(p => p.id) },
},
});

db.product.deleteMany({
where: {
category: {
id: { equals: id },
},
categoryId: { equals: id },
},
});

Expand Down
9 changes: 9 additions & 0 deletions src/frontend/tests/mockApi/categories/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
import { db } from '../db';
import data from './categories.data.json';

export { handlers as categoriesHandlers } from './categories.handlers';
export * as categoriesService from './categories.service';

export const fillCategories = (): void => {
data.forEach(category => {
db.category.create(category);
});
};
86 changes: 0 additions & 86 deletions src/frontend/tests/mockApi/data.json

This file was deleted.

9 changes: 5 additions & 4 deletions src/frontend/tests/mockApi/db.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { factory, manyOf, oneOf, primaryKey } from '@mswjs/data';
import { factory, primaryKey } from '@mswjs/data';

export const db = factory({
page: {
id: primaryKey(Number),
date: String,
notes: manyOf('note'),
},

note: {
Expand All @@ -13,7 +12,7 @@ export const db = factory({
displayOrder: Number,
quantity: Number,
pageId: Number,
product: oneOf('product'),
productId: Number,
},

category: {
Expand All @@ -25,10 +24,12 @@ export const db = factory({
id: primaryKey(Number),
name: String,
caloriesCost: Number,
category: oneOf('category'),
categoryId: Number,
},
});

export type Db = typeof db;

export type DbPage = NonNullable<ReturnType<Db['page']['findFirst']>>;
export type DbNote = NonNullable<ReturnType<Db['note']['findFirst']>>;
export type DbProduct = NonNullable<ReturnType<Db['product']['findFirst']>>;
12 changes: 11 additions & 1 deletion src/frontend/tests/mockApi/initBrowserMockApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { initDb } from './initDb';
import { fillCategories } from './categories';
import { fillNotes } from './notes';
import { fillPages } from './pages';
import { fillProducts } from './products';

const initDb = () => {
fillPages();
fillNotes();
fillProducts();
fillCategories();
};

const IGNORED_HOSTNAMES = ['apis.google.com', 'fonts.gstatic.com'];
const IGNORED_PATHNAMES = ['manifest.json', 'favicon.*', 'main.*.hot-update.js'];
Expand Down
52 changes: 0 additions & 52 deletions src/frontend/tests/mockApi/initDb.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/frontend/tests/mockApi/notes/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
import { db } from '../db';
import data from './notes.data.json';

export { handlers as notesHandlers } from './notes.handlers';
export * as notesService from './notes.service';

export const fillNotes = (): void => {
data.forEach(note => {
db.note.create(note);
});
};
34 changes: 34 additions & 0 deletions src/frontend/tests/mockApi/notes/notes.data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[
{
"id": 1,
"mealType": 1,
"displayOrder": 1,
"quantity": 200,
"productId": 1,
"pageId": 1
},
{
"id": 2,
"mealType": 1,
"displayOrder": 0,
"quantity": 150,
"productId": 2,
"pageId": 1
},
{
"id": 10,
"mealType": 1,
"displayOrder": 0,
"quantity": 100,
"productId": 1,
"pageId": 2
},
{
"id": 20,
"mealType": 1,
"displayOrder": 0,
"quantity": 100,
"productId": 1,
"pageId": 3
}
]
25 changes: 15 additions & 10 deletions src/frontend/tests/mockApi/notes/notes.handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ import { notesService } from '.';
export const handlers: RestHandler[] = [
rest.get(`${API_URL}/api/v1/notes`, (req, res, ctx) => {
const pageId = parseInt(req.url.searchParams.get('pageId') ?? '0');
const dbNotes = notesService.getByPageId(pageId);
const notes = notesService.getByPageId(pageId);
const productsMap = notesService.getProducts(notes);

const response = dbNotes.map(({ id, mealType, displayOrder, product, quantity }) => ({
id,
mealType,
displayOrder,
productId: product?.id,
productName: product?.name,
productQuantity: quantity,
calories: product ? (quantity * product.caloriesCost) / 100 : 0,
}));
const response = notes.map(({ id, mealType, displayOrder, productId, quantity }) => {
const product = productsMap.get(productId);

return {
id,
mealType,
displayOrder,
productId: product?.id,
productName: product?.name,
productQuantity: quantity,
calories: product ? (quantity * product.caloriesCost) / 100 : 0,
};
});

return res(ctx.json(response));
}),
Expand Down
24 changes: 23 additions & 1 deletion src/frontend/tests/mockApi/notes/notes.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { db } from '../db';
import { db, DbNote, DbProduct } from '../db';

export const getByPageId = (pageId: number) =>
db.note.findMany({
Expand All @@ -9,3 +9,25 @@ export const getByPageId = (pageId: number) =>
displayOrder: 'asc',
},
});

export const getProducts = (notes: DbNote[]): Map<number, DbProduct> => {
return notes
.map(n => n.productId)
.reduce((map, id) => {
if (map.has(id)) {
return map;
}

const product = db.product.findFirst({
where: {
id: { equals: id },
},
});

if (product) {
map.set(id, product);
}

return map;
}, new Map<number, DbProduct>());
};
9 changes: 9 additions & 0 deletions src/frontend/tests/mockApi/pages/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
import { db } from '../db';
import data from './pages.data.json';

export { handlers as pagesHandlers } from './pages.handlers';
export * as pagesService from './pages.service';

export const fillPages = (): void => {
data.forEach(page => {
db.page.create(page);
});
};
14 changes: 14 additions & 0 deletions src/frontend/tests/mockApi/pages/pages.data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"id": 1,
"date": "2022-01-01"
},
{
"id": 2,
"date": "2022-01-02"
},
{
"id": 3,
"date": "2022-01-03"
}
]
Loading

0 comments on commit 4ffa9b3

Please sign in to comment.