Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link receipts to the current association #384

Merged
merged 1 commit into from
May 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ReceiptAPI(private val db: SupabaseClient, private val cachePath: Path) :
private var receipts: List<Receipt>? = null
/// The user's UID
private var userUid: String? = CurrentUser.userUid
private var associationUid: String? = CurrentUser.associationUid
sarahbadr17 marked this conversation as resolved.
Show resolved Hide resolved

init {
updateCaches({ _, _ -> }, { _, _ -> })
Expand Down Expand Up @@ -101,8 +102,9 @@ class ReceiptAPI(private val db: SupabaseClient, private val cachePath: Path) :
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
sarahbadr17 marked this conversation as resolved.
Show resolved Hide resolved
associationUid = null
userReceipts = null
receipts = null
}
Expand Down Expand Up @@ -159,10 +161,16 @@ class ReceiptAPI(private val db: SupabaseClient, private val cachePath: Path) :
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 @@ -171,8 +179,14 @@ class ReceiptAPI(private val db: SupabaseClient, private val cachePath: Path) :
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
sarahbadr17 marked this conversation as resolved.
Show resolved Hide resolved
onSuccess(receipts!!)
}
}
Expand Down Expand Up @@ -204,7 +218,9 @@ class ReceiptAPI(private val db: SupabaseClient, private val cachePath: Path) :
* @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 @@ -225,7 +241,9 @@ class ReceiptAPI(private val db: SupabaseClient, private val cachePath: Path) :
?: 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 @@ -240,7 +258,9 @@ class ReceiptAPI(private val db: SupabaseClient, private val cachePath: Path) :
* @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
Loading