mirror of
https://github.com/hcengineering/platform.git
synced 2024-11-22 21:50:34 +03:00
Send invite when employee created (#2803)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
parent
ad6eb3a84b
commit
60e3809a46
@ -15,8 +15,10 @@
|
||||
<script lang="ts">
|
||||
import { Channel, combineName, Employee, findPerson, Person } from '@hcengineering/contact'
|
||||
import core, { AccountRole, AttachedData, Data, generateId, Ref } from '@hcengineering/core'
|
||||
import login from '@hcengineering/login'
|
||||
import { getResource } from '@hcengineering/platform'
|
||||
import { Card, getClient } from '@hcengineering/presentation'
|
||||
import { EditBox, IconInfo, Label, createFocusManager, FocusHandler } from '@hcengineering/ui'
|
||||
import { createFocusManager, EditBox, FocusHandler, IconInfo, Label } from '@hcengineering/ui'
|
||||
import { createEventDispatcher } from 'svelte'
|
||||
import { ChannelsDropdown } from '..'
|
||||
import contact from '../plugin'
|
||||
@ -64,6 +66,9 @@
|
||||
role: AccountRole.User
|
||||
})
|
||||
|
||||
const sendInvite = await getResource(login.function.SendInvite)
|
||||
await sendInvite(email.trim())
|
||||
|
||||
for (const channel of channels) {
|
||||
await client.addCollection(contact.class.Channel, contact.space.Contacts, id, contact.class.Person, 'channels', {
|
||||
value: channel.value,
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
import InviteLink from './components/InviteLink.svelte'
|
||||
import LoginApp from './components/LoginApp.svelte'
|
||||
import { changeName, changePassword, getWorkspaces, leaveWorkspace, selectWorkspace } from './utils'
|
||||
import { changeName, changePassword, getWorkspaces, leaveWorkspace, selectWorkspace, sendInvite } from './utils'
|
||||
/*!
|
||||
* Anticrm Platform™ Login Plugin
|
||||
* © 2020, 2021 Anticrm Platform Contributors.
|
||||
@ -34,7 +34,8 @@ export default async () => ({
|
||||
LeaveWorkspace: leaveWorkspace,
|
||||
ChangePassword: changePassword,
|
||||
SelectWorkspace: selectWorkspace,
|
||||
GetWorkspaces: getWorkspaces
|
||||
GetWorkspaces: getWorkspaces,
|
||||
SendInvite: sendInvite
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -21,20 +21,20 @@ import {
|
||||
Request,
|
||||
Response,
|
||||
serialize,
|
||||
setMetadata,
|
||||
Status,
|
||||
unknownError,
|
||||
unknownStatus,
|
||||
setMetadata
|
||||
unknownStatus
|
||||
} from '@hcengineering/platform'
|
||||
import presentation from '@hcengineering/presentation'
|
||||
import {
|
||||
fetchMetadataLocalStorage,
|
||||
getCurrentLocation,
|
||||
Location,
|
||||
navigate,
|
||||
setMetadataLocalStorage,
|
||||
Location
|
||||
setMetadataLocalStorage
|
||||
} from '@hcengineering/ui'
|
||||
import { workbenchId } from '@hcengineering/workbench'
|
||||
import presentation from '@hcengineering/presentation'
|
||||
|
||||
const DEV_WORKSPACE = 'DEV WORKSPACE'
|
||||
|
||||
@ -555,6 +555,37 @@ export async function leaveWorkspace (email: string): Promise<void> {
|
||||
})
|
||||
}
|
||||
|
||||
export async function sendInvite (email: string): Promise<void> {
|
||||
const accountsUrl = getMetadata(login.metadata.AccountsUrl)
|
||||
|
||||
if (accountsUrl === undefined) {
|
||||
throw new Error('accounts url not specified')
|
||||
}
|
||||
|
||||
const overrideToken = getMetadata(login.metadata.OverrideLoginToken)
|
||||
if (overrideToken !== undefined) {
|
||||
const endpoint = getMetadata(login.metadata.OverrideEndpoint)
|
||||
if (endpoint !== undefined) {
|
||||
return
|
||||
}
|
||||
}
|
||||
const token = getMetadata(presentation.metadata.Token) as string
|
||||
|
||||
const request: Request<[string]> = {
|
||||
method: 'sendInvite',
|
||||
params: [email]
|
||||
}
|
||||
|
||||
await fetch(accountsUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: 'Bearer ' + token,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: serialize(request)
|
||||
})
|
||||
}
|
||||
|
||||
export async function requestPassword (email: string): Promise<Status> {
|
||||
const accountsUrl = getMetadata(login.metadata.AccountsUrl)
|
||||
|
||||
|
@ -69,6 +69,7 @@ export default plugin(loginId, {
|
||||
},
|
||||
function: {
|
||||
ChangeName: '' as Resource<(first: string, last: string) => Promise<void>>,
|
||||
SendInvite: '' as Resource<(email: string) => Promise<void>>,
|
||||
LeaveWorkspace: '' as Resource<(email: string) => Promise<void>>,
|
||||
ChangePassword: '' as Resource<(oldPassword: string, password: string) => Promise<void>>,
|
||||
SelectWorkspace: '' as Resource<(workspace: string) => Promise<[Status, WorkspaceLoginInfo | undefined]>>,
|
||||
|
@ -887,6 +887,64 @@ export async function leaveWorkspace (db: Db, productId: string, token: string,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @public
|
||||
*/
|
||||
export async function sendInvite (db: Db, productId: string, token: string, email: string): Promise<void> {
|
||||
const tokenData = decodeToken(token)
|
||||
const currentAccount = await getAccount(db, tokenData.email)
|
||||
if (currentAccount === null) {
|
||||
throw new PlatformError(
|
||||
new Status(Severity.ERROR, accountPlugin.status.AccountNotFound, { account: tokenData.email })
|
||||
)
|
||||
}
|
||||
|
||||
const workspace = await getWorkspace(db, productId, tokenData.workspace.name)
|
||||
if (workspace === null) {
|
||||
throw new PlatformError(
|
||||
new Status(Severity.ERROR, accountPlugin.status.WorkspaceNotFound, { workspace: tokenData.workspace.name })
|
||||
)
|
||||
}
|
||||
|
||||
const account = await getAccount(db, email)
|
||||
if (account !== null) return
|
||||
|
||||
const sesURL = getMetadata(accountPlugin.metadata.SES_URL)
|
||||
if (sesURL === undefined || sesURL === '') {
|
||||
throw new Error('Please provide email service url')
|
||||
}
|
||||
const front = getMetadata(accountPlugin.metadata.FrontURL)
|
||||
if (front === undefined || front === '') {
|
||||
throw new Error('Please provide front url')
|
||||
}
|
||||
|
||||
const expHours = 48
|
||||
const exp = expHours * 60 * 60 * 1000
|
||||
|
||||
const inviteId = await getInviteLink(db, productId, token, exp, email, 1)
|
||||
const link = concatLink(front, `/login/join?inviteId=${inviteId.toString()}`)
|
||||
|
||||
const text = `You was invited to ${workspace.workspace}. To join please paste the following link in your web browser's address bar: ${link}. Link valid for ${expHours} hours.`
|
||||
|
||||
const html = `<p>You was invited to ${workspace.workspace}. To join, please click the link below: <a href=${link}>Join</a></p><p>
|
||||
If the invite link above does not work, paste the following link in your web browser's address bar: ${link}
|
||||
</p><p>Link valid for ${expHours} hours.</p>`
|
||||
const subject = `Inivte to ${workspace.workspace}`
|
||||
const to = email
|
||||
await fetch(concatLink(sesURL, '/send'), {
|
||||
method: 'post',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
text,
|
||||
html,
|
||||
subject,
|
||||
to
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function deactivateEmployeeAccount (email: string, workspace: string, productId: string): Promise<void> {
|
||||
const connection = await connect(getTransactor(), getWorkspaceId(workspace, productId), email)
|
||||
try {
|
||||
@ -965,7 +1023,8 @@ export function getMethods (
|
||||
changeName: wrap(changeName),
|
||||
changePassword: wrap(changePassword),
|
||||
requestPassword: wrap(requestPassword),
|
||||
restorePassword: wrap(restorePassword)
|
||||
restorePassword: wrap(restorePassword),
|
||||
sendInvite: wrap(sendInvite)
|
||||
// updateAccount: wrap(updateAccount)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user