-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
61 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,24 @@ | ||
import { z } from 'zod'; | ||
|
||
export const recordId = () => z.coerce.number().int().positive().finite(); | ||
|
||
/** | ||
* Used for the case when array is expected to be received like 1,2,3. | ||
* For example GET queries | ||
*/ | ||
export const commaSeparatedRecordIds = z.string().transform((str, ctx) => { | ||
const idSchema = recordId(); | ||
const ids = str.split(',').map((id) => { | ||
const result = idSchema.safeParse(id); | ||
return result.success ? result.data : null; | ||
}); | ||
|
||
if (ids.some((id) => id === null)) { | ||
ctx.addIssue({ | ||
code: z.ZodIssueCode.custom, | ||
message: 'Not all values are valid record IDs', | ||
}); | ||
return z.NEVER; | ||
} | ||
return ids as number[]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,27 @@ | ||
import AccountGroup from '@models/accounts-groups/AccountGroups.model'; | ||
import { withTransaction } from '../common'; | ||
import { Op } from 'sequelize'; | ||
import Accounts from '@models/Accounts.model'; | ||
|
||
export const getAccountGroups = withTransaction( | ||
async ({ userId }: { userId: number }): Promise<AccountGroup[]> => { | ||
async ({ | ||
userId, | ||
accountIds = [], | ||
}: { | ||
userId: number; | ||
accountIds?: number[]; | ||
}): Promise<AccountGroup[]> => { | ||
return AccountGroup.findAll({ | ||
where: { userId }, | ||
include: [{ model: AccountGroup, as: 'childGroups' }], | ||
include: [ | ||
{ model: AccountGroup, as: 'childGroups' }, | ||
{ | ||
model: Accounts, | ||
where: accountIds.length > 0 ? { id: { [Op.in]: accountIds } } : undefined, | ||
through: { attributes: [] }, | ||
required: accountIds.length > 0, | ||
}, | ||
], | ||
}); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters