From 2735a45377a88f83ecee2f5b2b841ccfc7bd6261 Mon Sep 17 00:00:00 2001 From: watchtower314 Date: Mon, 5 Apr 2021 07:18:12 -0400 Subject: [PATCH 1/6] allow 0 contribution without minFeeToJoin being 0 --- .../functions/src/common/business/createCommon.ts | 10 +++++++--- .../src/proposals/business/createJoinRequest.ts | 2 +- packages/types/src/entities/ICommonEntity.ts | 5 +++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/firebase/functions/src/common/business/createCommon.ts b/packages/firebase/functions/src/common/business/createCommon.ts index 6a3a713c7..d90831b8c 100644 --- a/packages/firebase/functions/src/common/business/createCommon.ts +++ b/packages/firebase/functions/src/common/business/createCommon.ts @@ -39,7 +39,9 @@ const createCommonDataValidationScheme = yup.object({ rules: yup.array(commonRuleValidationSchema).optional(), - links: yup.array(linkValidationSchema).optional() + links: yup.array(linkValidationSchema).optional(), + + zeroContribution: yup.boolean().required(), }); type CreateCommonPayload = yup.InferType; @@ -73,7 +75,8 @@ export const createCommon = async ( description, contributionType, contributionAmount, - fundingGoalDeadline + fundingGoalDeadline, + zeroContribution, } = payload; // @todo Check if user exists @@ -97,7 +100,8 @@ export const createCommon = async ( contributionType, founderId: userId, - minFeeToJoin: contributionAmount + minFeeToJoin: contributionAmount, + zeroContribution, }, register: 'na' diff --git a/packages/firebase/functions/src/proposals/business/createJoinRequest.ts b/packages/firebase/functions/src/proposals/business/createJoinRequest.ts index fed7a78b5..192229e4e 100644 --- a/packages/firebase/functions/src/proposals/business/createJoinRequest.ts +++ b/packages/firebase/functions/src/proposals/business/createJoinRequest.ts @@ -74,7 +74,7 @@ export const createJoinRequest = async (payload: CreateRequestToJoinPayload): Pr } // Check if the request is funded with less than required amount - if (common.metadata.minFeeToJoin > payload.funding) { + if (common.metadata.minFeeToJoin > payload.funding && !common.metadata.zeroContribution) { throw new CommonError('The funding cannot be less than the minimum required funding', { userMessage: `Your join request cannot be created, because the min fee to join is $${(common.metadata.minFeeToJoin / 100) .toFixed(2)}, but you provided $${(payload.funding / 100).toFixed(2)}`, diff --git a/packages/types/src/entities/ICommonEntity.ts b/packages/types/src/entities/ICommonEntity.ts index 559b76cd9..9c6511b39 100644 --- a/packages/types/src/entities/ICommonEntity.ts +++ b/packages/types/src/entities/ICommonEntity.ts @@ -111,6 +111,11 @@ export interface ICommonMetadata { * or only when they join */ contributionType: ContributionType; + + /** + * Allow users to join common with zero contribution + */ + zeroContribution: boolean; } export interface ICommonUpdate { From 0fb284218d331c642827a2bf8a09e6685cbf9e07 Mon Sep 17 00:00:00 2001 From: watchtower314 Date: Mon, 5 Apr 2021 07:50:31 -0400 Subject: [PATCH 2/6] fix tests --- packages/firebase/__tests__/funtions/common.test.ts | 3 ++- .../src/util/tests/helpers/mockers/commons/getCommon.mocker.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/firebase/__tests__/funtions/common.test.ts b/packages/firebase/__tests__/funtions/common.test.ts index 1c444d1c2..52e8e879e 100644 --- a/packages/firebase/__tests__/funtions/common.test.ts +++ b/packages/firebase/__tests__/funtions/common.test.ts @@ -21,7 +21,8 @@ const validCommonCreationPayload = { description: 'hey there, am i descriptive', contributionType: 'one-time', contributionAmount: 6500, - fundingGoalDeadline: new Date().getTime() + fundingGoalDeadline: new Date().getTime(), + zeroContribution: true, }; describe('Common Related Cloud Functions', () => { diff --git a/packages/firebase/functions/src/util/tests/helpers/mockers/commons/getCommon.mocker.ts b/packages/firebase/functions/src/util/tests/helpers/mockers/commons/getCommon.mocker.ts index f657c39b2..d06d533f6 100644 --- a/packages/firebase/functions/src/util/tests/helpers/mockers/commons/getCommon.mocker.ts +++ b/packages/firebase/functions/src/util/tests/helpers/mockers/commons/getCommon.mocker.ts @@ -24,7 +24,8 @@ jest.mock('../../../../../common/database/getCommon', () => description: 'testetest', founderId: 'Xlun3Ux94Zfc73axkiuVdkktOWf1', byline: 'testtestetstetst', - contributionType: contributionType + contributionType: contributionType, + zeroContribution: true, }, raised: 0, rules: [ From a8a71f273c0d6a7c45ddc7d82df276eba1f7a52a Mon Sep 17 00:00:00 2001 From: watchtower314 Date: Mon, 5 Apr 2021 07:56:07 -0400 Subject: [PATCH 3/6] fix createTestCommon --- packages/firebase/__tests__/helpers/createTestCommon.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/firebase/__tests__/helpers/createTestCommon.ts b/packages/firebase/__tests__/helpers/createTestCommon.ts index 4e3fda2ed..83d4ef529 100644 --- a/packages/firebase/__tests__/helpers/createTestCommon.ts +++ b/packages/firebase/__tests__/helpers/createTestCommon.ts @@ -11,7 +11,8 @@ export const createTestCommon = async (userId = 'test-user'): Promise Date: Thu, 8 Apr 2021 05:06:03 -0400 Subject: [PATCH 4/6] pr comments, and tests --- .../__tests__/funtions/proposal.test.ts | 78 +++++++++++++++++++ .../__tests__/helpers/createTestCommon.ts | 4 +- .../src/common/business/createCommon.ts | 2 +- .../proposals/business/createJoinRequest.ts | 4 +- 4 files changed, 83 insertions(+), 5 deletions(-) diff --git a/packages/firebase/__tests__/funtions/proposal.test.ts b/packages/firebase/__tests__/funtions/proposal.test.ts index 173725f74..8ed0932a8 100644 --- a/packages/firebase/__tests__/funtions/proposal.test.ts +++ b/packages/firebase/__tests__/funtions/proposal.test.ts @@ -38,6 +38,20 @@ const validFundingData = (commonId: string) => ({ title: 'I need money' }); +const validJoinDataZeroContribution = (commonId: string) => ({ + commonId, + description: 'I wanna be a part, but without paying', + funding: 0, + cardId: `test-card-id-for-common-${commonId}` +}); + +const invalidJoinDataZeroContribution = (commonId: string) => ({ + commonId, + description: 'I wanna be a part, but pay less that $5', + funding: 300, + cardId: `test-card-id-for-common-${commonId}` +}); + describe('Proposal Related Cloud Functions', () => { afterAll(async () => { await test.cleanup(); @@ -168,6 +182,70 @@ describe('Proposal Related Cloud Functions', () => { expect(response.body.type).toBe('join'); expect(response.body.commonId).toBe(common.id); }); + + it('should make a join request when funding = 0 and 0 contribution is allowed', async () => { + // Setup + const joinerId = v4(); + const founderId = v4(); + + const authToken = await getTestAuthToken(joinerId); + const common = await createTestCommon(founderId); + + const response = await proposalsApp + .post(joinEndpoint) + .send(validJoinDataZeroContribution(common.id)) + .set({ + Authorization: authToken + }); + + expect(response.body.message).toBe('Join request successfully created!'); + expect(response.body.proposerId).toBe(joinerId); + expect(response.body.type).toBe('join'); + expect(response.body.commonId).toBe(common.id); + + }); + + it('should not make a join request when 0 < funding < 5 and 0 contribution is allowed', async () => { + // Setup + const joinerId = v4(); + const founderId = v4(); + + const authToken = await getTestAuthToken(joinerId); + const common = await createTestCommon(founderId); + + const response = await proposalsApp + .post(joinEndpoint) + .send(invalidJoinDataZeroContribution(common.id)) + .set({ + Authorization: authToken + }); + + expect(response.body.error.includes(`The funding cannot be less than the minimum required funding`)).toBeTruthy(); + expect(response.body.errorCode).toBe('GenericError'); + expect(response.body.errorMessage).toBe('Your join request cannot be created, because the min fee to join is $65.00, but you provided $3.00'); + expect(response.status).toBe(400); + }); + + it('should not make a join request when funding = 0 and 0 contribution is not allowed', async () => { + // Setup + const joinerId = v4(); + const founderId = v4(); + + const authToken = await getTestAuthToken(joinerId); + const common = await createTestCommon(founderId, false); //@askAlexI if he's ok with that (having a second argument for zeroContribution) :) + + const response = await proposalsApp + .post(joinEndpoint) + .send(validJoinDataZeroContribution(common.id)) + .set({ + Authorization: authToken + }); + + expect(response.body.error.includes(`The funding cannot be less than the minimum required funding`)).toBeTruthy(); + expect(response.body.errorCode).toBe('GenericError'); + expect(response.body.errorMessage).toBe('Your join request cannot be created, because the min fee to join is $65.00, but you provided $0.00'); + expect(response.status).toBe(400); + }); }); describe('Funding Proposal Creation', () => { diff --git a/packages/firebase/__tests__/helpers/createTestCommon.ts b/packages/firebase/__tests__/helpers/createTestCommon.ts index 83d4ef529..d4ce6d3ed 100644 --- a/packages/firebase/__tests__/helpers/createTestCommon.ts +++ b/packages/firebase/__tests__/helpers/createTestCommon.ts @@ -3,7 +3,7 @@ import { ICommonEntity } from '@common/types'; import { commonApp } from './supertests'; import { getTestAuthToken } from './auth'; -export const createTestCommon = async (userId = 'test-user'): Promise => { +export const createTestCommon = async (userId = 'test-user', zeroContribution = true): Promise => { const payload = { name: 'Common Test', image: 'https://llandscapes-10674.kxcdn.com/wp-content/uploads/2019/07/lighting.jpg', @@ -12,7 +12,7 @@ export const createTestCommon = async (userId = 'test-user'): Promise; diff --git a/packages/firebase/functions/src/proposals/business/createJoinRequest.ts b/packages/firebase/functions/src/proposals/business/createJoinRequest.ts index 192229e4e..15b3219bd 100644 --- a/packages/firebase/functions/src/proposals/business/createJoinRequest.ts +++ b/packages/firebase/functions/src/proposals/business/createJoinRequest.ts @@ -73,8 +73,8 @@ export const createJoinRequest = async (payload: CreateRequestToJoinPayload): Pr ); } - // Check if the request is funded with less than required amount - if (common.metadata.minFeeToJoin > payload.funding && !common.metadata.zeroContribution) { + if (common.metadata.minFeeToJoin > payload.funding + && (!common.metadata.zeroContribution || payload.funding !== 0)) { throw new CommonError('The funding cannot be less than the minimum required funding', { userMessage: `Your join request cannot be created, because the min fee to join is $${(common.metadata.minFeeToJoin / 100) .toFixed(2)}, but you provided $${(payload.funding / 100).toFixed(2)}`, From c801bed82b815fcc7e25f84db77ea874639f7d25 Mon Sep 17 00:00:00 2001 From: watchtower314 Date: Thu, 8 Apr 2021 09:03:49 -0400 Subject: [PATCH 5/6] remove deadline and tests --- .../__tests__/funtions/__snapshots__/common.test.ts.snap | 1 - packages/firebase/__tests__/funtions/common.test.ts | 1 - packages/firebase/__tests__/helpers/createTestCommon.ts | 1 - .../firebase/functions/src/common/business/createCommon.ts | 4 ---- packages/types/src/entities/ICommonEntity.ts | 6 ------ 5 files changed, 13 deletions(-) diff --git a/packages/firebase/__tests__/funtions/__snapshots__/common.test.ts.snap b/packages/firebase/__tests__/funtions/__snapshots__/common.test.ts.snap index 235f07577..3805af90c 100644 --- a/packages/firebase/__tests__/funtions/__snapshots__/common.test.ts.snap +++ b/packages/firebase/__tests__/funtions/__snapshots__/common.test.ts.snap @@ -5,7 +5,6 @@ Array [ "You must provide a valid URL", "image is a required field", "byline is a required field", - "fundingGoalDeadline is a required field", "contributionAmount is a required field", ] `; diff --git a/packages/firebase/__tests__/funtions/common.test.ts b/packages/firebase/__tests__/funtions/common.test.ts index 52e8e879e..ca1a23454 100644 --- a/packages/firebase/__tests__/funtions/common.test.ts +++ b/packages/firebase/__tests__/funtions/common.test.ts @@ -21,7 +21,6 @@ const validCommonCreationPayload = { description: 'hey there, am i descriptive', contributionType: 'one-time', contributionAmount: 6500, - fundingGoalDeadline: new Date().getTime(), zeroContribution: true, }; diff --git a/packages/firebase/__tests__/helpers/createTestCommon.ts b/packages/firebase/__tests__/helpers/createTestCommon.ts index d4ce6d3ed..13569881a 100644 --- a/packages/firebase/__tests__/helpers/createTestCommon.ts +++ b/packages/firebase/__tests__/helpers/createTestCommon.ts @@ -11,7 +11,6 @@ export const createTestCommon = async (userId = 'test-user', zeroContribution = description: 'hey there, am i descriptive', contributionType: 'one-time', contributionAmount: 6500, - fundingGoalDeadline: new Date().getTime() / 1000, zeroContribution, }; diff --git a/packages/firebase/functions/src/common/business/createCommon.ts b/packages/firebase/functions/src/common/business/createCommon.ts index d309a818a..1161a6b8e 100644 --- a/packages/firebase/functions/src/common/business/createCommon.ts +++ b/packages/firebase/functions/src/common/business/createCommon.ts @@ -27,8 +27,6 @@ const createCommonDataValidationScheme = yup.object({ description: yup.string().required(), - fundingGoalDeadline: yup.number().required(), - contributionAmount: yup.number().min(0).required(), contributionType: yup @@ -75,7 +73,6 @@ export const createCommon = async ( description, contributionType, contributionAmount, - fundingGoalDeadline, zeroContribution, } = payload; @@ -84,7 +81,6 @@ export const createCommon = async ( const common = await commonDb.add({ name, image, - fundingGoalDeadline, rules: (rules as ICommonRule[]) || [], links: (links as ICommonLink[]) || [], diff --git a/packages/types/src/entities/ICommonEntity.ts b/packages/types/src/entities/ICommonEntity.ts index 9c6511b39..452af5f43 100644 --- a/packages/types/src/entities/ICommonEntity.ts +++ b/packages/types/src/entities/ICommonEntity.ts @@ -27,12 +27,6 @@ export interface ICommonEntity extends IBaseEntity { */ raised: number; - /** - * The timestamp after witch you are able to - * create funding proposals - */ - fundingGoalDeadline: number; - /** * List of all users, that are members of this common */ From 24eec905cb0e00f8dd0cc71ffdef16ebe83b5a12 Mon Sep 17 00:00:00 2001 From: watchtower314 Date: Thu, 8 Apr 2021 10:45:45 -0400 Subject: [PATCH 6/6] fix test --- .../src/util/tests/helpers/mockers/commons/getCommon.mocker.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/firebase/functions/src/util/tests/helpers/mockers/commons/getCommon.mocker.ts b/packages/firebase/functions/src/util/tests/helpers/mockers/commons/getCommon.mocker.ts index d06d533f6..fa1127c78 100644 --- a/packages/firebase/functions/src/util/tests/helpers/mockers/commons/getCommon.mocker.ts +++ b/packages/firebase/functions/src/util/tests/helpers/mockers/commons/getCommon.mocker.ts @@ -18,7 +18,6 @@ jest.mock('../../../../../common/database/getCommon', () => return { links: [], image: 'https://firebasestorage.googleapis.com/v0/b/common-staging-50741.appspot.com/o/public_img%2Fimg_1605603725987.jpg?alt=media&token=4fc5ab99-8f38-49f0-8d6e-83a94b30db60', - fundingGoalDeadline: 1606206379, metadata: { minFeeToJoin: 700, description: 'testetest',