feat: implement expense creation functionality with validation#24
Open
RishikaBothra wants to merge 10 commits into
Open
feat: implement expense creation functionality with validation#24RishikaBothra wants to merge 10 commits into
RishikaBothra wants to merge 10 commits into
Conversation
adistrim
reviewed
Mar 3, 2026
1ecedf8 to
518e384
Compare
adistrim
requested changes
Mar 10, 2026
Comment on lines
+6
to
+21
| export const createExpense = async ({ | ||
| title, | ||
| description, | ||
| amount, | ||
| userId, | ||
| }: CreateExpenseInput) => { | ||
| await db().insert(expense).values({ | ||
| title, | ||
| description, | ||
| amount, | ||
| userId, | ||
| status: "draft", | ||
| }); | ||
|
|
||
| return { msg: "status: draft" }; | ||
| }; |
Comment on lines
+23
to
+36
| export const getExpenseById = async (id: number) => { | ||
| const result = await db() | ||
| .select({ | ||
| id: expense.id, | ||
| userId: expense.userId, | ||
| title: expense.title, | ||
| amount: expense.amount, | ||
| status: expense.status, | ||
| }) | ||
| .from(expense) | ||
| .where(eq(expense.id, id)); | ||
|
|
||
| return result[0]; | ||
| }; |
Comment on lines
+38
to
+44
| export const updateExpenseStatus = async (id: number, status: "draft" | "submitted" | "accepted" | "rejected") => { | ||
| await db() | ||
| .update(expense) | ||
| .set({ status, | ||
| updatedAt: new Date() }) | ||
| .where(eq(expense.id, id)); | ||
| }; |
Comment on lines
+94
to
+112
| export const deleteExpenseService = async (expenseId: number, userId: number) => { | ||
| const expenseData = await getExpenseById(expenseId); | ||
|
|
||
| if (!expenseData) { | ||
| throw new Error("Expense not found"); | ||
| } | ||
|
|
||
| if (expenseData.userId !== userId) { | ||
| throw new Error("Unauthorized to delete this expense"); | ||
| } | ||
|
|
||
| if (expenseData.status !== "draft") { | ||
| throw new Error("Only draft expenses can be deleted"); | ||
| } | ||
|
|
||
| await deleteExpense(expenseId); | ||
|
|
||
| return; | ||
| } |
Collaborator
There was a problem hiding this comment.
just for deleting you don't need to perform two database operations
|
|
||
| export const getExpensesByUserId = async (userId: number) => { | ||
| const result = await db() | ||
| .select() |
Collaborator
There was a problem hiding this comment.
do not select *
select exactly what's needed
| return; | ||
| }; | ||
|
|
||
| export const updateExpenseService = async (expenseId: number, userId: number, data: Partial<CreateExpenseInput>) => { |
Collaborator
There was a problem hiding this comment.
there are some redundant input validation checks.
if zod is already doing it at the route level, you don't need to do it here again
Comment on lines
+31
to
+49
| export const submitExpenseService = async (expenseId: number, userId: number) => { | ||
| const expenseData = await getExpenseById(expenseId); | ||
|
|
||
| if (!expenseData) { | ||
| throw new Error("Expense not found"); | ||
| } | ||
|
|
||
| if (expenseData.userId !== userId) { | ||
| throw new Error("Unauthorized to submit this expense"); | ||
| } | ||
|
|
||
| if (expenseData.status !== "draft") { | ||
| throw new Error("Only draft expenses can be submitted"); | ||
| } | ||
|
|
||
| await updateExpenseStatus(expenseId, "submitted"); | ||
|
|
||
| return; | ||
| }; |
Collaborator
There was a problem hiding this comment.
please research it this the best way of doing this?
can we do it but without multiple times querying the database
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #23