Updating other subscription part when deleting a subscription

This commit is contained in:
Eric van der Vlist 2023-03-02 16:39:17 +01:00
parent 12bbc525dc
commit f7a8d6ad9a
1 changed files with 14 additions and 1 deletions

View File

@ -91,6 +91,14 @@ const User: Component<Props> = (props) => {
return { username, ...userDocument };
};
const removeSubscription = (userDocument: any) => {
const subscriptions = userDocument.subscriptions || [];
userDocument.subscriptions = subscriptions.filter(
(subscription: any) => subscription.username !== username
);
return { username, ...userDocument };
};
const defaultUserDocument = {
database,
};
@ -98,7 +106,7 @@ const User: Component<Props> = (props) => {
for (let i = 0; i < updatedSubscriptions.length; i++) {
let subscription = updatedSubscriptions[i];
if (!isIn(subscription.username, subscriptions)) {
update(
await update(
userId(subscription.username, database),
addSubscription,
defaultUserDocument
@ -115,6 +123,11 @@ const User: Component<Props> = (props) => {
for (let i = 0; i < subscriptions.length; i++) {
let subscription = subscriptions[i];
if (!isIn(subscription.username, updatedSubscriptions)) {
await update(
userId(subscription.username, database),
removeSubscription,
defaultUserDocument
);
console.log({
caller: 'User / submitHandler / deleted subscription',
username: subscription.username,