-
Notifications
You must be signed in to change notification settings - Fork 0
feat: update revenues task #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { repository } from '@roll-stack/database' | ||
|
|
||
| export default defineEventHandler(async (event) => { | ||
| const kitchenId = getRouterParam(event, 'kitchenId') | ||
| if (!kitchenId) { | ||
| throw createError({ | ||
| statusCode: 400, | ||
| message: 'Id is required', | ||
| }) | ||
| } | ||
|
|
||
| const query = getQuery(event) | ||
| const start = query.start ? new Date(query.start.toString()) : null | ||
| const end = query.end ? new Date(query.end.toString()) : null | ||
| if (!start || !end) { | ||
| throw createError({ | ||
| statusCode: 400, | ||
| message: 'Start and end is required', | ||
| }) | ||
| } | ||
|
|
||
| return repository.kitchen.listRevenuesByKitchenForPeriod(kitchenId, start, end) | ||
| }) | ||
|
Comment on lines
+3
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainConsider adding authentication and input validation. The endpoint lacks authentication checks and additional input validation. Consider adding:
Would you like me to help implement authentication middleware and additional validation for this endpoint? 🏁 Script executed: #!/bin/bash
# Search for references to authentication mechanisms
rg "auth" -n || true
# Search for any custom middleware definitions
rg "middleware" -n || true
# List a few API route handlers to see common patterns
rg "defineEventHandler" -n | head -n 20Length of output: 5233 Add authentication, date validation, and rate limiting to this endpoint This handler currently allows unauthenticated access and only checks for the presence of
File to update:
Let me know if you’d like assistance wiring up an 🤖 Prompt for AI Agents |
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,40 @@ | ||||||||||||||||||
| import { repository } from '@roll-stack/database' | ||||||||||||||||||
| import { endOfWeek, startOfWeek } from 'date-fns' | ||||||||||||||||||
|
|
||||||||||||||||||
| const logger = useLogger('kitchen:revenue-update') | ||||||||||||||||||
|
|
||||||||||||||||||
| export default defineTask({ | ||||||||||||||||||
| meta: { | ||||||||||||||||||
| name: 'kitchen:revenue-update', | ||||||||||||||||||
| description: 'Update weekly revenue of kitchens', | ||||||||||||||||||
| }, | ||||||||||||||||||
| async run() { | ||||||||||||||||||
| try { | ||||||||||||||||||
| const kitchens = await repository.kitchen.list() | ||||||||||||||||||
|
|
||||||||||||||||||
| // From this monday to sunday (use UTC+0 time zone) | ||||||||||||||||||
| const now = new Date() | ||||||||||||||||||
|
|
||||||||||||||||||
| const thisMonday = startOfWeek(now, { weekStartsOn: 1 }) | ||||||||||||||||||
| const thisSunday = endOfWeek(now, { weekStartsOn: 1 }) | ||||||||||||||||||
|
|
||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||
| logger.log(thisMonday, thisSunday) | ||||||||||||||||||
|
|
||||||||||||||||||
| for (const kitchen of kitchens) { | ||||||||||||||||||
| const revenues = await repository.kitchen.listRevenuesByKitchenForPeriod(kitchen.id, thisMonday, thisSunday) | ||||||||||||||||||
|
|
||||||||||||||||||
| const revenueForThisWeek = revenues.reduce((acc, curr) => acc + curr.total, 0) | ||||||||||||||||||
|
|
||||||||||||||||||
| await repository.kitchen.update(kitchen.id, { | ||||||||||||||||||
| revenueForThisWeek, | ||||||||||||||||||
| }) | ||||||||||||||||||
|
|
||||||||||||||||||
| // logger.log(`Kitchen ${kitchen.id}: Revenue updated from ${kitchen.revenueForThisWeek} to ${revenueForThisWeek}`) | ||||||||||||||||||
| } | ||||||||||||||||||
| } catch (error) { | ||||||||||||||||||
| errorResolver(error) | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve error handling specificity. The generic Apply this diff to improve error handling: - } catch (error) {
- errorResolver(error)
- }
+ } catch (error) {
+ logger.error('Failed to update kitchen revenues:', error)
+ errorResolver(error)
+ throw error // Re-throw to indicate task failure
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
|
|
||||||||||||||||||
| return { result: true } | ||||||||||||||||||
| }, | ||||||||||||||||||
| }) | ||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.