Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@aws-sdk/lib-dynamodb": "^3.751.0",
"@middy/core": "^6.0.0",
"@middy/http-cors": "^6.0.0",
"@notifycal/shared": "4.9.0",
"@notifycal/shared": "5.0.0",
"@vonage/server-sdk": "^3.20.0",
"axios": "^1.8.4",
"env-var": "^7.5.0",
Expand Down
12 changes: 6 additions & 6 deletions src/lambdas/api/post-demo-reminder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import type {
CorrelationId,
DateTime,
EventId,
Identity,
IdpName,
TemplateId
TemplateId,
UserIdentity
} from '@notifycal/shared/types';
import { errorHandler, successHandler } from '@services/common/api-response-handlers';
import { SnsService } from '@services/sns';
Expand Down Expand Up @@ -48,7 +48,7 @@ function buildEvent(
requestBody: Event['body'],
userReminderConfig: LiveUserStoreRecord<unknown>['Config'],
templateId: TemplateId,
identity: Identity<IdpName>
userIdentity: UserIdentity<IdpName>
): DemoReminderToBeSentEvent {
const eventId = v4();
const message = interpolate(
Expand All @@ -63,9 +63,9 @@ function buildEvent(
correlationId: eventId as CorrelationId,
eventType: 'DemoReminderToBeSent',
happenedAt: new Date().toISOString() as DateTime,
userId: identity.userId,
idp: identity.idp,
idpId: identity.idpId,
userId: userIdentity.userId,
idp: userIdentity.idp,
idpId: userIdentity.idpId,
data: {
senderDetails: senderToCanonicalForm(
fromStoreRecord(userReminderConfig.Business.SenderContact)
Expand Down
8 changes: 4 additions & 4 deletions src/lambdas/api/post-login/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import type { Algorithm } from '@model/Config';
import type { AuthorizationForIdp } from '@model/IdpAuthorization';
import type {
Email,
Identity,
IdpId,
IdpName,
Jwt,
UnixTimestamp,
UserIdentity,
Uuid
} from '@notifycal/shared/types';
import type { AwsArn, PrivateKey } from '@own-types/model';
Expand Down Expand Up @@ -41,7 +41,7 @@ import { handler, type Event } from './index';
describe('POST Login', () => {
const userEmail = 'test@notifycal.com' as Email;
const validUserId = validJwts.accessToken.decoded.payload.userId;
const validIdentity: Identity<'google.com'> = {
const validIdentity: UserIdentity<'google.com'> = {
userId: validJwts.accessToken.decoded.payload.userId,
email: userEmail,
idp: 'google.com',
Expand All @@ -51,7 +51,7 @@ describe('POST Login', () => {
refreshToken: 'some_google_refressssh_token'
};
const validVerifyGoogleIdentityFn = (): Promise<
[Identity<'google.com'>, AuthorizationForIdp<'google.com'>]
[UserIdentity<'google.com'>, AuthorizationForIdp<'google.com'>]
> => Promise.resolve([validIdentity, validAuthorization]);
const validQueryParams = {
idp: 'google.com'
Expand Down Expand Up @@ -252,7 +252,7 @@ describe('POST Login', () => {
// eslint-disable-next-line @typescript-eslint/require-await
async function testit<T extends IdpName>(
event: APIGatewayProxyEvent,
verifyGoogleIdentityFn: () => Promise<[Identity<T>, AuthorizationForIdp<T>]>,
verifyGoogleIdentityFn: () => Promise<[UserIdentity<T>, AuthorizationForIdp<T>]>,
signInOrUpUserFn: () => Promise<EncodedAndDecodedJwts>,
env: LoginConfig = defaultEnv
): Promise<APIGatewayProxyResult> {
Expand Down
14 changes: 7 additions & 7 deletions src/lambdas/api/post-login/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logger } from '@common/powertools';
import type { IdpConfigs } from '@model/Config';
import type { AuthorizationForIdp } from '@model/IdpAuthorization';
import { apiEventSchema } from '@model/lambda-events/ApiGatewayEvents';
import type { Identity, IdpName } from '@notifycal/shared/types';
import type { IdpName, UserIdentity } from '@notifycal/shared/types';
import type { Url } from '@own-types/model';
import { _successHandler, signInOrUp } from '@services/auth';
import { errorHandler } from '@services/common/api-response-handlers';
Expand All @@ -30,7 +30,7 @@ function verifyIdentity(
event: Event,
idpQueryParameter: string | undefined,
config: IdpConfigs
): Promise<[Identity<IdpName>, AuthorizationForIdp<IdpName>]> {
): Promise<[UserIdentity<IdpName>, AuthorizationForIdp<IdpName>]> {
const origin = event.headers?.origin || event.headers?.Origin || event.headers?.ORIGIN;
if (isValidIdpName(idpQueryParameter) && idpQueryParameter === 'google.com' && origin) {
return GoogleOAuth.withConfig(config['google.com'], origin as Url, logger).verifyIdentity(
Expand All @@ -51,13 +51,13 @@ function lambdaHandler(
const idpQueryPath = event.queryStringParameters?.['idp'];

return verifyIdentity(event, idpQueryPath, config.idpConfigs)
.then(([identity, idpAuthorization]) => {
.then(([userIdentity, idpAuthorization]) => {
logger.appendKeys({
userId: identity.userId,
idp: identity.idp,
idpId: identity.idpId
userId: userIdentity.userId,
idp: userIdentity.idp,
idpId: userIdentity.idpId
});
return signInOrUp(identity, idpAuthorization, config, logger)
return signInOrUp(userIdentity, idpAuthorization, config, logger)
.then(_successHandler)
.catch(errorHandler(500));
})
Expand Down
32 changes: 16 additions & 16 deletions src/lambdas/api/post-payment-session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { corsErrorResponse } from '@common/cors-middleware';
import { protectedEndpointMiddleware } from '@common/lambda-middleware';
import { logger, metrics } from '@common/powertools';
import type { Tier, Topup } from '@model/PaymentPlans';
import type { Identity, IdpName, StripeCustomerId } from '@notifycal/shared/types';
import type { IdpName, StripeCustomerId, UserIdentity } from '@notifycal/shared/types';
import type { Url } from '@own-types/model';
import {
errorHandler,
Expand All @@ -19,15 +19,15 @@ import { readPostPaymentCheckoutSessionConfig } from './config';
import { type Event, eventSchema } from './schemas';

function createCustomerOrRetrieve(
identity: Identity<IdpName>,
userIdentity: UserIdentity<IdpName>,
userBaseStore: UserBaseStore<IdpName>,
stripeService: StripeService
): Promise<StripeCustomerId> {
return userBaseStore
.getStripeCustomerId(identity.userId)
.getStripeCustomerId(userIdentity.userId)
.catch((error) => {
logger.error('Failed to get stripe customer ID from database', {
userId: identity.userId,
userId: userIdentity.userId,
error
});
throw error;
Expand All @@ -37,21 +37,21 @@ function createCustomerOrRetrieve(
return Promise.resolve(stripeCustomerIdOrNot);
} else {
return stripeService
.createCustomer(identity)
.createCustomer(userIdentity)
.catch((error) => {
logger.error('Failed to create stripe customer', {
userId: identity.userId,
email: identity.email,
userId: userIdentity.userId,
email: userIdentity.email,
error
});
throw error;
})
.then((stripeCustomerId) =>
userBaseStore
.setStripeCustomerId(identity.userId, stripeCustomerId)
.setStripeCustomerId(userIdentity.userId, stripeCustomerId)
.catch((error) => {
logger.error('Failed to save stripe customer ID to database', {
userId: identity.userId,
userId: userIdentity.userId,
stripeCustomerId,
error
});
Expand All @@ -67,7 +67,7 @@ function checkEligibility(
stripeCustomerId: StripeCustomerId,
selectedProduct: Tier | Topup,
stripeService: StripeService,
identity: Identity<IdpName>
userIdentity: UserIdentity<IdpName>
): Promise<{ eligible: boolean; stripeCustomerId: StripeCustomerId }> {
if (selectedProduct.type === 'topup') {
return Promise.resolve({ eligible: true, stripeCustomerId });
Expand All @@ -76,7 +76,7 @@ function checkEligibility(
return stripeService.countSubscriptions(stripeCustomerId).then((activeSubscriptionCount) => {
if (activeSubscriptionCount >= 1) {
logger.info('Customer already has an active subscription', {
userId: identity.userId,
userId: userIdentity.userId,
stripeCustomerId,
activeSubscriptionCount
});
Expand All @@ -92,7 +92,7 @@ async function lambdaHandler(
_ctx: Context
): Promise<APIGatewayProxyResult> {
const { userId, idp, idpId, email } = event.requestContext.authorizer.payload;
const identity = { userId, idp, idpId, email };
const userIdentity = { userId, idp, idpId, email };
const { stripeAuthConfig, stripeCheckoutConfig, paymentPlans, userBaseStoreConfig } =
event.lambdaConfig;
const apiKey = stripeAuthConfig.apiKey;
Expand Down Expand Up @@ -121,9 +121,9 @@ async function lambdaHandler(
const userBaseStore = UserBaseStore.withConfig(userBaseStoreConfig, logger);
const stripeService = await StripeService.withConfig(apiKey);

return createCustomerOrRetrieve(identity, userBaseStore, stripeService)
return createCustomerOrRetrieve(userIdentity, userBaseStore, stripeService)
.then((stripeCustomerId) =>
checkEligibility(stripeCustomerId, selectedProduct, stripeService, identity)
checkEligibility(stripeCustomerId, selectedProduct, stripeService, userIdentity)
)
.then((eligibilityResult) => {
if (!eligibilityResult.eligible) {
Expand All @@ -139,7 +139,7 @@ async function lambdaHandler(
return stripeService
.createCheckoutSession(
eligibilityResult.stripeCustomerId,
identity,
userIdentity,
selectedProduct,
language,
successRedirectUrl,
Expand All @@ -159,7 +159,7 @@ async function lambdaHandler(
},
(error) => {
logger.error('Failed to create stripe checkout session', {
userId: identity.userId,
userId: userIdentity.userId,
stripeCustomerId: eligibilityResult.stripeCustomerId,
product: selectedProduct.id,
error
Expand Down
4 changes: 2 additions & 2 deletions src/lambdas/api/post-refresh/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { unprotectedCrossDomainEndpointMiddleware } from '@common/lambda-middlew
import { logger } from '@common/powertools';
import { refreshTokenSchema } from '@model/Jwt';
import { apiEventSchema } from '@model/lambda-events/ApiGatewayEvents';
import { extractIdentity } from '@model/UserIdentity';
import { extractUserIdentity } from '@model/UserIdentity';
import type { Jwt } from '@notifycal/shared/types';
import { _successHandler, buildJwtsAndStoreRefreshJwt } from '@services/auth';
import { errorHandler } from '@services/common/api-response-handlers';
Expand Down Expand Up @@ -53,7 +53,7 @@ function lambdaHandler(
idpId: user.IdpId
});
return buildJwtsAndStoreRefreshJwt(
extractIdentity(user),
extractUserIdentity(user),
config.encodeAccessJwtConfig,
config.encodeRefreshJwtConfig,
refreshTokenStore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function processAlertEvent(
logger: Logger
): Promise<void> {
logger.info(`Processing alert event`, { eventType: event.EventType });
const identity: EventSourceIdentity = extractIdentity(event);
const userIdentity: EventSourceIdentity = extractIdentity(event);
const options: EventCreationOptions = {
correlationId: event.CorrelationId
};
Expand All @@ -64,7 +64,7 @@ function processAlertEvent(
templateConfig,
event.EventType,
{ eventType: event.EventType },
identity,
userIdentity,
options
);
return snsService.publish(alertEvent).then();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function createEmailEvent(
errorRate,
notificationsSentCountBeforeUpdate: updateCounterResult.NotificationSentCount
};
const identity: EventSourceIdentity = extractIdentity(event);
const userIdentity: EventSourceIdentity = extractIdentity(event);
const options: EventCreationOptions = {
correlationId: event.CorrelationId
};
Expand All @@ -103,7 +103,7 @@ function createEmailEvent(
templateConfig,
subEventType,
metadata,
identity,
userIdentity,
options
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lambdas/sqs/fetch-user-calendars/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { fromStoreRecord as fromContactStoreRecord } from '@model/store/ContactD
import type { LiveUserStoreRecord } from '@model/store/LiveUserStoreRecord';
import { fromStoreRecord } from '@model/store/ReminderConfigStoreRecord';
import type { UserIdpAuthorizationStoreRecord } from '@model/store/UserIdpAuthorizationStoreRecord';
import { extractIdentity } from '@model/UserIdentity';
import { extractUserIdentity } from '@model/UserIdentity';
import type { CorrelationId, DateTime, EventId } from '@notifycal/shared/types';
import { setupLoggerCorrelationIdEventBridge } from '@services/common/logger';
import { SnsService } from '@services/sns';
Expand Down Expand Up @@ -131,7 +131,7 @@ async function lambdaHandler(event: Event, _context: Context): Promise<void> {
if (user.Config.Calendars && user.Config.Calendars.length > 0) {
return Promise.resolve(toEvents(user, run));
} else {
const errorEvent = noUserCalendarFound(record, run, extractIdentity(user));
const errorEvent = noUserCalendarFound(record, run, extractUserIdentity(user));
return snsService.safePublish(errorEvent).then(() => []);
}
})
Expand Down
6 changes: 3 additions & 3 deletions src/lambdas/sqs/stripe-webhook/event-handlers/checkout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Logger } from '@aws-lambda-powertools/logger';
import type { Identity, IdpName } from '@notifycal/shared/types';
import type { IdpName, UserIdentity } from '@notifycal/shared/types';
import type { Stripe } from 'stripe';
import type { StripeEventType } from '../stripe-schemas';
import { BaseHandler } from './base-handler';
Expand All @@ -18,13 +18,13 @@ export class CheckoutSessionCompletedHandler

public handle(
event: Stripe.CheckoutSessionCompletedEvent,
identity: Identity<IdpName>
userIdentity: UserIdentity<IdpName>
): Promise<void> {
const session = event.data.object;
this.logger.info('Handling checkout session completed', {
checkoutSessionId: session.id,
customerId: session.customer,
userId: identity.userId
userId: userIdentity.userId
});
return Promise.resolve();
}
Expand Down
4 changes: 2 additions & 2 deletions src/lambdas/sqs/stripe-webhook/event-handlers/common.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Identity, IdpName } from '@notifycal/shared/types';
import type { IdpName, UserIdentity } from '@notifycal/shared/types';
import type Stripe from 'stripe';
import type { StripeEventType } from '../stripe-schemas';

export type EventHandlerBuilder<T extends Stripe.Event = Stripe.Event> = (
type: StripeEventType
) => EventHandler<T>;
export interface EventHandler<T extends Stripe.Event = Stripe.Event> {
handle(event: T, identity: Identity<IdpName>): Promise<void>;
handle(event: T, userIdentity: UserIdentity<IdpName>): Promise<void>;
}
Loading
Loading