-
Notifications
You must be signed in to change notification settings - Fork 0
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
6c145b0
commit 1e74ea9
Showing
4 changed files
with
101 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { COLLECTION } from '@/types/database'; | ||
import { | ||
unstable_cacheLife as cacheLife, | ||
unstable_cacheTag as cacheTag, | ||
revalidateTag, | ||
} from 'next/cache'; | ||
import { monthFilter } from '../utils/transaction-filters'; | ||
import { aggregateSum, fetch } from '../firebase-admin/firestore'; | ||
|
||
/** | ||
* Cache keys | ||
*/ | ||
const getTxnByMonthYearKey = (userId: string, month: number, year: number) => | ||
`transactions-month-year-${userId}-${month}-${year}`; | ||
|
||
const getMontlyTxnSumKey = (userId: string, month: number, year: number) => | ||
`monthly-sum-${userId}-${month}-${year}`; | ||
|
||
/** | ||
* Cache flush methods | ||
*/ | ||
export const clearMonthYearTxnCache = ( | ||
userId: string, | ||
month: number, | ||
year: number | ||
) => { | ||
revalidateTag(getTxnByMonthYearKey(userId, month, year)); | ||
revalidateTag(getMontlyTxnSumKey(userId, month, year)); | ||
}; | ||
|
||
/** | ||
* Cache methods | ||
*/ | ||
export const getTxnByMonthYearCache = async ( | ||
userId: string, | ||
month: number, | ||
year: number | ||
) => { | ||
'use cache'; | ||
cacheTag(getTxnByMonthYearKey(userId, month, year)); | ||
cacheLife('max'); | ||
|
||
return fetch({ | ||
userId, | ||
collection: COLLECTION.TRANSACTIONS, | ||
conditions: monthFilter(month, year), | ||
}); | ||
}; | ||
|
||
export const getMonthTotalSumCache = async ( | ||
userId: string, | ||
month: number, | ||
year: number | ||
) => { | ||
'use cache'; | ||
cacheTag(getMontlyTxnSumKey(userId, month, year)); | ||
cacheLife('max'); | ||
|
||
return await aggregateSum({ | ||
userId, | ||
collection: COLLECTION.TRANSACTIONS, | ||
conditions: monthFilter(month, year), | ||
}); | ||
}; |
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.
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,29 @@ | ||
import { COLLECTION, TCondition } from '@/types/database'; | ||
|
||
export const monthFilter = ( | ||
month: number, | ||
year: number | ||
): TCondition<COLLECTION.TRANSACTIONS>[] => { | ||
const startDate = new Date(year, month, 1).getTime(); | ||
const endDate = new Date(year, month + 1, 1).getTime(); | ||
return [ | ||
{ | ||
fieldPath: 'date', | ||
opStr: '>=', | ||
value: startDate, | ||
}, | ||
{ | ||
fieldPath: 'date', | ||
opStr: '<', | ||
value: endDate, | ||
}, | ||
]; | ||
}; | ||
|
||
export const cardFilter = ( | ||
cardId: string | ||
): TCondition<COLLECTION.TRANSACTIONS> => ({ | ||
fieldPath: 'cardId', | ||
opStr: '==', | ||
value: cardId, | ||
}); |