Skip to content

Commit

Permalink
Merge pull request #384 from Assocify-Team/ft/ReceiptsToAsso
Browse files Browse the repository at this point in the history
Link receipts to the current association
  • Loading branch information
zizouet authored May 29, 2024
2 parents 23467d7 + d6bd6f8 commit 8aa2867
Showing 1 changed file with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class ReceiptAPI(private val db: SupabaseClient, cachePath: Path) : SupabaseApi(
private var receipts: List<Receipt>? = null
/// The user's UID
private var userUid: String? = CurrentUser.userUid
private var associationUid: String? = CurrentUser.associationUid

init {
updateCaches({ _, _ -> }, { _, _ -> })
Expand Down Expand Up @@ -94,8 +95,9 @@ class ReceiptAPI(private val db: SupabaseClient, cachePath: Path) : SupabaseApi(
db.from("receipt_status").upsert(LinkedReceiptStatus(sreceipt.uid, receipt.status))

// Update the cache
if (CurrentUser.userUid != userUid) {
if (CurrentUser.userUid != userUid || CurrentUser.associationUid != associationUid) {
// Invalidate cache if the user changed
associationUid = null
userReceipts = null
receipts = null
}
Expand Down Expand Up @@ -130,10 +132,16 @@ class ReceiptAPI(private val db: SupabaseClient, cachePath: Path) : SupabaseApi(
tryAsync(onFailure) {
userReceipts =
db.from("receipt")
.select(columns) { filter { SupabaseReceipt::userId eq CurrentUser.userUid } }
.select(columns) {
filter {
SupabaseReceipt::userId eq CurrentUser.userUid
SupabaseReceipt::associationId eq CurrentUser.associationUid
}
}
.decodeList<SupabaseReceipt>()
.map { it.toReceipt() }
userUid = CurrentUser.userUid
associationUid = CurrentUser.associationUid

onSuccess(userReceipts!!)
}
Expand All @@ -142,8 +150,14 @@ class ReceiptAPI(private val db: SupabaseClient, cachePath: Path) : SupabaseApi(
private fun updateCache(onSuccess: (List<Receipt>) -> Unit, onFailure: (Exception) -> Unit) {
tryAsync(onFailure) {
receipts =
db.from("receipt").select(columns).decodeList<SupabaseReceipt>().map { it.toReceipt() }

db.from("receipt")
.select(columns) {
filter { SupabaseReceipt::associationId eq CurrentUser.associationUid }
}
.decodeList<SupabaseReceipt>()
.map { it.toReceipt() }
userUid = CurrentUser.userUid
associationUid = CurrentUser.associationUid
onSuccess(receipts!!)
}
}
Expand Down Expand Up @@ -175,7 +189,9 @@ class ReceiptAPI(private val db: SupabaseClient, cachePath: Path) : SupabaseApi(
* @param onFailure called when the fetch fails with the exception that occurred
*/
fun getUserReceipts(onSuccess: (List<Receipt>) -> Unit, onFailure: (Exception) -> Unit) {
if (userReceipts != null && userUid == CurrentUser.userUid) {
if (userReceipts != null &&
userUid == CurrentUser.userUid &&
associationUid == CurrentUser.associationUid) {
onSuccess(userReceipts!!)
return
}
Expand All @@ -196,7 +212,9 @@ class ReceiptAPI(private val db: SupabaseClient, cachePath: Path) : SupabaseApi(
?: onFailure(Exception("Receipt not found"))
}

if (receipts != null && userUid == CurrentUser.userUid) {
if (receipts != null &&
userUid == CurrentUser.userUid &&
associationUid == CurrentUser.associationUid) {
getReceiptFromCache(receipts)
return
}
Expand All @@ -211,7 +229,9 @@ class ReceiptAPI(private val db: SupabaseClient, cachePath: Path) : SupabaseApi(
* @param onFailure called whenever an error has occurred with the exception that occurred
*/
fun getAllReceipts(onSuccess: (List<Receipt>) -> Unit, onFailure: (Exception) -> Unit) {
if (receipts != null && userUid == CurrentUser.userUid) {
if (receipts != null &&
userUid == CurrentUser.userUid &&
associationUid == CurrentUser.associationUid) {
onSuccess(receipts!!)
return
}
Expand Down

0 comments on commit 8aa2867

Please sign in to comment.