add resendInvite method to invite repo
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import { UMB_USER_STORE_CONTEXT_TOKEN, UmbUserStore } from '../user.store.js';
|
||||
import { type UmbInviteUserDataSource } from './types.js';
|
||||
import { UmbInviteUserServerDataSource } from './invite-user.server.data.js';
|
||||
import type { UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api';
|
||||
import { UmbContextConsumerController } from '@umbraco-cms/backoffice/context-api';
|
||||
import { UMB_NOTIFICATION_CONTEXT_TOKEN, UmbNotificationContext } from '@umbraco-cms/backoffice/notification';
|
||||
import { InviteUserRequestModel, UserStateModel } from '@umbraco-cms/backoffice/backend-api';
|
||||
import { InviteUserRequestModel } from '@umbraco-cms/backoffice/backend-api';
|
||||
|
||||
export class UmbInviteUserRepository {
|
||||
#host: UmbControllerHostElement;
|
||||
@@ -12,38 +11,57 @@ export class UmbInviteUserRepository {
|
||||
|
||||
#inviteSource: UmbInviteUserDataSource;
|
||||
#notificationContext?: UmbNotificationContext;
|
||||
#detailStore?: UmbUserStore;
|
||||
|
||||
constructor(host: UmbControllerHostElement) {
|
||||
this.#host = host;
|
||||
this.#inviteSource = new UmbInviteUserServerDataSource(this.#host);
|
||||
|
||||
this.#init = Promise.all([
|
||||
new UmbContextConsumerController(this.#host, UMB_USER_STORE_CONTEXT_TOKEN, (instance) => {
|
||||
this.#detailStore = instance;
|
||||
}).asPromise(),
|
||||
|
||||
new UmbContextConsumerController(this.#host, UMB_NOTIFICATION_CONTEXT_TOKEN, (instance) => {
|
||||
this.#notificationContext = instance;
|
||||
}).asPromise(),
|
||||
]);
|
||||
}
|
||||
|
||||
async invite(data: InviteUserRequestModel) {
|
||||
if (!data) throw new Error('data is missing');
|
||||
/**
|
||||
* Invites a user
|
||||
* @param {InviteUserRequestModel} requestModel
|
||||
* @return {*}
|
||||
* @memberof UmbInviteUserRepository
|
||||
*/
|
||||
async invite(requestModel: InviteUserRequestModel) {
|
||||
if (!requestModel) throw new Error('data is missing');
|
||||
await this.#init;
|
||||
|
||||
const { error } = await this.#inviteSource.invite(ids);
|
||||
const { error } = await this.#inviteSource.invite(requestModel);
|
||||
|
||||
if (!error) {
|
||||
ids.forEach((id) => {
|
||||
this.#detailStore?.updateItem(id, { state: UserStateModel.DISABLED });
|
||||
});
|
||||
|
||||
const notification = { data: { message: `User disabled` } };
|
||||
const notification = { data: { message: `Invite sent to user` } };
|
||||
this.#notificationContext?.peek('positive', notification);
|
||||
}
|
||||
|
||||
return { data, error };
|
||||
return { error };
|
||||
}
|
||||
|
||||
/**
|
||||
* Resend an invite to a user
|
||||
* @param {string} userId
|
||||
* @param {InviteUserRequestModel} requestModel
|
||||
* @return {*}
|
||||
* @memberof UmbInviteUserRepository
|
||||
*/
|
||||
async resendInvite(userId: string, requestModel: any) {
|
||||
if (!userId) throw new Error('User id is missing');
|
||||
if (!requestModel) throw new Error('data is missing');
|
||||
await this.#init;
|
||||
|
||||
const { error } = await this.#inviteSource.resendInvite(userId, requestModel);
|
||||
|
||||
if (!error) {
|
||||
const notification = { data: { message: `Invite resent to user` } };
|
||||
this.#notificationContext?.peek('positive', notification);
|
||||
}
|
||||
|
||||
return { error };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { type UmbInviteUserDataSource } from './types.js';
|
||||
import { InviteUserRequestModel, UserResource } from '@umbraco-cms/backoffice/backend-api';
|
||||
import { ApiError, InviteUserRequestModel, UserResource } from '@umbraco-cms/backoffice/backend-api';
|
||||
import type { UmbControllerHostElement } from '@umbraco-cms/backoffice/controller-api';
|
||||
import { UmbDataSourceErrorResponse } from '@umbraco-cms/backoffice/repository';
|
||||
import { tryExecuteAndNotify } from '@umbraco-cms/backoffice/resources';
|
||||
|
||||
/**
|
||||
@@ -20,14 +21,50 @@ export class UmbInviteUserServerDataSource implements UmbInviteUserDataSource {
|
||||
this.#host = host;
|
||||
}
|
||||
|
||||
async invite(userId: string, requestBody: InviteUserRequestModel) {
|
||||
if (!userId) throw new Error('User id is missing');
|
||||
/**
|
||||
* Invites a user
|
||||
* @param {InviteUserRequestModel} requestModel
|
||||
* @returns
|
||||
* @memberof UmbInviteUserServerDataSource
|
||||
*/
|
||||
async invite(requestModel: InviteUserRequestModel) {
|
||||
if (!requestModel) throw new Error('Data is missing');
|
||||
|
||||
return tryExecuteAndNotify(
|
||||
this.#host,
|
||||
UserResource.postUserInvite({
|
||||
requestBody,
|
||||
requestBody: requestModel,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resend an invite to a user
|
||||
* @param {string} userId
|
||||
* @param {InviteUserRequestModel} requestModel
|
||||
* @returns
|
||||
* @memberof UmbInviteUserServerDataSource
|
||||
*/
|
||||
async resendInvite(userId: string, requestModel: InviteUserRequestModel) {
|
||||
if (!userId) throw new Error('User id is missing');
|
||||
if (!requestModel) throw new Error('Data is missing');
|
||||
|
||||
alert('End point is missing');
|
||||
|
||||
const body = JSON.stringify({
|
||||
userId,
|
||||
requestModel,
|
||||
});
|
||||
|
||||
return tryExecuteAndNotify(
|
||||
this.#host,
|
||||
fetch('/umbraco/management/api/v1/user/invite/resend', {
|
||||
method: 'POST',
|
||||
body: body,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
}).then((res) => res.json()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,5 +2,6 @@ import { InviteUserRequestModel } from '@umbraco-cms/backoffice/backend-api';
|
||||
import { UmbDataSourceErrorResponse } from '@umbraco-cms/backoffice/repository';
|
||||
|
||||
export interface UmbInviteUserDataSource {
|
||||
invite(data: InviteUserRequestModel): Promise<UmbDataSourceErrorResponse>;
|
||||
invite(requestModel: InviteUserRequestModel): Promise<UmbDataSourceErrorResponse>;
|
||||
resendInvite(userId: string, requestModel: any): Promise<UmbDataSourceErrorResponse>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user