-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix stale data on activity feed when reseting & replace unstable_cach…
…e with tags in fetch request in activity feed
- Loading branch information
1 parent
f17148f
commit 25a08b3
Showing
4 changed files
with
48 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,53 @@ | ||
import { NextResponse, type NextRequest } from 'next/server'; | ||
import { searchParamsCache } from '~/app/dashboard/_components/ActivityFeed/SearchParams'; | ||
import { getActivities } from '~/queries/activityFeed'; | ||
import { requireApiAuth } from '~/utils/auth'; | ||
import { prisma } from '~/utils/db'; | ||
|
||
export async function GET(request: NextRequest) { | ||
await requireApiAuth(); | ||
|
||
const rawParams = request.nextUrl.searchParams.entries(); | ||
const searchParams = searchParamsCache.parse(Object.fromEntries(rawParams)); | ||
|
||
const result = await getActivities(searchParams); | ||
return NextResponse.json(result); | ||
const { page, perPage, sort, sortField, filterParams } = searchParams; | ||
|
||
// Number of items to skip | ||
const offset = page > 0 ? (page - 1) * perPage : 0; | ||
|
||
// Generate the dynamic filter parameters for the database call from the | ||
// input filter params. | ||
const queryFilterParams = filterParams | ||
? { | ||
OR: [ | ||
...filterParams.map(({ id, value }) => { | ||
const operator = Array.isArray(value) ? 'in' : 'contains'; | ||
return { | ||
[id]: { [operator]: value }, | ||
}; | ||
}), | ||
], | ||
} | ||
: {}; | ||
|
||
// Transaction is used to ensure both queries are executed in a single transaction | ||
const [count, events] = await prisma.$transaction([ | ||
prisma.events.count({ | ||
where: { | ||
...queryFilterParams, | ||
}, | ||
}), | ||
prisma.events.findMany({ | ||
take: perPage, | ||
skip: offset, | ||
orderBy: { [sortField]: sort }, | ||
where: { | ||
...queryFilterParams, | ||
}, | ||
}), | ||
]); | ||
|
||
const pageCount = Math.ceil(count / perPage); | ||
|
||
|
||
return NextResponse.json({ events, pageCount }); | ||
} |
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 was deleted.
Oops, something went wrong.