Skip to content

Commit

Permalink
add analytics (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrobaina authored May 22, 2024
1 parent 591de8f commit f9c952f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 8 deletions.
7 changes: 3 additions & 4 deletions Dockerfile.prod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Build stage
FROM arm64v8/node:16-buster AS build-stage
FROM arm64v8/node:18.17.0-buster AS build-stage

WORKDIR /usr/src/app

Expand All @@ -16,14 +16,13 @@ RUN npx prisma generate --schema ./src/database/prisma/schema.prisma
RUN yarn build

# Production stage
FROM arm64v8/node:16-buster
FROM arm64v8/node:18.17.0-buster
WORKDIR /usr/src/app

# Copy built node modules and compiled code
COPY --from=build-stage /usr/src/app/node_modules ./node_modules
COPY --from=build-stage /usr/src/app/dist ./dist
COPY --from=build-stage /usr/src/app/src/database/prisma ./src/database/prisma

# Command to run the app
CMD ["sh", "-c", "npx prisma migrate deploy --schema=./src/database/prisma/schema.prisma && node dist/index.js"]

21 changes: 17 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import routes from './routes'
import path from 'path'
import { config } from './config/config'

const uploadsDir = process.env.DEV === 'true' ? 'uploads' : process.env.UPLOAD_DIR || 'uploads';
const uploadsDir =
process.env.DEV === 'true' ? 'uploads' : process.env.UPLOAD_DIR || 'uploads'

// initializations
const app = express()
Expand All @@ -21,9 +22,21 @@ app.set('port', port)

app.use(morgan('dev'))

app.use(cors({
origin: process.env.DEV === 'true' ? 'http://localhost:3000' : 'http://frontend' // O la URL de tu frontend
}));
const getCors = () => {
if (process.env.BETA === 'true') {
return 'http://beta-frontend'
}
if (process.env.DEV === 'true') {
return 'http://localhost:3000'
}
return 'http://frontend'
}

app.use(
cors({
origin: getCors(),
}),
)

app.use(express.json())

Expand Down
2 changes: 2 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import seedRoutes from './seed'
import vaccineRoutes from './vaccine'
import appointmentsRoutes from './appointment'
import teamRoutes from './team'
import petAnalyticsRoutes from './petAnalytics'

const router = routerx()

Expand All @@ -17,6 +18,7 @@ router.use(
teamRoutes,
productRoutes,
vaccineRoutes,
petAnalyticsRoutes,
appointmentsRoutes,
)
router.use('/api/auth', authRoutes)
Expand Down
9 changes: 9 additions & 0 deletions src/routes/petAnalytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// src/routes/index.ts
import express from 'express'
import { getAnalytics } from '../useCases/analytics/analytics.controller'

const router = express.Router()

router.get('/analytics/:userId', getAnalytics)

export default router
13 changes: 13 additions & 0 deletions src/useCases/analytics/analytics.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Request, Response } from 'express'
import * as getPetAnalytics from './service/analytics.service'

export const getAnalytics = async (req: Request, res: Response) => {
const { userId } = req.params
try {
const analytics = await getPetAnalytics.getPetAnalytics(userId as string)
res.status(200).json(analytics)
} catch (error: any) {
console.log(error)
res.status(500).json({ error: error.message })
}
}
37 changes: 37 additions & 0 deletions src/useCases/analytics/service/analytics.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// src/useCases/analyticsCase/analytics.service.ts
import { prisma } from '../../../database/prisma'

export const getPetAnalytics = async (userId: string) => {
// Total pets where the user is the owner in any capacity
const totalPets = await prisma.pet.count({
where: {
OR: [
{ createdBy: userId },
{ adoptedBy: userId },
{ vetId: userId },
{ shelterId: userId },
],
},
})

// Adopted pets where the user is the owner in any capacity
const adoptedPets = await prisma.pet.count({
where: {
adopted: true,
OR: [
{ createdBy: userId },
{ adoptedBy: userId },
{ vetId: userId },
{ shelterId: userId },
],
},
})

const petsInAdoption = totalPets - adoptedPets

return {
totalPets,
adoptedPets,
petsInAdoption,
}
}

0 comments on commit f9c952f

Please sign in to comment.