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
34 changes: 34 additions & 0 deletions src/controllers/user-settings/edit-exclude-categories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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';

export const editExcludedCategoriesHandler = async (
req,
res: CustomResponse
) => {
try {
const { addIds, removeIds } = req.validated;
const { user } = req;

const updatedCategories = await editExcludedCategories({
userId: user.id,
addIds,
removeIds,
});

res.status(200).json({
status: API_RESPONSE_STATUS.success,
response: updatedCategories,
});
} catch (error) {
errorHandler(res,error);
}
};

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

const router = Router({});

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

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

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

export const editExcludedCategories = withTransaction(
async ({
userId,
addIds = [],
removeIds = [],
}: {
userId: number;
addIds?: number[];
removeIds?: number[];
}): Promise<number[]> => {
const [existingSettings] = await UserSettings.findOrCreate({
where: { userId },
defaults: { ZodSettingsSchema },
letehaha marked this conversation as resolved.
Show resolved Hide resolved
});

const currentExcludedCategories =
existingSettings.settings.stats.expenses.excludedCategories || [];

let updatedExcludedCategories = currentExcludedCategories.filter(
(id) => !removeIds.includes(id),
);

updatedExcludedCategories = [
...new Set([...updatedExcludedCategories, ...addIds]),
];

existingSettings.settings.stats.expenses.excludedCategories = updatedExcludedCategories;
existingSettings.changed('settings', true);
await existingSettings.save();

return updatedExcludedCategories;
},
);
Loading