Skip to content

Commit

Permalink
Add targetedMessaging feature flag, modify controller
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorgomezv committed Oct 2, 2024
1 parent 77506a0 commit fbd00ff
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,6 @@
#STAKING_TESTNET_API_KEY=
#STAKING_API_BASE_URI=
#STAKING_API_KEY=

# Targeted Messaging
#FF_TARGETED_MESSAGING=
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class AppModule implements NestModule {
confirmationView: isConfirmationViewEnabled,
delegatesV2: isDelegatesV2Enabled,
pushNotifications: isPushNotificationsEnabled,
targetedMessaging: isTargetedMessagingFeatureEnabled,
} = configFactory()['features'];

return {
Expand Down Expand Up @@ -98,7 +99,7 @@ export class AppModule implements NestModule {
RootModule,
SafeAppsModule,
SafesModule,
TargetedMessagingModule,
...(isTargetedMessagingFeatureEnabled ? [TargetedMessagingModule] : []),
TransactionsModule,
...(isConfirmationViewEnabled
? [TransactionsViewControllerModule]
Expand Down
1 change: 1 addition & 0 deletions src/config/entities/__tests__/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export default (): ReturnType<typeof configuration> => ({
pushNotifications: false,
nativeStaking: false,
nativeStakingDecoding: false,
targetedMessaging: false,
},
httpClient: { requestTimeout: faker.number.int() },
locking: {
Expand Down
2 changes: 2 additions & 0 deletions src/config/entities/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ export default () => ({
nativeStaking: process.env.FF_NATIVE_STAKING?.toLowerCase() === 'true',
nativeStakingDecoding:
process.env.FF_NATIVE_STAKING_DECODING?.toLowerCase() === 'true',
targetedMessaging:
process.env.FF_TARGETED_MESSAGING?.toLowerCase() === 'true',
},
httpClient: {
// Timeout in milliseconds to be used for the HTTP client.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HttpException, HttpStatus } from '@nestjs/common';

export class TargetedSafeNotFoundError extends HttpException {
constructor() {
super(`Targeted Safe not found`, HttpStatus.NO_CONTENT);
}
}
4 changes: 3 additions & 1 deletion src/routes/common/interceptors/cache-control.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export class CacheControlInterceptor implements NestInterceptor {
return next.handle().pipe(
tap(() => {
const response: Response = context.switchToHttp().getResponse();
response.header('Cache-Control', 'no-cache');
if (!response.headersSent) {
response.header('Cache-Control', 'no-cache');
}
}),
);
}
Expand Down
29 changes: 22 additions & 7 deletions src/routes/targeted-messaging/targeted-messaging.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TargetedSafeNotFoundError } from '@/domain/targeted-messaging/errors/targeted-safe-not-found.error';
import { Submission } from '@/routes/targeted-messaging/entities/submission.entity';
import { TargetedMessagingService } from '@/routes/targeted-messaging/targeted-messaging.service';
import { AddressSchema } from '@/validation/entities/schemas/address.schema';
Expand All @@ -7,10 +8,13 @@ import {
Controller,
DefaultValuePipe,
Get,
HttpStatus,
Param,
ParseIntPipe,
Res,
} from '@nestjs/common';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';

@ApiTags('targeted-messaging')
@Controller({
Expand All @@ -25,19 +29,30 @@ export class TargetedMessagingController {
'outreaches/:outreachId/chains/:chainId/safes/:safeAddress/signers/:signerAddress/submissions',
)
async getSubmission(
@Res() res: Response,
@Param('outreachId', new DefaultValuePipe(0), ParseIntPipe)
outreachId: number,
@Param('chainId', new ValidationPipe(NumericStringSchema)) chainId: string,
@Param('safeAddress', new ValidationPipe(AddressSchema))
safeAddress: `0x${string}`,
@Param('signerAddress', new ValidationPipe(AddressSchema))
signerAddress: `0x${string}`,
): Promise<Submission> {
return this.service.getSubmission({
outreachId,
chainId,
safeAddress,
signerAddress,
});
): Promise<Submission | Response> {
try {
return await this.service.getSubmission({
outreachId,
chainId,
safeAddress,
signerAddress,
});
} catch (err) {
if (err instanceof TargetedSafeNotFoundError) {
return res
.status(HttpStatus.NO_CONTENT)
.header('Cache-Control', 'no-cache')
.send({});
}
throw err;
}
}
}
46 changes: 35 additions & 11 deletions src/routes/targeted-messaging/targeted-messaging.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { TargetedSafe } from '@/domain/targeted-messaging/entities/targeted-safe.entity';
import { TargetedSafeNotFoundError } from '@/domain/targeted-messaging/errors/targeted-safe-not-found.error';
import { ITargetedMessagingRepository } from '@/domain/targeted-messaging/targeted-messaging.repository.interface';
import { Submission } from '@/routes/targeted-messaging/entities/submission.entity';
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, NotFoundException } from '@nestjs/common';

@Injectable()
export class TargetedMessagingService {
Expand All @@ -16,20 +18,42 @@ export class TargetedMessagingService {
signerAddress: `0x${string}`;
}): Promise<Submission> {
const { outreachId, signerAddress } = args;
let targetedSafe: TargetedSafe;
let submission: Submission;

const targetedSafe = await this.repository.getTargetedSafe(args);
const submission = await this.repository.getSubmission({
targetedSafe,
signerAddress,
});
try {
targetedSafe = await this.repository.getTargetedSafe(args);
} catch (err) {
if (err instanceof NotFoundException) {
throw new TargetedSafeNotFoundError();
} else {
throw err;
}
}

console.log('Submission:', submission);
try {
submission = await this.repository.getSubmission({
targetedSafe,
signerAddress,
});
} catch (err) {
if (err instanceof NotFoundException) {
submission = {
outreachId,
targetedSafeId: targetedSafe.id,
signerAddress,
completionDate: null,
};
} else {
throw err;
}
}

return {
outreachId,
targetedSafeId: targetedSafe.id,
signerAddress,
completionDate: null,
outreachId: submission.outreachId,
targetedSafeId: submission.targetedSafeId,
signerAddress: submission.signerAddress,
completionDate: submission.completionDate,
};
}
}

0 comments on commit fbd00ff

Please sign in to comment.