diff --git a/src/index.ts b/src/index.ts index f64b3614..1ce715c9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,6 +30,7 @@ import { SystemModules, Follow, People, + Notifications, } from './modules'; import { setContext } from '@apollo/client/link/context'; @@ -114,7 +115,7 @@ export default class KanvasCore { public companiesBranches: CompaniesBranches; public follow: Follow; public people: People; - + public notifications: Notifications; constructor(protected options: Options) { this.client = new ApolloClient({ link: this.generateLink(), @@ -145,6 +146,7 @@ export default class KanvasCore { this.companiesBranches = new CompaniesBranches(this.client); this.follow = new Follow(this.client); this.people = new People(this.client); + this.notifications = new Notifications(this.client); } protected generateURL() { return new HttpLink({ uri: this.options.url }); diff --git a/src/modules/index.ts b/src/modules/index.ts index 963b050f..c355d613 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -19,3 +19,4 @@ export * from './companies'; export * from './companies-branches'; export * from './follows'; export * from './people'; +export * from './notifications'; \ No newline at end of file diff --git a/src/modules/notifications/index.ts b/src/modules/notifications/index.ts new file mode 100644 index 00000000..18c04e6d --- /dev/null +++ b/src/modules/notifications/index.ts @@ -0,0 +1,97 @@ +import { ClientType } from 'index'; + +import { + NOTIFICATION_QUERY, + NOTIFICATION_TYPE_QUERY, + NOTIFICATION_CHANNEL_QUERY, +} from '../../queries'; + +import { + NotificationInterface, + NotificationTypeInterface, + NotificationChannelInterface, + NotificationEntityFilterInputInterface, + NotificationTypeFilterInputInterface, + WhereCondition, +} from '../../types'; + +import { + READ_ALL_NOTIFICATIONS_MUTATION, + SEND_NOTIFICATION_BASE_TEMPLATE_MUTATION, + SEND_NOTIFICATION_BY_MESSAGE_MUTATION, +} from '../../mutations'; + +export class Notifications { + constructor(protected client: ClientType) {} + + public async getNotifications( + where: WhereCondition, + whereEntity: NotificationEntityFilterInputInterface, + whereType: NotificationTypeFilterInputInterface, + first: number, + page: number + ): Promise { + const response = await this.client.query({ + query: NOTIFICATION_QUERY, + variables: { where, first, page, whereEntity, whereType }, + }); + return response.data.notifications.data as NotificationInterface[]; + } + + public async getNotificationTypes( + where: WhereCondition, + first: number, + page: number + ): Promise { + const response = await this.client.query({ + query: NOTIFICATION_TYPE_QUERY, + variables: { where, first, page }, + }); + return response.data.notificationTypes.data as NotificationTypeInterface[]; + } + + public async getNotificationChannels( + where: WhereCondition, + first: number, + page: number + ): Promise { + const response = await this.client.query({ + query: NOTIFICATION_CHANNEL_QUERY, + variables: { where, first, page }, + }); + return response.data.notificationChannels + .data as NotificationChannelInterface[]; + } + + public async readAllNotifications(): Promise { + const response = await this.client.mutate({ + mutation: READ_ALL_NOTIFICATIONS_MUTATION, + }); + return response.data.readAllNotifications as boolean; + } + + public async sendNotificationBaseTemplate( + template_name: string, + data: any, + via: string[], + users_id: number[] + ): Promise { + const response = await this.client.mutate({ + mutation: SEND_NOTIFICATION_BASE_TEMPLATE_MUTATION, + variables: { template_name, data, via, users_id }, + }); + return response.data.sendNotificationBaseTemplate as boolean; + } + + public async sendNotificationByMessage( + message: string, + via: string[], + users_id: number[] + ): Promise { + const response = await this.client.mutate({ + mutation: SEND_NOTIFICATION_BY_MESSAGE_MUTATION, + variables: { message, via, users_id }, + }); + return response.data.sendNotificationByMessage as boolean; + } +} diff --git a/src/mutations/index.ts b/src/mutations/index.ts index ba7319e8..a4909202 100644 --- a/src/mutations/index.ts +++ b/src/mutations/index.ts @@ -18,4 +18,5 @@ export * from './companies-branches.mutation'; export * from './follows.mutation'; export * from './people.mutation'; export * from './message-comment.mutation'; -export * from './settings.mutation'; \ No newline at end of file +export * from './notification.mutation'; +export * from './settings.mutation'; diff --git a/src/mutations/notification.mutation.ts b/src/mutations/notification.mutation.ts new file mode 100644 index 00000000..a1c6c75f --- /dev/null +++ b/src/mutations/notification.mutation.ts @@ -0,0 +1,40 @@ +import { gql } from '@apollo/client/core'; + +export const READ_ALL_NOTIFICATIONS_MUTATION = gql` + mutation readAllNotifications { + readAllNotifications + } +`; + +export const SEND_NOTIFICATION_BASE_TEMPLATE_MUTATION = gql` + mutation sendNotificationBaseTemplate( + $template_name: String! + $data: Mixed! + $via: [String!]! + $users_id: [Int!]! + ) { + sendNotificationBaseTemplate( + template_name: $template_name + data: $data + via: $via + users_id: $users_id + ) + } +`; + +export const SEND_NOTIFICATION_BY_MESSAGE_MUTATION = gql` + mutation sendNotificationByMessage( + $message: String! + $via: [String!]! + $users_id: [Int!]! + ) { + sendNotificationByMessage( + message: $message + via: $via + users_id: $users_id + ) { + sent + message + } + } +`; diff --git a/src/queries/index.ts b/src/queries/index.ts index a7506e27..e333d5bd 100644 --- a/src/queries/index.ts +++ b/src/queries/index.ts @@ -18,3 +18,4 @@ export * from './companies.query'; export * from './follows.query'; export * from './people.query'; export * from './message-comment.query'; +export * from './notification.query'; diff --git a/src/queries/notification.query.ts b/src/queries/notification.query.ts new file mode 100644 index 00000000..a7bdb943 --- /dev/null +++ b/src/queries/notification.query.ts @@ -0,0 +1,156 @@ +import { gql } from '@apollo/client/core'; + +export const NOTIFICATION_QUERY = gql` + query notifications( + $where: QueryNotificationsWhereWhereConditions + $whereEntity: NotificationEntityFilterInput + $whereType: NotificationTypeFilterInput + $orderBy: [QueryNotificationsOrderByOrderByClause!] + $first: Int + $page: Int + ) { + notifications( + where: $where + whereEntity: $whereEntity + whereType: $whereType + orderBy: $orderBy + first: $first + page: $page + ) { + data { + id + users { + id + displayname + email + } + fromUsers { + id + displayname + email + } + companies { + id + name + } + systemModule { + id + name + slug + } + types { + id + name + key + verb + event + description + template + weight + is_published + } + entity_id + entity + content + read + content_group + created_at + updated_at + } + paginatorInfo { + total + currentPage + hasMorePages + lastPage + count + } + } + } +`; + +export const NOTIFICATION_TYPE_QUERY = gql` + query notificationTypes( + $where: QueryNotificationTypesWhereWhereConditions + $orderBy: [QueryNotificationTypesOrderByOrderByClause!] + $first: Int + $page: Int + ) { + notificationTypes( + where: $where + orderBy: $orderBy + first: $first + page: $page + ) { + data { + id + systemModule { + id + name + slug + } + parent { + id + name + key + verb + event + description + template + weight + is_published + } + channel { + id + name + slug + } + name + key + verb + event + description + template + weight + is_published + created_at + updated_at + } + paginatorInfo { + total + currentPage + hasMorePages + lastPage + count + } + } + } +`; + +export const NOTIFICATION_CHANNEL_QUERY = gql` + query notificationChannels( + $where: QueryNotificationChannelsWhereWhereConditions + $orderBy: [QueryNotificationChannelsOrderByOrderByClause!] + $first: Int + $page: Int + ) { + notificationChannels( + where: $where + orderBy: $orderBy + first: $first + page: $page + ) { + data { + id + name + slug + } + paginatorInfo { + total + currentPage + hasMorePages + lastPage + count + } + } + } +`; diff --git a/src/types/index.ts b/src/types/index.ts index 839e8a2d..3430a07c 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -24,3 +24,4 @@ export * from './follows'; export * from './people'; export * from './contact'; export * from './message-comment'; +export * from './notification'; diff --git a/src/types/notification.ts b/src/types/notification.ts new file mode 100644 index 00000000..5ae93875 --- /dev/null +++ b/src/types/notification.ts @@ -0,0 +1,57 @@ +import { UserInterface } from './users'; +import { CompanyInterface } from './companies'; +import { SystemModuleInterface } from './system-module'; + +export interface NotificationInterface { + id: string; + users: UserInterface; + fromUsers: UserInterface; + companies: CompanyInterface; + systemModule: SystemModuleInterface; + types: NotificationTypeInterface; + entity_id: string; + entity?: any; + content?: string; + read: number; + content_group?: string; + created_at: string; + updated_at?: string; +} + +export interface NotificationTypeInterface { + id: string; + systemModule: SystemModuleInterface; + parent?: NotificationTypeInterface; + channel: NotificationChannelInterface; + name: string; + key: string; + verb?: string; + event?: string; + description?: string; + template?: string; + weight: number; + is_published: number; + created_at: string; + updated_at?: string; +} + +export interface NotificationChannelInterface { + id: string; + name: string; + slug: string; +} + +export interface NotificationEntityFilterInputInterface { + nested_key: string; + value: string; +} + +export interface NotificationTypeFilterInputInterface { + verb: string; + event: string; +} + +export interface NotificationMessageResponseInterface { + sent: boolean; + message: string; +}