Skip to content

Commit

Permalink
Merge pull request #33 from alkem-io/infra-ops-224
Browse files Browse the repository at this point in the history
e2e flow fixes
  • Loading branch information
valentinyanakiev authored Nov 11, 2021
2 parents e55c6cf + 5fc6d65 commit ab9a723
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 57 deletions.
2 changes: 0 additions & 2 deletions .env.docker

This file was deleted.

5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ COPY package*.json ./
RUN npm i -g [email protected]
RUN npm install

## Add the wait script to the image
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
RUN chmod +x /wait

# If you are building your code for production
# RUN npm ci --only=production

Expand All @@ -28,5 +32,4 @@ RUN npm run build

ENV NODE_ENV=${ENV_ARG}

EXPOSE ${GRAPHQL_ENDPOINT_PORT_ARG}
CMD ["/bin/sh", "-c", "npm run start:prod"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Alkemio out-of-band notifications service.
2. Start mailslurper:

```
docker-compose -f quickstart-mailslurper.yml -f quickstart-notifications.yml --env-file .env.docker up --build --force-recreate
npm run start:services
```

3. Go to http://localhost:15672/#/queues/%2F/alkemio-notifications.
Expand Down
4 changes: 2 additions & 2 deletions notifications.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ hosting:
alkemio:
endpoint: ${ALKEMIO_SERVER_ENDPOINT}:http://localhost:3000/admin/graphql
service_account:
username: ${SERVICE_ACCOUNT_USERNAME}:admin@alkem.io
password: ${SERVICE_ACCOUNT_PASSWORD}:obichamazis
username: ${SERVICE_ACCOUNT_USERNAME}:notifications@alkem.io
password: ${SERVICE_ACCOUNT_PASSWORD}:change-me-now

kratos:
public_endpoint: ${KRATOS_API_PUBLIC_ENDPOINT}:http://localhost:3000/identity/ory/kratos/public
Expand Down
31 changes: 22 additions & 9 deletions package-lock.json

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

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alkemio-notifications",
"version": "0.3.0",
"version": "0.3.1",
"description": "Alkemio notifications service",
"author": "Cherrytwist Foundation",
"private": false,
Expand All @@ -20,6 +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",
"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 All @@ -29,10 +30,11 @@
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:ci:coverage": "jest --runInBand --config ./test/config/jest.config.ci.js && cat ./coverage-ci/lcov.info | coveralls",
"test:ci": "jest --runInBand --config ./test/config/jest.config.ci.js",
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/config/typeorm.cli.config.ts"
"typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js --config src/config/typeorm.cli.config.ts",
"validate-connection": "ts-node src/utils/validate-connection.ts"
},
"dependencies": {
"@alkemio/client-lib": "^0.9.9",
"@alkemio/client-lib": "^0.10.1",
"@nestjs/axios": "^0.0.1",
"@nestjs/common": "^8.0.5",
"@nestjs/config": "^1.0.1",
Expand All @@ -47,6 +49,7 @@
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"cross-env": "^7.0.3",
"dotenv": "^10.0.0",
"helmet": "^4.6.0",
"module-alias": "^2.2.2",
"nest-winston": "^1.5.0",
Expand Down
4 changes: 2 additions & 2 deletions quickstart-mailslurper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ services:
container_name: alkemio_notifications_mailslurper
image: oryd/mailslurper:latest-smtps
ports:
- 4436
- 5051:4437
- 4436:4436
- 4437:4437
- 1025:1025
networks:
- alkemio_notifications_net
21 changes: 0 additions & 21 deletions quickstart-notifications.yml

This file was deleted.

16 changes: 7 additions & 9 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Controller, Inject, LoggerService } from '@nestjs/common';
import { Ctx, EventPattern, Payload, RmqContext } from '@nestjs/microservices';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { Channel, Message } from 'amqplib';
import { ALKEMIO_CLIENT_ADAPTER } from './common';
import { ALKEMIO_CLIENT_ADAPTER, LogContext } from './common';
import { NotificationService } from '@src/services';
import { IFeatureFlagProvider } from '@core/contracts';
import { ApplicationCreatedEventPayload } from '@src/types/application.created.event.payload';
Expand All @@ -23,31 +23,29 @@ export class AppController {
@Payload() payload: ApplicationCreatedEventPayload,
@Ctx() context: RmqContext
) {
this.logger.verbose?.(JSON.stringify(payload), LogContext.NOTIFICATIONS);

const channel: Channel = context.getChannelRef();
const originalMsg = context.getMessage() as Message;

if (!(await this.featureFlagProvider.areNotificationsEnabled())) {
//toDo make this nack
channel.ack(originalMsg);
return;
}

this.notificationService
.sendApplicationNotifications(payload)
.then(x => {
const shouldNack = x.some(y => y.status === 'rejected');
const nacked = x.filter(y => y.status === 'rejected');

if (shouldNack) {
//toDo make this nack
channel.ack(originalMsg);
if (nacked.length === 0) {
this.logger.verbose?.(`All ${x.length} messages successfully sent!`);
} else {
channel.ack(originalMsg);
this.logger.verbose?.(`${nacked.length} messages failed to be sent!`);
}
})
.catch(err => {
this.logger.error(err);
// toDo check how to reject a message
// channel.reject(originalMsg);
});
}
}
9 changes: 9 additions & 0 deletions src/common/constants/defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const DEFAULT_ENDPOINTS = {
alkemioServer: 'http://localhost:3000/admin/graphql',
kratosApiEndpoint: 'http://localhost:3000/identity/ory/kratos/public',
};

export const DEFAULT_SERVICE_ACCOUNT_CREDENTIALS = {
email: '[email protected]',
password: 'change-me-now',
};
1 change: 1 addition & 0 deletions src/common/constants/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './defaults';
1 change: 1 addition & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './enums';
export * from './exceptions';
export * from './constants';
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export class AlkemioClientAdapter

async getApplicant(payload: any): Promise<User> {
const applicant = await this.alkemioClient.user(payload.applicantID);
if (!applicant) throw new Error('Applicant not found!');
if (!applicant)
throw new Error(`Applicant with id: ${payload.applicantID} not found!`);

return applicant;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class ApplicationNotificationBuilder {
}

const getBaseNotification = (payload: any, applicant: User) => ({
emailFrom: '<[email protected]>',
emailFrom: '[email protected]',
applicant: {
name: applicant.displayName,
email: applicant.email,
Expand Down
46 changes: 46 additions & 0 deletions src/utils/validate-connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { AlkemioClient } from '@alkemio/client-lib';
import {
DEFAULT_ENDPOINTS,
DEFAULT_SERVICE_ACCOUNT_CREDENTIALS,
} from '@src/common/constants/defaults';
import * as dotenv from 'dotenv';

const main = async () => {
dotenv.config();

const alkemioClient = new AlkemioClient({
graphqlEndpoint:
process.env.ALKEMIO_SERVER_ENDPOINT ?? DEFAULT_ENDPOINTS.alkemioServer,
authInfo: {
credentials: {
email:
process.env.SERVICE_ACCOUNT_USERNAME ??
DEFAULT_SERVICE_ACCOUNT_CREDENTIALS.email,
password:
process.env.SERVICE_ACCOUNT_PASSWORD ??
DEFAULT_SERVICE_ACCOUNT_CREDENTIALS.password,
},
kratosPublicApiEndpoint:
process.env.KRATOS_API_PUBLIC_ENDPOINT ??
DEFAULT_ENDPOINTS.kratosApiEndpoint,
},
});

console.log(`Client config: ${JSON.stringify(alkemioClient.config)}`);
await alkemioClient.enableAuthentication();

console.log(`API Token: ${alkemioClient.apiToken}`);

const serverVersion = await alkemioClient.validateConnection();
console.log(`Alkemio platform version: ${serverVersion}`);

const hubID = 'Test';
const hubExists = await alkemioClient.hubExists(hubID);
console.log(`Hub '${hubID}' exists: ${hubExists}`);
};

try {
main();
} catch (error) {
console.error(error);
}
17 changes: 12 additions & 5 deletions src/wrappers/alkemio-client/alkemio.client.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ export async function alkemioClientFactory(
): Promise<AlkemioClient | undefined> {
try {
const server = configService.get(ConfigurationTypes.ALKEMIO)?.endpoint;
const alkemioClient = new AlkemioClient({
const kratosApiEndpoint = configService.get(
ConfigurationTypes.KRATOS
)?.public_endpoint;

const alkemioClientConfig = {
graphqlEndpoint: server,
authInfo: {
credentials: {
Expand All @@ -18,11 +22,14 @@ export async function alkemioClientFactory(
password: configService.get(ConfigurationTypes.ALKEMIO)
?.service_account?.password,
},
apiEndpointFactory: () => {
return configService.get(ConfigurationTypes.KRATOS)?.public_endpoint;
},
kratosPublicApiEndpoint: kratosApiEndpoint,
},
});
};
logger.verbose?.(
`Alkemio client config: ${JSON.stringify(alkemioClientConfig)}`,
LogContext.NOTIFICATIONS
);
const alkemioClient = new AlkemioClient(alkemioClientConfig);

await alkemioClient.enableAuthentication();

Expand Down

0 comments on commit ab9a723

Please sign in to comment.