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

Feat: Add 'add/remove' exclude methods to categories #153

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
9 changes: 5 additions & 4 deletions src/controllers/user-settings/edit-exclude-categories.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { API_RESPONSE_STATUS } from 'shared-types';
import { z } from 'zod';
import { recordId } from '@common/lib/zod/custom-types';
import { editExcludedCategories } from '@services/user-settings/edit-excluded-categories';
import { errorHandler } from '@controllers/helpers';
import { CustomResponse } from '@common/types';
Expand All @@ -9,7 +10,7 @@ export const editExcludedCategoriesHandler = async (
res: CustomResponse
) => {
try {
const { addIds, removeIds } = editExcludedCategoriesSchema.parse(req.body);
const { addIds, removeIds } = req.validated;
const { user } = req;

const updatedCategories = await editExcludedCategories({
Expand All @@ -27,7 +28,7 @@ export const editExcludedCategoriesHandler = async (
}
};

const editExcludedCategoriesSchema = z.object({
addIds: z.array(z.number().int().positive().finite()).optional().default([]),
removeIds: z.array(z.number().int().positive().finite()).optional().default([]),
export const editExcludedCategoriesSchema = z.object({
addIds: z.array(recordId()).optional().default([]),
removeIds: z.array(recordId()).optional().default([]),
});
4 changes: 2 additions & 2 deletions src/routes/user.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { addUserCurrencies, addUserCurrenciesSchema } from '@controllers/currencies/add-user-currencies';
import { authenticateJwt } from '@middlewares/passport';
import { validateEndpoint } from '@middlewares/validations';
import { editExcludedCategoriesHandler } from '@controllers/user-settings/edit-exclude-categories';
import { editExcludedCategoriesHandler, editExcludedCategoriesSchema } from '@controllers/user-settings/edit-exclude-categories';

const router = Router({});

Expand Down Expand Up @@ -48,6 +48,6 @@ router.delete('/currency/rates', authenticateJwt, removeUserCurrencyExchangeRate

router.get('/settings', authenticateJwt, getUserSettings);
router.put('/settings', authenticateJwt, validateEndpoint(updateUserSettingsSchema), updateUserSettings);
router.put('/edit', authenticateJwt, editExcludedCategoriesHandler);
router.put('/edit-excluded-categories', authenticateJwt, validateEndpoint(editExcludedCategoriesSchema), editExcludedCategoriesHandler);
letehaha marked this conversation as resolved.
Show resolved Hide resolved

export default router;
12 changes: 2 additions & 10 deletions src/services/user-settings/edit-excluded-categories.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import UserSettings from '@models/UserSettings.model';
import UserSettings, { ZodSettingsSchema } from '@models/UserSettings.model';
import { withTransaction } from '../common';

export const editExcludedCategories = withTransaction(
Expand All @@ -13,15 +13,7 @@ export const editExcludedCategories = withTransaction(
}): Promise<number[]> => {
const [existingSettings] = await UserSettings.findOrCreate({
where: { userId },
defaults: {
settings: {
stats: {
expenses: {
excludedCategories: []
}
}
},
},
defaults: { ZodSettingsSchema },
});
letehaha marked this conversation as resolved.
Show resolved Hide resolved

const currentExcludedCategories =
Expand Down
Loading