Skip to content
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

🚀 feat: added pinojs and logger aggregation using asynclocalstorage implementation. [v.0] #65

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ MIDAZ_API_PORT=3000
MIDAZ_BASE_PATH='http://${MIDAZ_API_HOST}:${MIDAZ_API_PORT}/v1'
MIDAZ_TRANSACTION_BASE_HOST='transaction'
MIDAZ_TRANSACTION_BASE_PORT=3002
MIDAZ_TRANSACTION_BASE_PATH='http://${MIDAZ_API_HOST}:${MIDAZ_TRANSACTION_BASE_PORT}/v1'
MIDAZ_TRANSACTION_BASE_PATH='http://${MIDAZ_API_HOST}:${MIDAZ_TRANSACTION_BASE_PORT}/v1'

ENABLE_DEBUG=true
ENABLE_REQUEST_TIMELINE=true
NODE_ENV=development
17 changes: 15 additions & 2 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,22 @@ const nextConfig = {
? { properties: ['^data-testid$'] }
: false
},
webpack: (config, { isServer }) => {
config.resolve.fallback = {
...config.resolve.fallback,
worker_threads: false,
pino: false
}

return config
},
experimental: {
instrumentationHook: true,
serverComponentsExternalPackages: ['@opentelemetry/instrumentation']
serverComponentsExternalPackages: [
'pino',
'pino-pretty',
'@opentelemetry/instrumentation'
],
instrumentationHook: true
}
}

Expand Down
17,586 changes: 8,686 additions & 8,900 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
"lucide-react": "^0.453.0",
"next": "^14.1.3",
"next-auth": "^4.24.7",
"pino": "^9.5.0",
"pino-pretty": "^13.0.0",
"react": "^18.3.1",
"react-chartjs-2": "^5.2.0",
"react-color": "^2.19.3",
Expand Down Expand Up @@ -96,6 +98,7 @@
"@types/lodash": "^4.17.13",
"@types/node": "^22",
"@types/node-fetch": "^2.6.11",
"@types/pino": "^7.0.5",
"@types/react": "^18",
"@types/react-color": "^3.0.12",
"@types/react-dom": "^18",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export async function POST(
) {
try {
const body = await request.json()
console.log(body)
const organizationId = params.id
const ledgerId = params.ledgerId

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
UpdatePortfolio,
UpdatePortfolioUseCase
} from '@/core/application/use-cases/portfolios/update-portfolio-use-case'
import { NextResponse } from 'next/server'
import { NextRequest, NextResponse } from 'next/server'
import { RequestContextManager } from '@/lib/logger/request-context'

const updatePortfolioUseCase: UpdatePortfolio = container.get<UpdatePortfolio>(
UpdatePortfolioUseCase
Expand All @@ -25,71 +26,113 @@ const getPortfolioByIdUseCase: FetchPortfolioById =
container.get<FetchPortfolioById>(FetchPortfolioByIdUseCase)

export async function DELETE(
request: Request,
request: NextRequest,
{ params }: { params: { id: string; ledgerId: string; portfolioId: string } }
) {
try {
const organizationId = params.id
const ledgerId = params.ledgerId
const portfolioId = params.portfolioId

await deletePortfolioUseCase.execute(organizationId, ledgerId, portfolioId)
const { id: organizationId, ledgerId, portfolioId } = params
const midazId = request.headers.get('X-Midaz-Id')
return RequestContextManager.runWithContext(
request.url,
request.method,
{
organizationId,
ledgerId,
portfolioId,
deletedAt: new Date().toISOString(),
midazId
},
async () => {
RequestContextManager.addEvent({
layer: 'api',
operation: 'delete_portfolio',
level: 'audit',
message: 'Deleting portfolio',
metadata: {
organizationId,
ledgerId,
portfolioId
}
})

return NextResponse.json({}, { status: 200 })
} catch (error: any) {
console.error('Error deleting portfolio', error)
const { message, status } = await apiErrorHandler(error)
try {
await deletePortfolioUseCase.execute(
organizationId,
ledgerId,
midazId!,
portfolioId
)
return NextResponse.json({}, { status: 200 })
} catch (error: any) {
console.error('Error deleting portfolio', error)
paulofdev marked this conversation as resolved.
Show resolved Hide resolved
const { message, status } = await apiErrorHandler(error)

return NextResponse.json({ message }, { status })
}
return NextResponse.json({ message }, { status })
}
}
)
}

export async function PATCH(
request: Request,
request: NextRequest,
{ params }: { params: { id: string; ledgerId: string; portfolioId: string } }
) {
try {
const body = await request.json()
const organizationId = params.id
const ledgerId = params.ledgerId
const portfolioId = params.portfolioId
const { id: organizationId, ledgerId, portfolioId } = params
const midazId = request.headers.get('X-Midaz-Id')
return RequestContextManager.runWithContext(
request.url,
request.method,
{ organizationId, ledgerId, portfolioId, midazId },
async () => {
try {
const body = await request.json()

const portfolioUpdated = await updatePortfolioUseCase.execute(
organizationId,
ledgerId,
portfolioId,
body
)
const portfolioUpdated = await updatePortfolioUseCase.execute(
organizationId,
ledgerId,
portfolioId,
midazId!,
body
)

return NextResponse.json(portfolioUpdated)
} catch (error: any) {
console.error('Error updating portfolio', error)
const { message, status } = await apiErrorHandler(error)
return NextResponse.json(portfolioUpdated)
} catch (error: any) {
console.error('Error updating portfolio', error)
paulofdev marked this conversation as resolved.
Show resolved Hide resolved
const { message, status } = await apiErrorHandler(error)

return NextResponse.json({ message }, { status })
}
return NextResponse.json({ message }, { status })
}
}
)
}

export async function GET(
request: Request,
request: NextRequest,
{ params }: { params: { id: string; ledgerId: string; portfolioId: string } }
) {
try {
const organizationId = params.id
const ledgerId = params.ledgerId
const portfolioId = params.portfolioId
const { id: organizationId, ledgerId, portfolioId } = params
return RequestContextManager.runWithContext(
request.url,
request.method,
{ organizationId, ledgerId, portfolioId },
async () => {
try {
const organizationId = params.id
const ledgerId = params.ledgerId
const portfolioId = params.portfolioId

const portfolio = await getPortfolioByIdUseCase.execute(
organizationId,
ledgerId,
portfolioId
)
const portfolio = await getPortfolioByIdUseCase.execute(
organizationId,
ledgerId,
portfolioId
)

return NextResponse.json(portfolio)
} catch (error: any) {
console.error('Error getting portfolio', error)
const { message, status } = await apiErrorHandler(error)
return NextResponse.json(portfolio)
} catch (error: any) {
console.error('Error getting portfolio', error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remover console.log()

const { message, status } = await apiErrorHandler(error)

return NextResponse.json({ message }, { status })
}
return NextResponse.json({ message }, { status })
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
FetchAllPortfolios,
FetchAllPortfoliosUseCase
} from '@/core/application/use-cases/portfolios/fetch-all-portfolio-use-case'

import { NextResponse } from 'next/server'
import { NextRequest, NextResponse } from 'next/server'
import { RequestContextManager } from '@/lib/logger/request-context'

const createPortfolioUseCase: CreatePortfolio = container.get<CreatePortfolio>(
CreatePortfolioUseCase
Expand All @@ -22,49 +22,63 @@ export async function GET(
request: Request,
{ params }: { params: { id: string; ledgerId: string } }
) {
try {
const { searchParams } = new URL(request.url)
const limit = Number(searchParams.get('limit')) || 100
const page = Number(searchParams.get('page')) || 1
const organizationId = params.id
const ledgerId = params.ledgerId

const portfolios = await fetchAllPortfoliosUseCase.execute(
organizationId,
ledgerId,
page,
limit
)
const { searchParams } = new URL(request.url)
const limit = Number(searchParams.get('limit')) || 100
const page = Number(searchParams.get('page')) || 1
const organizationId = params.id
const ledgerId = params.ledgerId
return RequestContextManager.runWithContext(
request.url,
request.method,
{ organizationId, ledgerId },
async () => {
try {
const portfolios = await fetchAllPortfoliosUseCase.execute(
organizationId,
ledgerId,
page,
limit
)

return NextResponse.json(portfolios)
} catch (error: any) {
console.error('Error fetching all portfolios', error)
const { message, status } = await apiErrorHandler(error)

return NextResponse.json({ message }, { status })
}
return NextResponse.json(portfolios)
} catch (error: any) {
const { message, status } = await apiErrorHandler(error)
return NextResponse.json({ message }, { status })
}
}
)
}

export async function POST(
request: Request,
request: NextRequest,
{ params }: { params: { id: string; ledgerId: string } }
) {
try {
const body = await request.json()
const organizationId = params.id
const ledgerId = params.ledgerId

const portfolio = await createPortfolioUseCase.execute(
organizationId,
ledgerId,
body
)

return NextResponse.json(portfolio)
} catch (error: any) {
console.error('Error creating portfolio', error)
const { message, status } = await apiErrorHandler(error)

return NextResponse.json({ message }, { status })
}
const { id: organizationId, ledgerId } = params
const midazId = request.headers.get('X-Midaz-Id')
return RequestContextManager.runWithContext(
request.url,
request.method,
{ organizationId, ledgerId, midazId },
async () => {
try {
if (!midazId) {
return NextResponse.json(
{ message: 'X-Midaz-Id header is required' },
{ status: 400 }
)
}
const body = await request.json()
const portfolio = await createPortfolioUseCase.execute(
organizationId,
ledgerId,
midazId,
body
)
return NextResponse.json(portfolio)
} catch (error) {
const { message, status } = await apiErrorHandler(error)
return NextResponse.json({ message }, { status })
}
}
)
}
Loading
Loading