Skip to content

Commit e9ed561

Browse files
committed
refactor(billing): rename applyPromoCode to verifyPromoCode and update related types and logic for clarity
1 parent d782e2b commit e9ed561

6 files changed

Lines changed: 47 additions & 47 deletions

File tree

src/resolvers/billingNew.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { UserInputError } from 'apollo-server-express';
1414
import cloudPaymentsApi, { CloudPaymentsJsonData } from '../utils/cloudPaymentsApi';
1515
import * as telegram from '../utils/telegram';
1616
import { TelegramBotURLs } from '../utils/telegram';
17-
import PromoCodeService, { PromoCodeApplyResult, PromoCodeError, PromoCodeErrorCode, buildPaymentPromoData } from '../services/promoCodeService';
17+
import PromoCodeService, { PromoCodeVerifyResult, PromoCodeError, PromoCodeErrorCode, buildPaymentPromoData } from '../services/promoCodeService';
1818
import type { PaymentPromoData } from '../billing/types/paymentData';
1919
import { sanitizeUtmParams } from '../utils/utm/utm';
2020

@@ -38,9 +38,9 @@ interface ComposePaymentArgs {
3838
}
3939

4040
/**
41-
* Input data for promo code apply mutation.
41+
* Input data for promo code verification mutation.
4242
*/
43-
interface ApplyPromoCodeArgs {
43+
interface VerifyPromoCodeArgs {
4444
input: {
4545
workspaceId: string;
4646
value: string;
@@ -57,7 +57,7 @@ function throwPromoCodeGraphQLError(error: unknown): never {
5757
throw new UserInputError(error.code);
5858
}
5959

60-
throw new UserInputError(PromoCodeErrorCode.ApplyFailed);
60+
throw new UserInputError(PromoCodeErrorCode.VerifyFailed);
6161
}
6262

6363
/**
@@ -318,7 +318,7 @@ debug: ${Boolean(workspace.isDebug)}`
318318

319319
Mutation: {
320320
/**
321-
* Validates promo code for workspace and returns benefit data for client-side pricing.
321+
* Verifies promo code for workspace and returns benefit data for client-side pricing.
322322
*
323323
* Access check is handled by @requireAdmin on GraphQL schema.
324324
*
@@ -327,11 +327,11 @@ debug: ${Boolean(workspace.isDebug)}`
327327
* @param user - current authorized user
328328
* @param factories - factories for working with models
329329
*/
330-
async applyPromoCode(
330+
async verifyPromoCode(
331331
_obj: undefined,
332-
{ input }: ApplyPromoCodeArgs,
332+
{ input }: VerifyPromoCodeArgs,
333333
{ user, factories }: ResolverContextWithUser
334-
): Promise<PromoCodeApplyResult> {
334+
): Promise<PromoCodeVerifyResult> {
335335
const workspace = await factories.workspacesFactory.findById(input.workspaceId);
336336

337337
if (!workspace) {
@@ -341,7 +341,7 @@ debug: ${Boolean(workspace.isDebug)}`
341341
const promoCodeService = new PromoCodeService(factories);
342342

343343
try {
344-
return await promoCodeService.applyPromoCode(input.value, user.id, input.workspaceId);
344+
return await promoCodeService.verifyPromoCode(input.value, user.id, input.workspaceId);
345345
} catch (error) {
346346
throwPromoCodeGraphQLError(error);
347347
}

src/services/promoCodeService.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const DEFAULT_MIN_FINAL_PRICE = 1;
2020
export enum PromoCodeErrorCode {
2121
Invalid = 'PROMO_CODE_INVALID',
2222
LimitExceeded = 'PROMO_CODE_LIMIT_EXCEEDED',
23-
ApplyFailed = 'PROMO_CODE_APPLY_FAILED',
23+
VerifyFailed = 'PROMO_CODE_VERIFY_FAILED',
2424
}
2525

2626
/**
@@ -75,9 +75,9 @@ export interface PromoCodePricingResult {
7575
}
7676

7777
/**
78-
* Validated promo code data returned after apply.
78+
* Validated promo code data returned after verification.
7979
*/
80-
export interface PromoCodeApplyResult {
80+
export interface PromoCodeVerifyResult {
8181
/**
8282
* Normalized promo value.
8383
*/
@@ -268,17 +268,17 @@ export default class PromoCodeService {
268268
}
269269

270270
/**
271-
* Validates promo code and returns benefit data for client-side price calculation.
271+
* Verifies promo code and returns benefit data for client-side price calculation.
272272
*
273273
* @param value - raw promo code value
274274
* @param userId - user id
275275
* @param workspaceId - workspace id
276276
*/
277-
public async applyPromoCode(value: string, userId: string, workspaceId: string): Promise<PromoCodeApplyResult> {
277+
public async verifyPromoCode(value: string, userId: string, workspaceId: string): Promise<PromoCodeVerifyResult> {
278278
const promoCode = await this.getValidPromoCode(value, userId, workspaceId);
279279
const benefit = promoCode.benefit;
280280

281-
const result: PromoCodeApplyResult = {
281+
const result: PromoCodeVerifyResult = {
282282
value: promoCode.value,
283283
benefitType: benefit.type,
284284
};

src/typeDefs/billing.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ input ComposePaymentInput {
248248
}
249249
250250
"""
251-
Input for promo code apply
251+
Input for promo code verification
252252
"""
253-
input ApplyPromoCodeInput {
253+
input VerifyPromoCodeInput {
254254
"""
255255
Workspace id for which promo code is applied
256256
"""
@@ -273,9 +273,9 @@ enum PromoCodeBenefitType {
273273
}
274274
275275
"""
276-
Validated promo code data for client-side price calculation
276+
Verified promo code data for client-side price calculation
277277
"""
278-
type ApplyPromoCodeResponse {
278+
type VerifyPromoCodeResponse {
279279
"""
280280
Normalized promo code value
281281
"""
@@ -437,9 +437,9 @@ type PayWithCardResponse {
437437
438438
extend type Mutation {
439439
"""
440-
Validates promo code for workspace admin and returns benefit data
440+
Verifies promo code for workspace admin and returns benefit data
441441
"""
442-
applyPromoCode(input: ApplyPromoCodeInput!): ApplyPromoCodeResponse! @requireAdmin
442+
verifyPromoCode(input: VerifyPromoCodeInput!): VerifyPromoCodeResponse! @requireAdmin
443443
444444
"""
445445
Remove card

test/directives/requireAdmin.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ const typeDefs = `
1212
${requireAdminDirectiveTypeDefs}
1313
1414
type Query {
15-
applyPromoCode(input: ApplyPromoCodeInput!): String! @requireAdmin
15+
verifyPromoCode(input: VerifyPromoCodeInput!): String! @requireAdmin
1616
}
1717
18-
input ApplyPromoCodeInput {
18+
input VerifyPromoCodeInput {
1919
workspaceId: ID!
2020
value: String!
2121
}
2222
`;
2323

2424
const resolvers = {
2525
Query: {
26-
applyPromoCode: (): string => 'ok',
26+
verifyPromoCode: (): string => 'ok',
2727
},
2828
};
2929

@@ -65,8 +65,8 @@ describe('requireAdmin directive', () => {
6565
const result = await graphql({
6666
schema,
6767
source: `
68-
query ApplyPromoCode($input: ApplyPromoCodeInput!) {
69-
applyPromoCode(input: $input)
68+
query VerifyPromoCode($input: VerifyPromoCodeInput!) {
69+
verifyPromoCode(input: $input)
7070
}
7171
`,
7272
variableValues: {
@@ -79,7 +79,7 @@ describe('requireAdmin directive', () => {
7979
});
8080

8181
expect(result.errors).toBeUndefined();
82-
expect(result.data?.applyPromoCode).toBe('ok');
82+
expect(result.data?.verifyPromoCode).toBe('ok');
8383
});
8484

8585
it('should reject mutation when user is not workspace admin via input.workspaceId', async () => {
@@ -88,8 +88,8 @@ describe('requireAdmin directive', () => {
8888
const result = await graphql({
8989
schema,
9090
source: `
91-
query ApplyPromoCode($input: ApplyPromoCodeInput!) {
92-
applyPromoCode(input: $input)
91+
query VerifyPromoCode($input: VerifyPromoCodeInput!) {
92+
verifyPromoCode(input: $input)
9393
}
9494
`,
9595
variableValues: {

test/resolvers/billingNew.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ function withPromoFactories(
145145
}
146146

147147
/**
148-
* Creates test data and mocks for applyPromoCode tests.
148+
* Creates test data and mocks for verifyPromoCode tests.
149149
*/
150-
function createApplyPromoCodeTestSetup(options: {
150+
function createVerifyPromoCodeTestSetup(options: {
151151
promoCode: Record<string, unknown> | null;
152152
grantPlan?: PlanDBScheme;
153153
}): {
@@ -431,14 +431,14 @@ describe('GraphQLBillingNew', () => {
431431
});
432432
});
433433

434-
describe('applyPromoCode', () => {
434+
describe('verifyPromoCode', () => {
435435
beforeEach(() => {
436436
jest.clearAllMocks();
437437
});
438438

439439
it('should return benefit data without side effects', async () => {
440440
const promoCodeId = new ObjectId();
441-
const { mockContext, workspaceId, workspaceMock } = createApplyPromoCodeTestSetup({
441+
const { mockContext, workspaceId, workspaceMock } = createVerifyPromoCodeTestSetup({
442442
promoCode: {
443443
_id: promoCodeId,
444444
value: 'SAVE25',
@@ -449,7 +449,7 @@ describe('GraphQLBillingNew', () => {
449449
},
450450
});
451451

452-
const result = await billingNewResolver.Mutation.applyPromoCode(
452+
const result = await billingNewResolver.Mutation.verifyPromoCode(
453453
undefined,
454454
{
455455
input: {
@@ -471,7 +471,7 @@ describe('GraphQLBillingNew', () => {
471471
it('should reject unsupported grant_plan promo', async () => {
472472
const promoCodeId = new ObjectId();
473473
const grantPlanId = new ObjectId();
474-
const { mockContext, workspaceId } = createApplyPromoCodeTestSetup({
474+
const { mockContext, workspaceId } = createVerifyPromoCodeTestSetup({
475475
promoCode: {
476476
_id: promoCodeId,
477477
value: 'GRANT',
@@ -492,7 +492,7 @@ describe('GraphQLBillingNew', () => {
492492
});
493493

494494
await expect(
495-
billingNewResolver.Mutation.applyPromoCode(
495+
billingNewResolver.Mutation.verifyPromoCode(
496496
undefined,
497497
{
498498
input: {
@@ -526,7 +526,7 @@ describe('GraphQLBillingNew', () => {
526526
};
527527

528528
await expect(
529-
billingNewResolver.Mutation.applyPromoCode(
529+
billingNewResolver.Mutation.verifyPromoCode(
530530
undefined,
531531
{
532532
input: {
@@ -540,12 +540,12 @@ describe('GraphQLBillingNew', () => {
540540
});
541541

542542
it('should map promo validation errors to public codes', async () => {
543-
const { mockContext, workspaceId } = createApplyPromoCodeTestSetup({
543+
const { mockContext, workspaceId } = createVerifyPromoCodeTestSetup({
544544
promoCode: null,
545545
});
546546

547547
await expect(
548-
billingNewResolver.Mutation.applyPromoCode(
548+
billingNewResolver.Mutation.verifyPromoCode(
549549
undefined,
550550
{
551551
input: {

test/services/promoCodeService.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ describe('PromoCodeService', () => {
143143
});
144144
});
145145

146-
describe('applyPromoCode()', () => {
146+
describe('verifyPromoCode()', () => {
147147
it('should return benefit data for percent discount promo', async () => {
148148
const plan = createPlan({ monthlyCharge: 1000 });
149149
const promoCode = createPromoCode({
@@ -152,7 +152,7 @@ describe('PromoCodeService', () => {
152152
});
153153
const service = createService(promoCode, { plan });
154154

155-
const result = await service.applyPromoCode(' promo ', new ObjectId().toString(), new ObjectId().toString());
155+
const result = await service.verifyPromoCode(' promo ', new ObjectId().toString(), new ObjectId().toString());
156156

157157
expect(result).toMatchObject({
158158
value: 'PROMO',
@@ -165,7 +165,7 @@ describe('PromoCodeService', () => {
165165
it('should reject unknown promo code', async () => {
166166
const service = createService(null);
167167

168-
await expectPromoError(service.applyPromoCode('missing', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.Invalid);
168+
await expectPromoError(service.verifyPromoCode('missing', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.Invalid);
169169
});
170170

171171
it('should reject expired promo code', async () => {
@@ -177,7 +177,7 @@ describe('PromoCodeService', () => {
177177
});
178178
const service = createService(promoCode);
179179

180-
await expectPromoError(service.applyPromoCode('promo', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.Invalid);
180+
await expectPromoError(service.verifyPromoCode('promo', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.Invalid);
181181
});
182182

183183
it('should reject total usage limit', async () => {
@@ -189,7 +189,7 @@ describe('PromoCodeService', () => {
189189
});
190190
const service = createService(promoCode, { totalUses: 1 });
191191

192-
await expectPromoError(service.applyPromoCode('promo', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.LimitExceeded);
192+
await expectPromoError(service.verifyPromoCode('promo', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.LimitExceeded);
193193
});
194194

195195
it('should reject user usage limit', async () => {
@@ -199,7 +199,7 @@ describe('PromoCodeService', () => {
199199
});
200200
const service = createService(promoCode, { userUsage: {} });
201201

202-
await expectPromoError(service.applyPromoCode('promo', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.LimitExceeded);
202+
await expectPromoError(service.verifyPromoCode('promo', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.LimitExceeded);
203203
});
204204

205205
it('should reject workspace usage limit', async () => {
@@ -209,7 +209,7 @@ describe('PromoCodeService', () => {
209209
});
210210
const service = createService(promoCode, { workspaceUsage: {} });
211211

212-
await expectPromoError(service.applyPromoCode('promo', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.LimitExceeded);
212+
await expectPromoError(service.verifyPromoCode('promo', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.LimitExceeded);
213213
});
214214

215215
it('should reject invalid benefit structure', async () => {
@@ -219,7 +219,7 @@ describe('PromoCodeService', () => {
219219
});
220220
const service = createService(promoCode);
221221

222-
await expectPromoError(service.applyPromoCode('promo', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.Invalid);
222+
await expectPromoError(service.verifyPromoCode('promo', new ObjectId().toString(), new ObjectId().toString()), PromoCodeErrorCode.Invalid);
223223
});
224224
});
225225

0 commit comments

Comments
 (0)