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