Skip to content

Commit

Permalink
Merge pull request #44 from alkem-io/develop
Browse files Browse the repository at this point in the history
Release: Refactoring
  • Loading branch information
valentinyanakiev authored Nov 18, 2021
2 parents f920cb1 + bbaf1e1 commit 82cdd49
Show file tree
Hide file tree
Showing 40 changed files with 405 additions and 322 deletions.
73 changes: 56 additions & 17 deletions notifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,59 @@ kratos:

recipients:
application_created:
admin:
- rule:
type: CHALLENGE_ADMIN
resource_id: <challengeID>
- rule:
type: OPPORTUNITY_ADMIN
resource_id: <opportunityID>
- rule:
type: ECOVERSE_ADMIN
resource_id: <ecoverseID>
- rule:
type: GLOBAL_ADMIN
resource_id: <>
applicant:
- rule:
type: USER_SELF_MANAGEMENT
resource_id: <applicantID>
- name: admin
rules:
- rule:
type: CHALLENGE_ADMIN
resource_id: <challengeID>
- rule:
type: OPPORTUNITY_ADMIN
resource_id: <opportunityID>
- rule:
type: ECOVERSE_ADMIN
resource_id: <ecoverseID>
- rule:
type: GLOBAL_COMMUNITY_ADMIN
resource_id: <>
- rule:
type: GLOBAL_ADMIN
resource_id: <>
- name: applicant
rules:
- rule:
type: USER_SELF_MANAGEMENT
resource_id: <applicantID>
user_registration:
- name: admin
rules:
- rule:
type: GLOBAL_COMMUNITY_ADMIN
resource_id: <>
- rule:
type: GLOBAL_ADMIN
resource_id: <>
- name: registrant
rules:
- rule:
type: USER_SELF_MANAGEMENT
resource_id: <applicantID>
communication_update:
- name: admin
rules:
- rule:
type: GLOBAL_COMMUNITY_ADMIN
resource_id: <>
- rule:
type: GLOBAL_ADMIN
resource_id: <>
- name: community_member
rules:
- rule:
type: CHALLENGE_MEMBER
resource_id: <challengeID>
- rule:
type: OPPORTUNITY_MEMBER
resource_id: <opportunityID>
- rule:
type: ECOVERSE_MEMBER
resource_id: <ecoverseID>
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alkemio-notifications",
"version": "0.3.2",
"version": "0.3.3",
"description": "Alkemio notifications service",
"author": "Cherrytwist Foundation",
"private": false,
Expand All @@ -20,7 +20,7 @@
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"start:services": "docker-compose -f quickstart-mailslurper.yml --env-file .env.docker up --build --force-recreate",
"start:services": "docker-compose -f quickstart-mailslurper.yml up --build --force-recreate",
"lint": "tsc --noEmit && eslint src/**/*.ts{,x}",
"lint:prod": "tsc --noEmit && cross-env NODE_ENV=production eslint src/**/*.ts{,x}",
"lint:fix": "tsc --noEmit && eslint src/**/*.ts{,x} --fix",
Expand Down
6 changes: 2 additions & 4 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import { HttpExceptionsFilter } from './core';
import {
ApplicationNotificationBuilder,
NotificationService,
TemplateToCredentialMapper,
AlkemioClientAdapterModule,
} from '@src/services';
import { AlkemioClientModule, NotifmeModule } from '@src/wrappers';
import { NotificationRecipientsAdapterModule } from './services/notification-recipients-adapter/notification.recipients.adapter.module';
import { AlkemioClientModule, NotifmeModule } from '@src/services/external';
import { NotificationRecipientsAdapterModule } from './services/application/notification-recipients-adapter/notification.recipients.adapter.module';

@Module({
imports: [
Expand All @@ -37,7 +36,6 @@ import { NotificationRecipientsAdapterModule } from './services/notification-rec
},
NotificationService,
ApplicationNotificationBuilder,
TemplateToCredentialMapper,
],
controllers: [AppController],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { AuthorizationCredential } from '@alkemio/client-lib';
import { ApplicationCreatedEventPayload } from '@src/types/application.created.event.payload';

export type RecipientCredential = {
role: AuthorizationCredential;
type: AuthorizationCredential;
resourceID?: string;
isAdmin: boolean;
};

export interface INotificationRecipientProvider {
getApplicationCreatedRecipients(
payload: ApplicationCreatedEventPayload
payload: ApplicationCreatedEventPayload,
roleName: string
): RecipientCredential[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import { AuthorizationCredential } from '@alkemio/client-lib';
export type TemplateRule = {
rule: {
type: AuthorizationCredential;
resource_id: string;
resource_id: string | undefined;
};
};

export type TemplateRow = {
admin: TemplateRule[];
applicant: TemplateRule[];
export type TemplateRuleSet = {
name: string;
rules: TemplateRule[];
};

export type TemplateConfig = {
application_created?: TemplateRow;
application_created?: TemplateRuleSet[];
user_registration?: TemplateRuleSet[];
communication_update?: TemplateRuleSet[];
};

export interface INotificationRecipientTemplateProvider {
Expand Down
7 changes: 2 additions & 5 deletions src/core/contracts/notified.users.provider.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import { User } from '../models';
import { AuthorizationCredential } from '@alkemio/client-lib';

export interface INotifiedUsersProvider {
getHubAdmins(hubID: string): Promise<User[]> | User[];
getChallengeAdmins(challengeID: string): Promise<User[]> | User[];
getOpportunityAdmins(opportunityID: string): Promise<User[]> | User[];
getApplicant(payload: any): Promise<User> | User;
getUsersWithCredentials(
credential: AuthorizationCredential,
getUsersMatchingCredentialCriteria(
credentialType: AuthorizationCredential,
resourceID?: string
): Promise<User[]>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { ALKEMIO_CLIENT_ADAPTER } from '@src/common';
import { AlkemioClientModule } from '../../wrappers/alkemio-client/alkemio.client.module';
import { AlkemioClientModule } from '../../external/alkemio-client/alkemio.client.module';
import { AlkemioClientAdapter } from './alkemio.client.adapter';

@Module({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,39 +61,6 @@ describe('AlkemioAdapter', () => {
});

describe('Alkemio Client Adapter', () => {
it('Should get hub admins', async () => {
jest
.spyOn(alkemioClient, 'usersWithAuthorizationCredential')
.mockResolvedValue(testData.hubAdmins);

const res = await alkemioAdapter.getHubAdmins(
testData.eventPayload.data.hub.id
);
expect(res.length === 0);
});

it('Should get opportunity admins', async () => {
jest
.spyOn(alkemioClient, 'usersWithAuthorizationCredential')
.mockResolvedValue(testData.opportunityAdmins);

const res = await alkemioAdapter.getOpportunityAdmins(
testData.eventPayload.data.hub.id
);
expect(res.length === 1);
});

it('Should get challenge admins', async () => {
jest
.spyOn(alkemioClient, 'usersWithAuthorizationCredential')
.mockResolvedValue(testData.challengeAdmins);

const res = await alkemioAdapter.getChallengeAdmins(
testData.eventPayload.data.hub.id
);
expect(res.length === 1);
});

it('Should throw an error', async () => {
jest.spyOn(alkemioClient, 'user').mockResolvedValue(undefined);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,7 @@ export class AlkemioClientAdapter
return applicationCreator;
}

async getOpportunityAdmins(opportunityID: string): Promise<User[]> {
return await this.getUsersWithCredentials(
AuthorizationCredential.OpportunityAdmin,
opportunityID
);
}

async getHubAdmins(ecoverseID: string): Promise<User[]> {
return await this.getUsersWithCredentials(
AuthorizationCredential.EcoverseAdmin,
ecoverseID
);
}

async getChallengeAdmins(challengeID: string): Promise<User[]> {
return await this.getUsersWithCredentials(
AuthorizationCredential.ChallengeAdmin,
challengeID
);
}

async getUsersWithCredentials(
async getUsersMatchingCredentialCriteria(
credential: AuthorizationCredential,
resourceID?: string
): Promise<User[]> {
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ApplicationCreatedEventPayload } from '@src/types/application.created.e
import { getResourceId, getResourceIdByRole, ruleToCredential } from './utils';

describe('ruleToCredential', () => {
it('returns undefined on unsupported role or missing data', () => {
it('returns undefined resource_id on unsupported role or missing data', () => {
const payload = {
hub: {
id: 'hub',
Expand All @@ -19,7 +19,10 @@ describe('ruleToCredential', () => {
},
payload
)
).toBeUndefined();
).toEqual({
type: AuthorizationCredential.GlobalAdminCommunity,
resource_id: undefined,
});
expect(
ruleToCredential(
{
Expand All @@ -30,7 +33,10 @@ describe('ruleToCredential', () => {
},
payload
)
).toBeUndefined();
).toEqual({
type: AuthorizationCredential.ChallengeAdmin,
resource_id: undefined,
});
});
it('returns correct response', () => {
const payload = {
Expand All @@ -49,9 +55,8 @@ describe('ruleToCredential', () => {
payload
)
).toEqual({
role: AuthorizationCredential.EcoverseAdmin,
resourceID: 'hub',
isAdmin: false,
type: AuthorizationCredential.EcoverseAdmin,
resource_id: '<>',
});
});
it('returns correct response on global admin', () => {
Expand All @@ -71,8 +76,8 @@ describe('ruleToCredential', () => {
payload
)
).toEqual({
role: AuthorizationCredential.GlobalAdmin,
isAdmin: false,
type: AuthorizationCredential.GlobalAdmin,
resource_id: '<>',
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RecipientCredential, TemplateRule } from '@core/contracts';
import { ApplicationCreatedEventPayload } from '@src/types/application.created.event.payload';
import { AuthorizationCredential } from '@alkemio/client-lib';
import { ApplicationCreatedEventPayload } from '@src/types/application.created.event.payload';

/***
* Returns a credential from the payload based on the rule provided
Expand All @@ -11,21 +11,17 @@ import { AuthorizationCredential } from '@alkemio/client-lib';
*/
export const ruleToCredential = (
templateRule: TemplateRule,
payload: ApplicationCreatedEventPayload,
isAdmin = false
): RecipientCredential | undefined => {
payload: ApplicationCreatedEventPayload
): RecipientCredential => {
const { rule } = templateRule;
const resourceID = getResourceId(rule.type, rule.resource_id, payload);
const resourceID = getResourceId(rule.type, rule.resource_id || '', payload);

//valentin - what if it's undefined and not null?
if (resourceID === null) {
return undefined;
rule.resource_id = undefined;
}

return {
role: templateRule.rule.type,
resourceID,
isAdmin,
};
return rule;
};

/***
Expand Down
Loading

0 comments on commit 82cdd49

Please sign in to comment.