Updating othet subscription part when adding a subscription

This commit is contained in:
Eric van der Vlist 2023-03-02 14:26:34 +01:00
parent fe8ca05fd4
commit 12ae68f45e
1 changed files with 27 additions and 3 deletions

View File

@ -18,6 +18,7 @@ import { del } from '~/lib/del';
import { isFunction } from 'lodash'; import { isFunction } from 'lodash';
import { userId } from '~/lib/user-id'; import { userId } from '~/lib/user-id';
import { userExists } from '~/lib/user-exists'; import { userExists } from '~/lib/user-exists';
import { update } from '~/lib/update';
interface Props { interface Props {
values?: () => any; values?: () => any;
@ -47,13 +48,19 @@ const User: Component<Props> = (props) => {
values, values,
context, context,
}); });
if (!database) {
return;
}
const { username } = values;
setProgress(0); setProgress(0);
const id = getValues()?._id ?? userId(values.username, values.database); const id = getValues()?._id ?? userId(username, values.database);
await put(id, values, isNew()); await put(id, values, isNew());
const couchUserId = `org.couchdb.user:${values.username}`; const couchUserId = `org.couchdb.user:${username}`;
const userDoc = { const userDoc = {
_id: couchUserId, _id: couchUserId,
name: values.username, name: username,
password: values.password, password: values.password,
type: 'user', type: 'user',
roles: [], roles: [],
@ -77,9 +84,26 @@ const User: Component<Props> = (props) => {
return false; return false;
}; };
const addSubscriptionFactory =
(username: string) => (userDocument: any) => {
const subscriptions = userDocument.subscriptions || [];
subscriptions.push({ username, direction: '' });
userDocument.subscriptions = subscriptions;
return { username, ...userDocument };
};
const defaultUserDocument = {
database,
};
for (let i = 0; i < updatedSubscriptions.length; i++) { for (let i = 0; i < updatedSubscriptions.length; i++) {
let subscription = updatedSubscriptions[i]; let subscription = updatedSubscriptions[i];
if (!isIn(subscription.username, subscriptions)) { if (!isIn(subscription.username, subscriptions)) {
update(
userId(subscription.username, database),
addSubscriptionFactory(username),
defaultUserDocument
);
console.log({ console.log({
caller: 'User / submitHandler / new subscription', caller: 'User / submitHandler / new subscription',
username: subscription.username, username: subscription.username,