Skip to content

Commit

Permalink
Merge pull request #138 from letehaha/feat/extend-account-groups-endp…
Browse files Browse the repository at this point in the history
…oint

feat: Extend account groups retrieving endpoint
  • Loading branch information
letehaha authored Nov 15, 2024
2 parents c944f5c + ccd1c7e commit 4f807c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/controllers/account-groups/get-groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import * as accountGroupService from '@services/account-groups';
export const getAccountGroups = async (req, res: CustomResponse) => {
try {
const { id: userId } = req.user;
const { accountIds }: GetAccountGroupsParams['query'] = req.validated.query;
const { accountIds, hidden }: GetAccountGroupsParams['query'] = req.validated.query;

const groups = await accountGroupService.getAccountGroups({ userId, accountIds });
const groups = await accountGroupService.getAccountGroups({ userId, accountIds, hidden });

return res.status(200).json({
status: API_RESPONSE_STATUS.success,
Expand All @@ -22,7 +22,10 @@ export const getAccountGroups = async (req, res: CustomResponse) => {
};

export const getAccountGroupsSchema = z.object({
query: z.object({ accountIds: commaSeparatedRecordIds.optional() }),
query: z.object({
accountIds: commaSeparatedRecordIds.optional(),
hidden: z.coerce.boolean(),
}),
});

type GetAccountGroupsParams = z.infer<typeof getAccountGroupsSchema>;
16 changes: 14 additions & 2 deletions src/services/account-groups/get-account-groups.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
import AccountGroup from '@models/accounts-groups/AccountGroups.model';
import { withTransaction } from '../common';
import { Op } from 'sequelize';
import { Op, type WhereOptions } from 'sequelize';
import Accounts from '@models/Accounts.model';

export const getAccountGroups = withTransaction(
async ({
userId,
accountIds = [],
hidden = false,
}: {
userId: number;
accountIds?: number[];
hidden?: boolean;
}): Promise<AccountGroup[]> => {
const accountWhere: WhereOptions<Accounts> = {};

if (accountIds.length > 0) {
accountWhere.id = { [Op.in]: accountIds };
}

if (!hidden) {
accountWhere.isEnabled = true;
}

return AccountGroup.findAll({
where: { userId },
include: [
{ model: AccountGroup, as: 'childGroups' },
{
model: Accounts,
where: accountIds.length > 0 ? { id: { [Op.in]: accountIds } } : undefined,
where: accountWhere,
through: { attributes: [] },
required: accountIds.length > 0,
},
Expand Down

0 comments on commit 4f807c4

Please sign in to comment.