Skip to content

Commit

Permalink
fix(blacklist): fixes an issue where incorrect entries were being upd…
Browse files Browse the repository at this point in the history
…ated (#462)
  • Loading branch information
ssilve1989 authored Sep 13, 2024
1 parent 63da7d3 commit 13243a3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/firebase/collections/blacklist-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
EmptyError,
catchError,
concatMap,
filter,
first,
from,
lastValueFrom,
Expand Down Expand Up @@ -36,20 +37,21 @@ class BlacklistCollection {
*/
public async upsert(
guildId: string,
props: BlacklistDocument,
source: BlacklistDocument,
): Promise<BlacklistDocument> {
const { reason, ...props } = source;
const collection = this.getCollection(guildId);
const pipeline$ = this.query$(guildId, props).pipe(
mergeMap(async (result) => {
const doc = result.docs[0];
await doc.ref.update(props as Record<string, any>, {
await doc.ref.update(source as Record<string, any>, {
exists: true,
});
return await doc.ref.get();
}),
catchError(async (err) => {
if (err instanceof EmptyError) {
const doc = await collection.add(props);
const doc = await collection.add(source);
return await doc.get();
}
throw err;
Expand Down Expand Up @@ -108,6 +110,7 @@ class BlacklistCollection {
private query$(guildId: string, data: BlacklistDocumentKeys) {
const collection = this.getCollection(guildId);
return from(Object.entries(data)).pipe(
filter(([_, value]) => !!value),
// use concatMap to potentially save on query costs if one query returns successfully early
concatMap(([key, value]) =>
collection.where(key, '==', value).limit(1).get(),
Expand Down

0 comments on commit 13243a3

Please sign in to comment.