This repository has been archived by the owner on Apr 5, 2024. It is now read-only.
-
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.
♻️ refactor api to deliver transformed data
- Loading branch information
1 parent
9a0440f
commit bff2bcf
Showing
6 changed files
with
217 additions
and
74 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export const schema = gql` | ||
type MonthlyBudgetCategory { | ||
id: String! | ||
name: String! | ||
month: Int! | ||
year: Int! | ||
assigned: Float! | ||
activity: Float! | ||
available: Float! | ||
} | ||
type MonthlyBudgetGroup { | ||
id: String! | ||
name: String! | ||
month: Int! | ||
year: Int! | ||
assigned: Float! | ||
activity: Float! | ||
available: Float! | ||
subRows: [MonthlyBudgetCategory!] | ||
} | ||
type Query { | ||
monthlyBudget( | ||
id: String! | ||
userId: String! | ||
month: Int! | ||
year: Int! | ||
): [MonthlyBudgetGroup!]! @requireAuth | ||
} | ||
` |
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,111 @@ | ||
import type { | ||
MonthlyBudgetGroupRelationResolvers, | ||
QueryResolvers, | ||
} from 'types/graphql' | ||
|
||
import { db } from 'src/lib/db' | ||
|
||
export const monthlyBudget: QueryResolvers['monthlyBudget'] = async ({ | ||
id, | ||
userId, | ||
month, | ||
year, | ||
}) => { | ||
const query = await db.budget.findUnique({ | ||
where: { | ||
id, | ||
userId, | ||
}, | ||
include: { | ||
budgetCategoryGroups: { | ||
orderBy: { | ||
sortOrder: 'asc', | ||
}, | ||
include: { | ||
monthlyCategoryGroupActivities: { | ||
where: { | ||
month, | ||
year, | ||
}, | ||
}, | ||
budgetCategories: { | ||
orderBy: { | ||
sortOrder: 'asc', | ||
}, | ||
include: { | ||
monthlyBudgetPerCategories: { | ||
where: { | ||
month, | ||
year, | ||
}, | ||
include: { | ||
monthlyCategoryActivity: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
const res = query.budgetCategoryGroups.map((group) => { | ||
return { | ||
id: group.id, | ||
name: group.name, | ||
month, | ||
year, | ||
assigned: | ||
group.monthlyCategoryGroupActivities[0]?.assigned.toNumber() || 0, | ||
activity: | ||
group.monthlyCategoryGroupActivities[0]?.activity.toNumber() || 0, | ||
available: | ||
group.monthlyCategoryGroupActivities[0]?.available.toNumber() || 0, | ||
} | ||
}) | ||
|
||
return res | ||
} | ||
|
||
export const MonthlyBudgetGroup: MonthlyBudgetGroupRelationResolvers = { | ||
subRows: async (_obj, { root }) => { | ||
const categoryGroups = await db.budgetCategoryGroup.findUnique({ | ||
where: { id: root.id }, | ||
include: { | ||
budgetCategories: { | ||
orderBy: { | ||
sortOrder: 'asc', | ||
}, | ||
include: { | ||
monthlyBudgetPerCategories: { | ||
where: { | ||
month: root.month, | ||
year: root.year, | ||
}, | ||
include: { | ||
monthlyCategoryActivity: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
return categoryGroups.budgetCategories.map((category) => { | ||
return { | ||
id: category.id, | ||
name: category.name, | ||
month: root.month, | ||
year: root.year, | ||
assigned: | ||
category.monthlyBudgetPerCategories[0]?.assigned.toNumber() || 0, | ||
activity: | ||
category.monthlyBudgetPerCategories[0]?.monthlyCategoryActivity.activity.toNumber() || | ||
0, | ||
available: | ||
category.monthlyBudgetPerCategories[0]?.monthlyCategoryActivity.available.toNumber() || | ||
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
This file was deleted.
Oops, something went wrong.
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