-
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.
Merge pull request #137 from letehaha/feat/accounts-grouping
feat: Add accounts grouping
- Loading branch information
Showing
35 changed files
with
1,177 additions
and
2 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
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 |
---|---|---|
@@ -0,0 +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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { z } from 'zod'; | ||
import type { CustomResponse } from '@common/types'; | ||
import { API_RESPONSE_STATUS } from 'shared-types'; | ||
import { recordId } from '@common/lib/zod/custom-types'; | ||
import { errorHandler } from '@controllers/helpers'; | ||
import * as accountGroupService from '@services/account-groups'; | ||
|
||
export const addAccountToGroup = async (req, res: CustomResponse) => { | ||
try { | ||
const { accountId, groupId }: AddAccountToGroupParams['params'] = req.validated.params; | ||
|
||
const grouping = await accountGroupService.addAccountToGroup({ accountId, groupId }); | ||
|
||
return res.status(201).json({ | ||
status: API_RESPONSE_STATUS.success, | ||
response: grouping, | ||
}); | ||
} catch (err) { | ||
errorHandler(res, err); | ||
} | ||
}; | ||
|
||
export const addAccountToGroupSchema = z.object({ | ||
params: z.object({ | ||
accountId: recordId(), | ||
groupId: recordId(), | ||
}), | ||
}); | ||
|
||
type AddAccountToGroupParams = z.infer<typeof addAccountToGroupSchema>; |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { z } from 'zod'; | ||
import type { CustomResponse } from '@common/types'; | ||
import { API_RESPONSE_STATUS } from 'shared-types'; | ||
import { recordId } from '@common/lib/zod/custom-types'; | ||
import { errorHandler } from '@controllers/helpers'; | ||
import * as accountGroupService from '@services/account-groups'; | ||
|
||
export const createAccountGroup = async (req, res: CustomResponse) => { | ||
try { | ||
const { id: userId } = req.user; | ||
const { name, parentGroupId }: CreateAccountGroupParams['body'] = req.validated.body; | ||
|
||
const group = await accountGroupService.createAccountGroup({ userId, name, parentGroupId }); | ||
|
||
return res.status(201).json({ | ||
status: API_RESPONSE_STATUS.success, | ||
response: group, | ||
}); | ||
} catch (err) { | ||
errorHandler(res, err); | ||
} | ||
}; | ||
|
||
export const createAccountGroupSchema = z.object({ | ||
body: z | ||
.object({ | ||
name: z.string().min(1), | ||
parentGroupId: recordId().nullable().optional(), | ||
}) | ||
.strict(), | ||
}); | ||
|
||
type CreateAccountGroupParams = z.infer<typeof createAccountGroupSchema>; |
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { z } from 'zod'; | ||
import type { CustomResponse } from '@common/types'; | ||
import { recordId } from '@common/lib/zod/custom-types'; | ||
import { errorHandler } from '@controllers/helpers'; | ||
import * as accountGroupService from '@services/account-groups'; | ||
|
||
export const deleteAccountGroup = async (req, res: CustomResponse) => { | ||
try { | ||
const { id: userId } = req.user; | ||
const { groupId }: DeleteAccountGroupParams['params'] = req.validated.params; | ||
|
||
await accountGroupService.deleteAccountGroup({ | ||
groupId, | ||
userId, | ||
}); | ||
|
||
return res.status(204).send(); | ||
} catch (err) { | ||
errorHandler(res, err); | ||
} | ||
}; | ||
|
||
export const deleteAccountGroupSchema = z.object({ | ||
params: z.object({ groupId: recordId() }), | ||
}); | ||
|
||
type DeleteAccountGroupParams = z.infer<typeof deleteAccountGroupSchema>; |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { z } from 'zod'; | ||
import type { CustomResponse } from '@common/types'; | ||
import { API_RESPONSE_STATUS } from 'shared-types'; | ||
import { recordId } from '@common/lib/zod/custom-types'; | ||
import { errorHandler } from '@controllers/helpers'; | ||
import * as accountGroupService from '@services/account-groups'; | ||
|
||
export const getAccountsInGroup = async (req, res: CustomResponse) => { | ||
try { | ||
const { groupId }: GetAccountsInGroupParams['params'] = req.validated.params; | ||
|
||
const accounts = await accountGroupService.getAccountsInGroup({ | ||
groupId, | ||
}); | ||
|
||
return res.status(200).json({ | ||
status: API_RESPONSE_STATUS.success, | ||
response: accounts, | ||
}); | ||
} catch (err) { | ||
errorHandler(res, err); | ||
} | ||
}; | ||
|
||
export const getAccountsInGroupSchema = z.object({ | ||
params: z.object({ groupId: recordId() }), | ||
}); | ||
|
||
type GetAccountsInGroupParams = z.infer<typeof getAccountsInGroupSchema>; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { z } from 'zod'; | ||
import type { CustomResponse } from '@common/types'; | ||
import { commaSeparatedRecordIds } from '@common/lib/zod/custom-types'; | ||
import { API_RESPONSE_STATUS } from 'shared-types'; | ||
import { errorHandler } from '@controllers/helpers'; | ||
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 groups = await accountGroupService.getAccountGroups({ userId, accountIds }); | ||
|
||
return res.status(200).json({ | ||
status: API_RESPONSE_STATUS.success, | ||
response: groups, | ||
}); | ||
} catch (err) { | ||
errorHandler(res, err); | ||
} | ||
}; | ||
|
||
export const getAccountGroupsSchema = z.object({ | ||
query: z.object({ accountIds: commaSeparatedRecordIds.optional() }), | ||
}); | ||
|
||
type GetAccountGroupsParams = z.infer<typeof getAccountGroupsSchema>; |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export * from './add-account-to-group'; | ||
export * from './create-account-group'; | ||
export * from './delete-group'; | ||
export * from './get-accounts-in-group'; | ||
export * from './get-groups'; | ||
export * from './move-account-to-group'; | ||
export * from './remove-account-from-group'; | ||
export * from './update-group'; |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { z } from 'zod'; | ||
import type { CustomResponse } from '@common/types'; | ||
import { API_RESPONSE_STATUS } from 'shared-types'; | ||
import { recordId } from '@common/lib/zod/custom-types'; | ||
import { errorHandler } from '@controllers/helpers'; | ||
import * as accountGroupService from '@services/account-groups'; | ||
|
||
export const moveAccountGroup = async (req, res: CustomResponse) => { | ||
try { | ||
const { id: userId } = req.user; | ||
const { groupId }: MoveAccountGroupParams['params'] = req.validated.params; | ||
const { newParentGroupId }: MoveAccountGroupParams['body'] = req.validated.body; | ||
|
||
const [updatedCount, updatedGroups] = await accountGroupService.moveAccountGroup({ | ||
groupId, | ||
newParentGroupId, | ||
userId, | ||
}); | ||
|
||
if (updatedCount === 0) { | ||
return res.status(404).json({ status: API_RESPONSE_STATUS.error }); | ||
} | ||
|
||
return res.status(200).json({ | ||
status: API_RESPONSE_STATUS.success, | ||
response: updatedGroups[0], | ||
}); | ||
} catch (err) { | ||
errorHandler(res, err); | ||
} | ||
}; | ||
|
||
export const moveAccountGroupSchema = z.object({ | ||
params: z.object({ groupId: recordId() }), | ||
body: z.object({ newParentGroupId: recordId().nullable() }), | ||
}); | ||
|
||
type MoveAccountGroupParams = z.infer<typeof moveAccountGroupSchema>; |
32 changes: 32 additions & 0 deletions
32
src/controllers/account-groups/remove-account-from-group.ts
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { z } from 'zod'; | ||
import { API_RESPONSE_STATUS } from 'shared-types'; | ||
import type { CustomResponse } from '@common/types'; | ||
import { recordId } from '@common/lib/zod/custom-types'; | ||
import { errorHandler } from '@controllers/helpers'; | ||
import * as accountGroupService from '@services/account-groups'; | ||
|
||
export const removeAccountFromGroup = async (req, res: CustomResponse) => { | ||
try { | ||
const { accountId, groupId }: RemoveAccountFromGroupParams['params'] = req.validated.params; | ||
|
||
await accountGroupService.removeAccountFromGroup({ | ||
accountId, | ||
groupId, | ||
}); | ||
|
||
return res.status(200).json({ | ||
status: API_RESPONSE_STATUS.success, | ||
}); | ||
} catch (err) { | ||
errorHandler(res, err); | ||
} | ||
}; | ||
|
||
export const removeAccountFromGroupSchema = z.object({ | ||
params: z.object({ | ||
accountId: recordId(), | ||
groupId: recordId(), | ||
}), | ||
}); | ||
|
||
type RemoveAccountFromGroupParams = z.infer<typeof removeAccountFromGroupSchema>; |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { z } from 'zod'; | ||
import type { CustomResponse } from '@common/types'; | ||
import { API_RESPONSE_STATUS } from 'shared-types'; | ||
import { recordId } from '@common/lib/zod/custom-types'; | ||
import { errorHandler } from '@controllers/helpers'; | ||
import * as accountGroupService from '@services/account-groups'; | ||
|
||
export const updateAccountGroup = async (req, res: CustomResponse) => { | ||
try { | ||
const { id: userId } = req.user; | ||
const { groupId }: UpdateAccountGroupParams['params'] = req.validated.params; | ||
const updates: UpdateAccountGroupParams['body'] = req.validated.body; | ||
|
||
const updatedGroups = await accountGroupService.updateAccountGroup({ | ||
groupId, | ||
userId, | ||
...updates, | ||
}); | ||
|
||
return res.status(200).json({ | ||
status: API_RESPONSE_STATUS.success, | ||
response: updatedGroups, | ||
}); | ||
} catch (err) { | ||
errorHandler(res, err); | ||
} | ||
}; | ||
|
||
export const updateAccountGroupSchema = z.object({ | ||
params: z.object({ groupId: recordId() }), | ||
body: z | ||
.object({ | ||
name: z.string().min(1), | ||
parentGroupId: recordId().nullable().optional(), | ||
}) | ||
.strict() | ||
.partial(), | ||
}); | ||
|
||
type UpdateAccountGroupParams = z.infer<typeof updateAccountGroupSchema>; |
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
Oops, something went wrong.