Skip to content

Commit

Permalink
Merge branch 'develop' into haydnba/test-external-api-errors-not-prop…
Browse files Browse the repository at this point in the history
…agated
  • Loading branch information
annarhughes committed Jun 17, 2024
2 parents 67b00b7 + ceeb76a commit af8183d
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 54 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
},
"dependencies": {
"@mailchimp/mailchimp_marketing": "^3.0.80",
"@nestjs/axios": "^3.0.2",
"@nestjs/common": "^10.3.6",
"@nestjs/config": "^3.2.2",
"@nestjs/core": "^10.3.6",
"@nestjs/platform-express": "^10.3.7",
"@nestjs/swagger": "^7.3.1",
"@nestjs/terminus": "^10.2.3",
"@nestjs/typeorm": "^10.0.2",
"axios": "^1.6.8",
"axios": "^1.7.2",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"date-fns": "^3.6.0",
Expand Down
22 changes: 0 additions & 22 deletions src/app.controller.spec.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/app.controller.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CourseUserModule } from './course-user/course-user.module';
import { CourseModule } from './course/course.module';
import { EventLoggerModule } from './event-logger/event-logger.module';
import { FeatureModule } from './feature/feature.module';
import { HealthModule } from './health/health.module';
import { LoggerModule } from './logger/logger.module';
import { PartnerAccessModule } from './partner-access/partner-access.module';
import { PartnerAdminModule } from './partner-admin/partner-admin.module';
Expand Down Expand Up @@ -39,6 +40,7 @@ import { WebhooksModule } from './webhooks/webhooks.module';
FeatureModule,
PartnerFeatureModule,
EventLoggerModule,
HealthModule,
],
})
export class AppModule {}
21 changes: 21 additions & 0 deletions src/health/health.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { HttpModule } from '@nestjs/axios';
import { TerminusModule } from '@nestjs/terminus';
import { Test, TestingModule } from '@nestjs/testing';
import { HealthController } from './health.controller';

describe('HealthController', () => {
let controller: HealthController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [TerminusModule, HttpModule],
controllers: [HealthController],
}).compile();

controller = module.get<HealthController>(HealthController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
35 changes: 35 additions & 0 deletions src/health/health.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Controller, Get } from '@nestjs/common';
import {
HealthCheck,
HealthCheckService,
HttpHealthIndicator,
TypeOrmHealthIndicator,
} from '@nestjs/terminus';
import { frontendAppUrl } from 'src/utils/constants';

@Controller('ping')
export class HealthController {
constructor(
private readonly health: HealthCheckService,
private readonly http: HttpHealthIndicator,
private readonly db: TypeOrmHealthIndicator,
) {}

@Get()
@HealthCheck()
ping() {
return 'ok';
}

@Get('/frontend')
@HealthCheck()
checkFrontend() {
return this.health.check([() => this.http.pingCheck('frontend', frontendAppUrl)]);
}

@Get('/database')
@HealthCheck()
checkDatabase() {
return this.health.check([() => this.db.pingCheck('database')]);
}
}
10 changes: 10 additions & 0 deletions src/health/health.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { TerminusModule } from '@nestjs/terminus';
import { HealthController } from './health.controller';

@Module({
imports: [TerminusModule, HttpModule],
controllers: [HealthController],
})
export class HealthModule {}
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ async function bootstrap() {

app.setGlobalPrefix('api');

// Starts listening for shutdown hooks
app.enableShutdownHooks();

const options = new DocumentBuilder()
.setTitle('Bloom backend API')
.setDescription('Bloom backend API')
Expand Down
7 changes: 4 additions & 3 deletions src/partner-access/partner-access.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ describe('PartnerAccessService', () => {
let mockPartnerAccessRepository: DeepMocked<Repository<PartnerAccessEntity>>;

beforeEach(async () => {
jest.clearAllMocks();

mockPartnerRepository = createMock<Repository<PartnerEntity>>(mockPartnerRepositoryMethods);
mockPartnerAccessRepository = createMock<Repository<PartnerAccessEntity>>(
mockPartnerAccessRepositoryMethods,
Expand Down Expand Up @@ -138,7 +140,7 @@ describe('PartnerAccessService', () => {
return {
...mockPartnerAccessEntity,
id: 'pa1',
userId: mockGetUserDto.user.id,
userId: mockUserEntity.id,
};
});
// Mocks that the accesscode already exists
Expand All @@ -150,13 +152,12 @@ describe('PartnerAccessService', () => {

expect(partnerAccess).toEqual({
...mockPartnerAccessEntity,
id: 'pa1',
userId: mockUserEntity.id,
activatedAt: partnerAccess.activatedAt,
});

expect(profileData.updateServiceUserProfilesPartnerAccess).toHaveBeenCalledWith(
[partnerAccess],
[mockPartnerAccessEntity],
mockUserEntity.email,
);
});
Expand Down
9 changes: 6 additions & 3 deletions src/partner-access/partner-access.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,12 @@ export class PartnerAccessService {
assignedPartnerAccess.partner = partnerAccess.partner;

try {
const partnerAccesses = await this.partnerAccessRepository.findBy({
userId: user.id,
active: true,
const partnerAccesses = await this.partnerAccessRepository.find({
where: {
userId: user.id,
active: true,
},
relations: { partner: true },
});
await updateServiceUserProfilesPartnerAccess(partnerAccesses, user.email);
} catch (error) {
Expand Down
15 changes: 12 additions & 3 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,26 @@ export enum COMMUNICATION_SERVICE {
MAILCHIMP = 'MAILCHIMP',
}

export enum ENVIRONMENTS {
DEVELOPMENT = 'development',
STAGING = 'staging',
PRODUCTION = 'production',
TEST = 'test',
}

const getEnv = (env: string, envName: string): string => {
try {
if (!env) throw `Unable to get environemt variable ${envName}`;
if (!env) throw `Unable to get environment variable ${envName}`;

return env;
} catch (error) {
console.log(error);
if (nodeEnv !== ENVIRONMENTS.TEST) console.log(error);
}
};

export const isProduction = getEnv(process.env.NODE_ENV, 'NODE_ENV') === 'production';
export const nodeEnv = getEnv(process.env.NODE_ENV, 'NODE_ENV');
export const isProduction = nodeEnv === ENVIRONMENTS.PRODUCTION;
export const frontendAppUrl = getEnv(process.env.FRONTEND_APP_URL, 'FRONTEND_APP_URL');

export const rollbarEnv = getEnv(process.env.ROLLBAR_ENV, 'ROLLBAR_ENV');
export const rollbarToken = getEnv(process.env.ROLLBAR_TOKEN, 'ROLLBAR_TOKEN');
Expand Down
10 changes: 5 additions & 5 deletions src/utils/serviceUserProfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ export const serializePartnersString = (partnerAccesses: PartnerAccessEntity[])
return partnerAccesses?.map((pa) => pa.partner.name.toLowerCase()).join('; ') || '';
};

const serializeCrispPartnerSegments = (partners: PartnerEntity[]) => {
if (!partners.length) return ['public'];
return partners.map((p) => p.name.toLowerCase());
};

const serializeUserData = (user: UserEntity) => {
const { name, signUpLanguage, contactPermission, serviceEmailsPermission } = user;

Expand Down Expand Up @@ -356,8 +361,3 @@ const serializeCourseData = (courseUser: CourseUserEntity) => {

return { crispSchema, mailchimpSchema };
};

const serializeCrispPartnerSegments = (partners: PartnerEntity[]) => {
if (!partners.length) return ['public'];
return partners.map((p) => p.name.toLowerCase());
};
8 changes: 2 additions & 6 deletions test/utils/mockedServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
mockTherapySessionEntity,
mockUserEntity,
mockUserRecord,
partnerAccessArray,
} from './mockData';
import { createQueryBuilderMock } from './mockUtils';

Expand Down Expand Up @@ -209,11 +208,8 @@ export const mockPartnerAccessRepositoryMethods: PartialFuncReturn<
findBy: async (arg) => {
return [{ ...mockPartnerAccessEntity, ...(arg ? { ...arg } : {}) }] as PartnerAccessEntity[];
},
find: async (arg) => {
return [
...partnerAccessArray,
{ ...mockPartnerAccessEntity, ...(arg ? { ...arg } : {}) },
] as PartnerAccessEntity[];
find: async () => {
return [{ ...mockPartnerAccessEntity }] as PartnerAccessEntity[];
},
save: async (arg) => arg as PartnerAccessEntity,
};
Expand Down
Loading

0 comments on commit af8183d

Please sign in to comment.