-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Booster] Milestone 6: Plot twist! Special treat to Wakandians.
- Loading branch information
1 parent
fd7995e
commit 444facc
Showing
20 changed files
with
382 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Entity, Reduces } from '@boostercloud/framework-core' | ||
import { UUID } from '@boostercloud/framework-types' | ||
import { PromoCodeCreated } from '../events/promo-code-created' | ||
|
||
@Entity | ||
export class PromoCode { | ||
public constructor( | ||
public id: UUID, | ||
readonly profileId: UUID, | ||
) {} | ||
|
||
@Reduces(PromoCodeCreated) | ||
public static reducePromoCodeCreated(event: PromoCodeCreated, currentPromoCode?: PromoCode): PromoCode { | ||
return new PromoCode(event.promoCodeId, event.profileId) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { WelcomeEmailDelivered } from '../events/welcome-email-delivered' | ||
import { Booster, EventHandler } from '@boostercloud/framework-core' | ||
import { Register } from '@boostercloud/framework-types' | ||
import { WelcomeEmailDeliveryFailed } from '../events/welcome-email-delivery-failed' | ||
import { KYCCompleted } from '../events/kyc-completed' | ||
import { isValidTransition } from '../common/state-validation' | ||
import { Profile } from '../entities/profile' | ||
|
||
@EventHandler(WelcomeEmailDelivered) | ||
@EventHandler(WelcomeEmailDeliveryFailed) | ||
export class CompleteKYC { | ||
public static async handle(event: WelcomeEmailDelivered, register: Register): Promise<void> { | ||
const profile = await Booster.entity(Profile, event.profileId) | ||
|
||
if (!profile) { | ||
throw new Error(`Profile ${event.profileId} not found`) | ||
} | ||
|
||
if (isValidTransition(profile, 'KYCCompleted')) { | ||
register.events(new KYCCompleted(event.profileId)) | ||
} else { | ||
throw new Error(`Invalid state transition for profile ${event.profileId} while completing KYC. Current state: ${profile.kycStatus}`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { BackgroundCheckPassed } from '../events/background-check-passed' | ||
import { Booster, EventHandler } from '@boostercloud/framework-core' | ||
import { Register, UUID } from '@boostercloud/framework-types' | ||
import { Profile } from '../entities/profile' | ||
import { PromoCodeCreated } from '../events/promo-code-created' | ||
import { WelcomeEmailDelivered } from '../events/welcome-email-delivered' | ||
import { WelcomeEmailDeliveryFailed } from '../events/welcome-email-delivery-failed' | ||
|
||
@EventHandler(BackgroundCheckPassed) | ||
export class SendWelcomeEmail { | ||
public static async handle(event: BackgroundCheckPassed, register: Register): Promise<void> { | ||
const mailServiceURLStr = process.env.MAIL_SERVICE_URL | ||
const mailServiceAPIKey = process.env.MAIL_SERVICE_API_KEY | ||
|
||
const profile = await Booster.entity(Profile, event.profileId); | ||
|
||
if (!profile) { | ||
throw new Error(`Profile ${event.profileId} not found`); | ||
} | ||
|
||
const { id, firstName, lastName, email } = profile; | ||
const profileData = { | ||
id, | ||
firstName, | ||
lastName, | ||
email, | ||
}; | ||
|
||
let templateId: string | ||
if (profile.country === 'Wakanda') { | ||
register.events(new PromoCodeCreated(UUID.generate(), profile.id)) | ||
templateId = 'WakandianSpecialKYCWelcomeEmailTemplate' | ||
} else { | ||
templateId = 'KYCWelcomeEmailTemplate' | ||
} | ||
|
||
if (!mailServiceURLStr) { | ||
throw new Error(`MAIL_SERVICE_URL not set: ${mailServiceURLStr}`); | ||
} | ||
const mailServiceURL = new URL(mailServiceURLStr) | ||
|
||
const mailServiceResponse = await fetch(mailServiceURL, { | ||
method: 'POST', | ||
headers: { Authorization: `Bearer ${mailServiceAPIKey}` }, | ||
body: JSON.stringify({ | ||
origin: 'kycService', | ||
templateId, | ||
...profileData, | ||
}), | ||
}); | ||
|
||
const response = await mailServiceResponse.json(); | ||
if (response.result === 'delivered') { | ||
register.events(new WelcomeEmailDelivered(profile.id, new Date().toISOString())) | ||
} else { | ||
register.events(new WelcomeEmailDeliveryFailed(profile.id, new Date().toISOString())) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Event } from '@boostercloud/framework-core' | ||
import { UUID } from '@boostercloud/framework-types' | ||
|
||
@Event | ||
export class KYCCompleted { | ||
public constructor( | ||
readonly profileId: UUID, | ||
) {} | ||
|
||
public entityID(): UUID { | ||
return this.profileId | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Event } from '@boostercloud/framework-core' | ||
import { UUID } from '@boostercloud/framework-types' | ||
|
||
@Event | ||
export class PromoCodeCreated { | ||
public constructor( | ||
readonly promoCodeId: UUID, | ||
readonly profileId: UUID, | ||
) {} | ||
|
||
public entityID(): UUID { | ||
return this.promoCodeId | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Event } from '@boostercloud/framework-core' | ||
import { UUID } from '@boostercloud/framework-types' | ||
|
||
@Event | ||
export class WelcomeEmailDelivered { | ||
public constructor( | ||
readonly profileId: UUID, | ||
readonly timestamp: string, | ||
) {} | ||
|
||
public entityID(): UUID { | ||
return this.profileId | ||
} | ||
} |
Oops, something went wrong.