diff --git a/src/components/user/User.tsx b/src/components/user/User.tsx index 1298db2..6b709bc 100644 --- a/src/components/user/User.tsx +++ b/src/components/user/User.tsx @@ -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) => { 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) => { 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,