Skip to content

Commit

Permalink
Update frontend to use generated openapi calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mareklibra committed Nov 27, 2023
1 parent a503afa commit 4ceb7aa
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 249 deletions.
194 changes: 97 additions & 97 deletions plugins/notifications-common/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,100 @@
export type NotificationAction = {
id: string; // UUID
title: string;
url: string;
};

/**
* Basic object representing a notification.
*/
export type Notification = {
id: string; // UUID
created: Date;
readByUser: boolean;
isSystem: boolean;

origin: string;
title: string;
message?: string;
topic?: string;

actions: NotificationAction[];
};

/**
* Input data for the POST request (create a notification).
*/
export type CreateNotificationRequest = {
origin: string;
title: string;
message?: string;
actions?: { title: string; url: string }[];
topic?: string;
targetUsers?: string[];
targetGroups?: string[];
};

export type NotificationsFilterRequest = {
/**
* Filter notifications whose either title or message contains the provided string.
*/
containsText?: string;

/**
* Only notifications created after this timestamp will be included.
*/
createdAfter?: Date;

/**
* See MessageScopes
* Default: DefaultMessageScope
*/
messageScope?: string;

/**
* The user the query is executed for. Default: DefaultUser
* Its entity must be present in the catalog.
* Conforms IdentityApi.getBackstageIdentity()
*/
user?: string;
/**
* 'false' for user's unread messages, 'true' for read ones.
* If undefined, then both marks.
*/
read?: string;
};

/**
* How the result set is sorted.
*/
export type NotificationsSortingRequest = {
fieldName?: string;
direction?: string;
};

export type NotificationsOrderByFieldsType =
| 'title'
| 'message'
| 'created'
| 'topic'
| 'origin';

export const NotificationsOrderByFields: string[] = [
'title',
'message',
'created',
'topic',
'origin',
];

export type NotificationsOrderByDirectionsType = 'asc' | 'desc';

export const NotificationsOrderByDirections: string[] = ['asc', 'desc'];

export type NotificationsQuerySorting = {
fieldName: NotificationsOrderByFieldsType;
direction: NotificationsOrderByDirectionsType;
};
// export type NotificationAction = {
// id: string; // UUID
// title: string;
// url: string;
// };

// /**
// * Basic object representing a notification.
// */
// export type Notification = {
// id: string; // UUID
// created: Date;
// readByUser: boolean;
// isSystem: boolean;

// origin: string;
// title: string;
// message?: string;
// topic?: string;

// actions: NotificationAction[];
// };

// /**
// * Input data for the POST request (create a notification).
// */
// export type CreateNotificationRequest = {
// origin: string;
// title: string;
// message?: string;
// actions?: { title: string; url: string }[];
// topic?: string;
// targetUsers?: string[];
// targetGroups?: string[];
// };

// export type NotificationsFilterRequest = {
// /**
// * Filter notifications whose either title or message contains the provided string.
// */
// containsText?: string;

// /**
// * Only notifications created after this timestamp will be included.
// */
// createdAfter?: Date;

// /**
// * See MessageScopes
// * Default: DefaultMessageScope
// */
// messageScope?: string;

// /**
// * The user the query is executed for. Default: DefaultUser
// * Its entity must be present in the catalog.
// * Conforms IdentityApi.getBackstageIdentity()
// */
// user?: string;
// /**
// * 'false' for user's unread messages, 'true' for read ones.
// * If undefined, then both marks.
// */
// read?: string;
// };

// /**
// * How the result set is sorted.
// */
// export type NotificationsSortingRequest = {
// fieldName?: string;
// direction?: string;
// };

// export type NotificationsOrderByFieldsType =
// | 'title'
// | 'message'
// | 'created'
// | 'topic'
// | 'origin';

// export const NotificationsOrderByFields: string[] = [
// 'title',
// 'message',
// 'created',
// 'topic',
// 'origin',
// ];

// export type NotificationsOrderByDirectionsType = 'asc' | 'desc';

// export const NotificationsOrderByDirections: string[] = ['asc', 'desc'];

// export type NotificationsQuerySorting = {
// fieldName: NotificationsOrderByFieldsType;
// direction: NotificationsOrderByDirectionsType;
// };

/**
* MessageScopes
Expand Down
128 changes: 26 additions & 102 deletions plugins/notifications-frontend/src/api/NotificationsApiImpl.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { ConfigApi, IdentityApi } from '@backstage/core-plugin-api';
import { IdentityApi } from '@backstage/core-plugin-api';

import {
CreateNotificationRequest,
CreateBody,
DefaultConfig,
GetNotificationsRequest,
Notification,
} from '@backstage/plugin-notifications-common';

NotificationsApi as NotificationsOpenApi,
} from '../openapi';
import {
NotificationMarkAsRead,
NotificationsApi,
NotificationsCountQuery,
NotificationsFilter,
NotificationsQuery,
} from './notificationsApi';

export type NotificationsApiOptions = {
configApi: ConfigApi;
identityApi: IdentityApi;
};

export class NotificationsApiImpl implements NotificationsApi {
private readonly backendUrl: string;
private readonly identityApi: IdentityApi;
private readonly backendRestApi: NotificationsOpenApi;

constructor(options: NotificationsApiOptions) {
this.backendUrl = options.configApi.getString('backend.baseUrl');
this.identityApi = options.identityApi;

const configuration = DefaultConfig;
this.backendRestApi = new NotificationsOpenApi(configuration);
}

private async getLogedInUsername(): Promise<string> {
Expand All @@ -34,110 +36,32 @@ export class NotificationsApiImpl implements NotificationsApi {
return userEntityRef.slice('start:'.length - 1);
}

private addFilter(url: URL, user: string, filter: NotificationsFilter) {
url.searchParams.append('user', user);

if (filter.containsText) {
url.searchParams.append('containsText', filter.containsText);
}
if (filter.createdAfter) {
url.searchParams.append(
'createdAfter',
filter.createdAfter.toISOString(),
);
}
if (filter.messageScope) {
url.searchParams.append('messageScope', filter.messageScope);
}
if (filter.isRead !== undefined) {
url.searchParams.append('read', filter.isRead ? 'true' : 'false');
}
}

async post(notification: CreateNotificationRequest): Promise<string> {
const url = new URL(`${this.backendUrl}/api/notifications/notifications`);

const response = await fetch(url.href, {
method: 'POST',
body: JSON.stringify(notification),
headers: { 'Content-Type': 'application/json' },
async createNotification(notification: CreateBody): Promise<string> {
const data = await this.backendRestApi.createNotification({
createBody: notification,
});
const data = await response.json();
if (response.status !== 200 && response.status !== 201) {
throw new Error(data.message || data.error?.message);
}

return Promise.resolve(data.messageId);
return data.messageId;
}

async getNotifications(query: NotificationsQuery): Promise<Notification[]> {
const url = new URL(`${this.backendUrl}/api/notifications/notifications`);
async getNotifications(
query: GetNotificationsRequest,
): Promise<Notification[]> {
const user = await this.getLogedInUsername();

url.searchParams.append('pageSize', `${query.pageSize}`);
url.searchParams.append('pageNumber', `${query.pageNumber}`);

if (query.sorting) {
url.searchParams.append('orderBy', `${query.sorting.fieldName}`);
url.searchParams.append('orderByDirec', `${query.sorting.direction}`);
}

this.addFilter(url, user, query);

const response = await fetch(url.href);
const data = await response.json();
if (response.status !== 200 && response.status !== 201) {
throw new Error(data.message);
}

if (!Array.isArray(data)) {
throw new Error('Unexpected format of notifications received');
}

return data;
return this.backendRestApi.getNotifications({ ...query, user });
}

async getNotificationsCount(query: NotificationsCountQuery): Promise<number> {
const url = new URL(
`${this.backendUrl}/api/notifications/notifications/count`,
);
const user = await this.getLogedInUsername();

this.addFilter(url, user, query);

const response = await fetch(url.href);
const data = await response.json();
if (response.status !== 200 && response.status !== 201) {
throw new Error(data.message);
}

const count = parseInt(data.count, 10);
if (Number.isNaN(count)) {
throw new Error('Unexpected format of notifications count received');
}

return count;
const data = await this.backendRestApi.getNotificationsCount({
...query,
user,
});
return data.count;
}

async markAsRead({
notificationId,
isRead,
}: NotificationMarkAsRead): Promise<void> {
const url = new URL(
`${this.backendUrl}/api/notifications/notifications/read`,
);
async markAsRead(params: NotificationMarkAsRead): Promise<void> {
const user = await this.getLogedInUsername();

url.searchParams.append('read', isRead ? 'true' : 'false');
url.searchParams.append('user', user);
url.searchParams.append('messageId', notificationId);

const response = await fetch(url.href, {
method: 'PUT',
});

if (response.status !== 200 && response.status !== 201) {
throw new Error('Failed to mark the message as read');
}
return this.backendRestApi.setRead({ ...params, user });
}
}
Loading

0 comments on commit 4ceb7aa

Please sign in to comment.