-
Notifications
You must be signed in to change notification settings - Fork 116
feat: recruiter subscription #3341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8583c9
feat: recruiter subscription
capJavert ee93db6
feat: parse custom data directly
capJavert 749a082
fix: org can be optional
capJavert dee78a8
refactor: paid to subscriptionStatus
capJavert 272a0f4
feat: updatedAt for sub flags
capJavert 11e2abf
Merge branch 'main' into recruiter-subscription
capJavert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,52 @@ | ||
| import { EventName, type EventEntity } from '@paddle/paddle-node-sdk'; | ||
| import { PurchaseType, SubscriptionProvider } from '../../plus'; | ||
| import { logger } from '../../../logger'; | ||
| import { logPaddleAnalyticsEvent } from '../index'; | ||
| import { AnalyticsEventName } from '../../../integrations/analytics'; | ||
| import { | ||
| cancelRecruiterSubscription, | ||
| createOpportunitySubscription, | ||
| } from './processing'; | ||
| import { notifyNewPaddleRecruiterTransaction } from '../slack'; | ||
|
|
||
| export const processRecruiterPaddleEvent = async (event: EventEntity) => { | ||
| switch (event?.eventType) { | ||
| case EventName.SubscriptionCreated: | ||
| await createOpportunitySubscription({ event }); | ||
|
|
||
| break; | ||
| case EventName.SubscriptionCanceled: | ||
| await Promise.all([ | ||
| await cancelRecruiterSubscription({ event }), | ||
| logPaddleAnalyticsEvent(event, AnalyticsEventName.CancelSubscription), | ||
| ]); | ||
|
|
||
| break; | ||
| case EventName.SubscriptionUpdated: | ||
| logger.info( | ||
| { | ||
| provider: SubscriptionProvider.Paddle, | ||
| purchaseType: PurchaseType.Recruiter, | ||
| event, | ||
| }, | ||
| 'Subscription updated', | ||
| ); | ||
|
|
||
| break; | ||
| case EventName.TransactionCompleted: | ||
| await Promise.all([ | ||
| logPaddleAnalyticsEvent(event, AnalyticsEventName.ReceivePayment), | ||
| notifyNewPaddleRecruiterTransaction({ event }), | ||
| ]); | ||
|
|
||
| break; | ||
| default: | ||
| logger.info( | ||
| { | ||
| provider: SubscriptionProvider.Paddle, | ||
| purchaseType: PurchaseType.Recruiter, | ||
| }, | ||
| event?.eventType, | ||
| ); | ||
| } | ||
| }; |
This file contains hidden or 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,125 @@ | ||
| import type { | ||
| SubscriptionCanceledEvent, | ||
| SubscriptionCreatedEvent, | ||
| } from '@paddle/paddle-node-sdk'; | ||
| import { extractSubscriptionCycle, getPaddleSubscriptionData } from '../index'; | ||
| import type { PaddleSubscriptionEvent } from '../../../paddle'; | ||
| import createOrGetConnection from '../../../db'; | ||
| import { | ||
| PurchaseType, | ||
| SubscriptionProvider, | ||
| SubscriptionStatus, | ||
| } from '../../plus/subscription'; | ||
| import { logger } from '../../../logger'; | ||
| import { OpportunityJob } from '../../../entity/opportunities/OpportunityJob'; | ||
| import { recruiterPaddleCustomDataSchema } from './types'; | ||
| import { | ||
| ensureOpportunityPermissions, | ||
| OpportunityPermissions, | ||
| } from '../../opportunity/accessControl'; | ||
| import { updateSubscriptionFlags } from '../../utils'; | ||
|
|
||
| export const createOpportunitySubscription = async ({ | ||
| event, | ||
| }: { | ||
| event: SubscriptionCreatedEvent; | ||
| }) => { | ||
| const data = getPaddleSubscriptionData({ event }); | ||
| const con = await createOrGetConnection(); | ||
| const { opportunity_id, user_id } = recruiterPaddleCustomDataSchema.parse( | ||
| event.data.customData, | ||
| ); | ||
|
|
||
| const subscriptionType = extractSubscriptionCycle( | ||
| data.items as PaddleSubscriptionEvent['data']['items'], | ||
| ); | ||
|
|
||
| if (!subscriptionType) { | ||
| logger.error( | ||
| { | ||
| provider: SubscriptionProvider.Paddle, | ||
| purchaseType: PurchaseType.Recruiter, | ||
| data: event, | ||
| }, | ||
| 'Subscription type missing in payload', | ||
| ); | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| const opportunity: Pick<OpportunityJob, 'id'> = await con | ||
| .getRepository(OpportunityJob) | ||
| .findOneOrFail({ | ||
| select: ['id'], | ||
| where: { | ||
| id: opportunity_id, | ||
| }, | ||
| }); | ||
|
|
||
| await ensureOpportunityPermissions({ | ||
| con: con.manager, | ||
| userId: user_id, | ||
| opportunityId: opportunity_id, | ||
| permission: OpportunityPermissions.Edit, | ||
| }); | ||
|
|
||
| await con.getRepository(OpportunityJob).update( | ||
| { | ||
| id: opportunity.id, | ||
| }, | ||
| { | ||
| subscriptionFlags: updateSubscriptionFlags<OpportunityJob>({ | ||
| cycle: subscriptionType, | ||
| createdAt: data.startedAt ?? new Date(), | ||
| updatedAt: new Date(), | ||
| subscriptionId: data.id, | ||
| priceId: data.items[0].price.id, | ||
| provider: SubscriptionProvider.Paddle, | ||
| status: SubscriptionStatus.Active, | ||
| }), | ||
| }, | ||
| ); | ||
| }; | ||
|
|
||
| export const cancelRecruiterSubscription = async ({ | ||
| event, | ||
| }: { | ||
| event: SubscriptionCanceledEvent; | ||
| }) => { | ||
| const con = await createOrGetConnection(); | ||
| const { opportunity_id, user_id } = recruiterPaddleCustomDataSchema.parse( | ||
| event.data.customData, | ||
| ); | ||
|
|
||
| const opportunity: Pick<OpportunityJob, 'id'> = await con | ||
| .getRepository(OpportunityJob) | ||
| .findOneOrFail({ | ||
| select: ['id'], | ||
| where: { | ||
| id: opportunity_id, | ||
| }, | ||
| }); | ||
|
|
||
| await ensureOpportunityPermissions({ | ||
| con: con.manager, | ||
| userId: user_id, | ||
| opportunityId: opportunity_id, | ||
| permission: OpportunityPermissions.Edit, | ||
| }); | ||
|
|
||
| const subscriptionFlags: OpportunityJob['subscriptionFlags'] = { | ||
| cycle: null, | ||
| status: SubscriptionStatus.Expired, | ||
| updatedAt: new Date(), | ||
| }; | ||
|
|
||
| con.getRepository(OpportunityJob).update( | ||
| { | ||
| id: opportunity.id, | ||
| }, | ||
| { | ||
| subscriptionFlags: | ||
| updateSubscriptionFlags<OpportunityJob>(subscriptionFlags), | ||
| }, | ||
| ); | ||
| }; |
This file contains hidden or 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,6 @@ | ||
| import z from 'zod'; | ||
|
|
||
| export const recruiterPaddleCustomDataSchema = z.object({ | ||
| user_id: z.string(), | ||
| opportunity_id: z.uuid(), | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just formatting, there were extra spaces :hidethepain: