Skip to content

Commit

Permalink
refactor(api): share dayjs lib to utilities package
Browse files Browse the repository at this point in the history
  • Loading branch information
Quốc Khánh committed Jul 18, 2024
1 parent 178e5bb commit a1e3110
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 52 deletions.
2 changes: 1 addition & 1 deletion apps/api/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const nextConfig = {
reactStrictMode: true,
rewrites: () => [{ source: '/v1/:path*', destination: '/api/v1/:path*' }],
transpilePackages: ['@6pm/validation'],
transpilePackages: ['@6pm/validation', '@6pm/utilities'],
}

module.exports = nextConfig
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"check": "biome check"
},
"dependencies": {
"@6pm/utilities": "workspace:^",
"@6pm/validation": "workspace:^",
"@clerk/backend": "^1.2.4",
"@hono/clerk-auth": "^2.0.0",
"@hono/node-server": "^1.11.4",
"@hono/zod-validator": "^0.2.2",
"@prisma/client": "^5.16.0",
"@vercel/blob": "^0.23.4",
"dayjs": "^1.11.11",
"hono": "^4.4.8",
"next": "^14.2.4",
"openai": "^4.52.7",
Expand Down
52 changes: 5 additions & 47 deletions apps/api/v1/services/budget.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { calculateBudgetPeriodStartEndDates } from '@6pm/utilities'
import type { CreateBudget, UpdateBudget } from '@6pm/validation'
import {
type Budget,
BudgetPeriodType,
BudgetUserPermission,
type User,
} from '@prisma/client'
import { dayjsExtended } from '../../lib/dayjs'
import { type Budget, BudgetUserPermission, type User } from '@prisma/client'
import prisma from '../../lib/prisma'
import { inviteUserToBudget } from './budget-invitation.service'

Expand Down Expand Up @@ -92,49 +87,12 @@ export async function isUserBudgetOwner({
export async function findBudget({ budgetId }: { budgetId: string }) {
return prisma.budget.findUnique({
where: { id: budgetId },
include: {
periodConfig: true,
},
})
}

/**
* Given an anchor date and period type,
* calculate the start and end dates of the budget period.
* Example:
* - anchorDate: 2022-01-15
* - periodType: MONTHLY
* - startDate: 2022-01-01
* - endDate: 2022-01-31
*/
function calculateBudgetPeriodStartEndDates({
anchorDate,
type,
}: {
anchorDate: Date
type: BudgetPeriodType
}) {
switch (type) {
case BudgetPeriodType.MONTHLY:
return {
startDate: dayjsExtended(anchorDate).startOf('month').toDate(),
endDate: dayjsExtended(anchorDate).endOf('month').toDate(),
}
case BudgetPeriodType.QUARTERLY:
return {
startDate: dayjsExtended(anchorDate).startOf('quarter').toDate(),
endDate: dayjsExtended(anchorDate).endOf('quarter').toDate(),
}
case BudgetPeriodType.YEARLY:
return {
startDate: dayjsExtended(anchorDate).startOf('year').toDate(),
endDate: dayjsExtended(anchorDate).endOf('year').toDate(),
}
default:
return {
startDate: null,
endDate: null,
}
}
}

export async function createBudget({
user,
data,
Expand Down
1 change: 1 addition & 0 deletions packages/utilities/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @6pm/utilities
7 changes: 7 additions & 0 deletions packages/utilities/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.0/schema.json",
"extends": ["../../biome.json"],
"files": {
"include": ["**/*.ts", "**/*.tsx"]
}
}
17 changes: 17 additions & 0 deletions packages/utilities/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@6pm/utilities",
"version": "1.0.0",
"description": "Shared utilities between apps",
"main": "src/index.ts",
"scripts": {
"check": "biome check"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@6pm/validation": "workspace:^",
"@prisma/client": "^5.16.0",
"dayjs": "^1.11.11"
}
}
File renamed without changes.
47 changes: 47 additions & 0 deletions packages/utilities/src/date/period.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { BudgetPeriodType } from '@prisma/client'
import { dayjsExtended } from './dayjs'

/**
* Given an anchor date and period type,
* calculate the start and end dates of the budget period.
* Example:
* - anchorDate: 2022-01-15
* - periodType: MONTHLY
* - startDate: 2022-01-01
* - endDate: 2022-01-31
*/
export function calculateBudgetPeriodStartEndDates({
anchorDate,
type,
}: {
anchorDate: Date
type: BudgetPeriodType
}) {
switch (type) {
case BudgetPeriodType.WEEKLY:
return {
startDate: dayjsExtended(anchorDate).startOf('week').toDate(),
endDate: dayjsExtended(anchorDate).endOf('week').toDate(),
}
case BudgetPeriodType.MONTHLY:
return {
startDate: dayjsExtended(anchorDate).startOf('month').toDate(),
endDate: dayjsExtended(anchorDate).endOf('month').toDate(),
}
case BudgetPeriodType.QUARTERLY:
return {
startDate: dayjsExtended(anchorDate).startOf('quarter').toDate(),
endDate: dayjsExtended(anchorDate).endOf('quarter').toDate(),
}
case BudgetPeriodType.YEARLY:
return {
startDate: dayjsExtended(anchorDate).startOf('year').toDate(),
endDate: dayjsExtended(anchorDate).endOf('year').toDate(),
}
default:
return {
startDate: null,
endDate: null,
}
}
}
2 changes: 2 additions & 0 deletions packages/utilities/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './date/dayjs'
export * from './date/period'
18 changes: 15 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a1e3110

Please sign in to comment.