Skip to content

Commit

Permalink
ASAP-756 Save Cookie Preferences in DynamoDB (#4438)
Browse files Browse the repository at this point in the history
* ASAP-756 Save Cookie Preferences in DynamoDB

* add get

* remove some logs

* adding gp2 handlers and things on serverless

* add factory test

* coverage + adding log info again

* print get response

* add cookie not found case in get-cookie-factory-handler

* parse get response

* fix typecheck

* fix issues

* frontend send request to store cookie data in dynamodb

* update test

* use test throughout

* state update to be made in different pr

---------

Co-authored-by: AkosuaA <nattuah@gmail.com>
  • Loading branch information
gabiayako and AkosuaA authored Nov 18, 2024
1 parent 1a84a1d commit c983b08
Show file tree
Hide file tree
Showing 98 changed files with 2,847 additions and 31 deletions.
1,060 changes: 1,055 additions & 5 deletions .pnp.cjs

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 5 additions & 3 deletions apps/crn-frontend/src/auth/Signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { Frame, useCookieConsent } from '@asap-hub/frontend-utils';
import { UtilityBar, WelcomePage } from '@asap-hub/react-components';
import { useAuth0CRN } from '@asap-hub/react-context';
import { useHistory, useLocation } from 'react-router-dom';
import { COOKIE_CONSENT_NAME } from '../config';
import { API_BASE_URL, COOKIE_CONSENT_NAME } from '../config';

const Signin: React.FC<Record<string, never>> = () => {
const { loginWithRedirect } = useAuth0CRN();
const { showCookieModal, onSaveCookiePreferences } =
useCookieConsent(COOKIE_CONSENT_NAME);
const { showCookieModal, onSaveCookiePreferences } = useCookieConsent(
COOKIE_CONSENT_NAME,
`${API_BASE_URL}/cookie-preferences/save`,
);

const { pathname, search, hash } = useLocation();
const searchParams = new URLSearchParams(search);
Expand Down
6 changes: 4 additions & 2 deletions apps/crn-frontend/src/welcome/Welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ const Welcome: React.FC<Record<string, never>> = () => {

const toast = useContext(ToastContext);

const { showCookieModal, onSaveCookiePreferences } =
useCookieConsent(COOKIE_CONSENT_NAME);
const { showCookieModal, onSaveCookiePreferences } = useCookieConsent(
COOKIE_CONSENT_NAME,
`${API_BASE_URL}/cookie-preferences/save`,
);

const invitationValidityCheck = useRef<Promise<boolean>>();
useEffect(() => {
Expand Down
74 changes: 74 additions & 0 deletions apps/crn-server/serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,26 @@ const serverlessConfig: AWS = {
iam: {
role: {
statements: [
{
Effect: 'Allow',
Action: [
'dynamodb:PutItem',
'dynamodb:Get*',
'dynamodb:Update*',
'dynamodb:Delete*',
],
Resource: {
'Fn::Join': [
':',
[
'arn:aws:dynamodb',
{ Ref: 'AWS::Region' },
{ Ref: 'AWS::AccountId' },
'table/${self:service}-${self:provider.stage}-cookie-preferences',
],
],
},
},
{
Effect: 'Allow',
Action: 'secretsmanager:*',
Expand Down Expand Up @@ -959,6 +979,40 @@ const serverlessConfig: AWS = {
SENTRY_DSN: sentryDsnHandlers,
},
},
saveCookiePreferences: {
handler:
'./src/handlers/cookie-preferences/save-cookie-preferences-handler.handler',
events: [
{
httpApi: {
method: 'POST',
path: '/cookie-preferences/save',
},
},
],
environment: {
COOKIE_PREFERENCES_TABLE_NAME:
'${self:service}-${self:provider.stage}-cookie-preferences',
SENTRY_DSN: sentryDsnHandlers,
},
},
getCookiePreferences: {
handler:
'./src/handlers/cookie-preferences/get-cookie-preferences-handler.handler',
events: [
{
httpApi: {
method: 'GET',
path: '/cookie-preferences/{cookieId}',
},
},
],
environment: {
COOKIE_PREFERENCES_TABLE_NAME:
'${self:service}-${self:provider.stage}-cookie-preferences',
SENTRY_DSN: sentryDsnHandlers,
},
},

cronjobSyncOrcidContentful: {
handler: './src/handlers/user/cronjob-sync-orcid.handler',
Expand Down Expand Up @@ -1656,6 +1710,26 @@ const serverlessConfig: AWS = {
TopicName: '${self:custom.apiGateway5xxTopic}',
},
},
CookiePreferencesDynamoDbTable: {
Type: 'AWS::DynamoDB::Table',
Properties: {
TableName:
'${self:service}-${self:provider.stage}-cookie-preferences',
AttributeDefinitions: [
{
AttributeName: 'cookieId',
AttributeType: 'S',
},
],
KeySchema: [
{
AttributeName: 'cookieId',
KeyType: 'HASH',
},
],
BillingMode: 'PAY_PER_REQUEST',
},
},
},
extensions: {
GcalSubscribeCalendarContentfulLambdaFunction: {
Expand Down
4 changes: 4 additions & 0 deletions apps/crn-server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
CONTENTFUL_SPACE_ID,
CONTENTFUL_POLLER_QUEUE_URL,
CONTENTFUL_WEBHOOK_AUTHENTICATION_TOKEN,
COOKIE_PREFERENCES_TABLE_NAME,
CRN_MEETING_MATERIALS_DRIVE,
CURRENT_REVISION,
EMAIL_BCC,
Expand Down Expand Up @@ -71,6 +72,8 @@ export const contentfulPollerQueueUrl =
export const contentfulWebhookAuthenticationToken =
CONTENTFUL_WEBHOOK_AUTHENTICATION_TOKEN ||
'contentful-webhook-authentication-token';
export const cookiePreferencesTableName =
COOKIE_PREFERENCES_TABLE_NAME || 'asap-hub-dev-cookie-preferences';
export const crnMeetingMaterialsDrive =
CRN_MEETING_MATERIALS_DRIVE ||
'https://drive.google.com/drive/u/0/folders/0AKtA9ScsuPjTUk9PVA?pli=1';
Expand All @@ -82,6 +85,7 @@ export const eventBridgeAccessKey = EVENT_BRIDGE_ACCESS_KEY || '';
export const eventBridgeEndpoint = EVENT_BRIDGE_ENDPOINT;
export const eventBridgeSecret = EVENT_BRIDGE_SECRET || '';
export const eventBus = EVENT_BUS || 'asap-events-dev';

export const eventSource = EVENT_SOURCE || '';
export const googleApiCredentialsSecretId =
GOOGLE_API_CREDENTIALS_SECRET_ID || 'google-api-credentials-dev';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* istanbul ignore file */
import { getCookiePreferencesHandlerFactory } from '@asap-hub/server-common';
import { framework as lambda } from '@asap-hub/services-common';
import { Handler } from 'aws-lambda';

import { cookiePreferencesTableName } from '../../config';
import logger from '../../utils/logger';
import { sentryWrapper } from '../../utils/sentry-wrapper';

export const getCookiePreferencesHandler = lambda.http(
getCookiePreferencesHandlerFactory(logger, cookiePreferencesTableName),
);

export const handler: Handler = sentryWrapper(getCookiePreferencesHandler);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* istanbul ignore file */
import { saveCookiePreferencesHandlerFactory } from '@asap-hub/server-common';
import { framework as lambda } from '@asap-hub/services-common';
import { Handler } from 'aws-lambda';

import { cookiePreferencesTableName } from '../../config';
import logger from '../../utils/logger';
import { sentryWrapper } from '../../utils/sentry-wrapper';

export const saveCookiePreferencesHandler = lambda.http(
saveCookiePreferencesHandlerFactory(logger, cookiePreferencesTableName),
);

export const handler: Handler = sentryWrapper(saveCookiePreferencesHandler);
8 changes: 5 additions & 3 deletions apps/gp2-frontend/src/auth/Signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { useCookieConsent } from '@asap-hub/frontend-utils';
import { UtilityBar, WelcomePage } from '@asap-hub/react-components';
import { useAuth0GP2 } from '@asap-hub/react-context';
import { useHistory, useLocation } from 'react-router-dom';
import { COOKIE_CONSENT_NAME } from '../config';
import { API_BASE_URL, COOKIE_CONSENT_NAME } from '../config';
import Frame from '../Frame';

const Signin: React.FC<Record<string, never>> = () => {
const { loginWithRedirect } = useAuth0GP2();
const { showCookieModal, onSaveCookiePreferences } =
useCookieConsent(COOKIE_CONSENT_NAME);
const { showCookieModal, onSaveCookiePreferences } = useCookieConsent(
COOKIE_CONSENT_NAME,
`${API_BASE_URL}/cookie-preferences/save`,
);

const { pathname, search, hash } = useLocation();
const searchParams = new URLSearchParams(search);
Expand Down
6 changes: 4 additions & 2 deletions apps/gp2-frontend/src/welcome/Welcome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { API_BASE_URL, COOKIE_CONSENT_NAME } from '../config';

const Welcome: React.FC<Record<string, never>> = () => {
const { code } = useRouteParams(welcome({}).invited);
const { showCookieModal, onSaveCookiePreferences } =
useCookieConsent(COOKIE_CONSENT_NAME);
const { showCookieModal, onSaveCookiePreferences } = useCookieConsent(
COOKIE_CONSENT_NAME,
`${API_BASE_URL}/cookie-preferences/save`,
);

const { loginWithRedirect } = useAuth0GP2();

Expand Down
75 changes: 75 additions & 0 deletions apps/gp2-server/serverless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ const serverlessConfig: AWS = {
iam: {
role: {
statements: [
{
Effect: 'Allow',
Action: [
'dynamodb:PutItem',
'dynamodb:Get*',
'dynamodb:Update*',
'dynamodb:Delete*',
],
Resource: {
'Fn::Join': [
':',
[
'arn:aws:dynamodb',
{ Ref: 'AWS::Region' },
{ Ref: 'AWS::AccountId' },
'table/${self:service}-${self:provider.stage}-cookie-preferences',
],
],
},
},
{
Effect: 'Allow',
Action: 'secretsmanager:*',
Expand Down Expand Up @@ -836,6 +856,41 @@ const serverlessConfig: AWS = {
SENTRY_DSN: sentryDsnHandlers,
},
},
saveCookiePreferences: {
handler:
'./src/handlers/cookie-preferences/save-cookie-preferences-handler.handler',
events: [
{
httpApi: {
method: 'POST',
path: '/cookie-preferences/save',
},
},
],
environment: {
COOKIE_PREFERENCES_TABLE_NAME:
'${self:service}-${self:provider.stage}-cookie-preferences',
SENTRY_DSN: sentryDsnHandlers,
},
},
getCookiePreferences: {
handler:
'./src/handlers/cookie-preferences/get-cookie-preferences-handler.handler',
events: [
{
httpApi: {
method: 'GET',
path: '/cookie-preferences/{cookieId}',
},
},
],
environment: {
COOKIE_PREFERENCES_TABLE_NAME:
'${self:service}-${self:provider.stage}-cookie-preferences',
SENTRY_DSN: sentryDsnHandlers,
},
},

eventsUpdated: {
handler: './src/handlers/webhooks/events-updated.handler',
events: [
Expand Down Expand Up @@ -1499,6 +1554,26 @@ const serverlessConfig: AWS = {
'${self:service}-${self:provider.stage}-google-calendar-event-queue-dlq',
},
},
CookiePreferencesDynamoDbTable: {
Type: 'AWS::DynamoDB::Table',
Properties: {
TableName:
'${self:service}-${self:provider.stage}-cookie-preferences',
AttributeDefinitions: [
{
AttributeName: 'cookieId',
AttributeType: 'S',
},
],
KeySchema: [
{
AttributeName: 'cookieId',
KeyType: 'HASH',
},
],
BillingMode: 'PAY_PER_REQUEST',
},
},
},
extensions: {
SubscribeCalendarLambdaFunction: {
Expand Down
3 changes: 3 additions & 0 deletions apps/gp2-server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
CONTENTFUL_PREVIEW_ACCESS_TOKEN,
CONTENTFUL_SPACE_ID,
CONTENTFUL_WEBHOOK_AUTHENTICATION_TOKEN,
COOKIE_PREFERENCES_TABLE_NAME,
CURRENT_REVISION,
EMAIL_BCC,
EMAIL_RETURN,
Expand Down Expand Up @@ -63,6 +64,8 @@ export const contentfulSpaceId = CONTENTFUL_SPACE_ID || 'contentful-space-id';
export const contentfulWebhookAuthenticationToken =
CONTENTFUL_WEBHOOK_AUTHENTICATION_TOKEN ||
'contentful-webhook-authentication-token';
export const cookiePreferencesTableName =
COOKIE_PREFERENCES_TABLE_NAME || 'asap-hub-dev-cookie-preferences';
export const currentRevision = CURRENT_REVISION || 'default';
export const environment = ENVIRONMENT
? ENVIRONMENT.toLowerCase()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* istanbul ignore file */
import { getCookiePreferencesHandlerFactory } from '@asap-hub/server-common';
import { framework as lambda } from '@asap-hub/services-common';
import { Handler } from 'aws-lambda';

import { cookiePreferencesTableName } from '../../config';
import logger from '../../utils/logger';
import { sentryWrapper } from '../../utils/sentry-wrapper';

export const getCookiePreferencesHandler = lambda.http(
getCookiePreferencesHandlerFactory(logger, cookiePreferencesTableName),
);

export const handler: Handler = sentryWrapper(getCookiePreferencesHandler);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* istanbul ignore file */
import { saveCookiePreferencesHandlerFactory } from '@asap-hub/server-common';
import { framework as lambda } from '@asap-hub/services-common';
import { Handler } from 'aws-lambda';

import { cookiePreferencesTableName } from '../../config';
import logger from '../../utils/logger';
import { sentryWrapper } from '../../utils/sentry-wrapper';

export const saveCookiePreferencesHandler = lambda.http(
saveCookiePreferencesHandlerFactory(logger, cookiePreferencesTableName),
);

export const handler: Handler = sentryWrapper(saveCookiePreferencesHandler);
Loading

0 comments on commit c983b08

Please sign in to comment.