-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
591de8f
commit f9c952f
Showing
6 changed files
with
81 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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 |
This file contains 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
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 }) | ||
} | ||
} |
This file contains 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
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, | ||
} | ||
} |