-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rotorsoft
committed
Nov 14, 2024
1 parent
b1e9fc4
commit dd0ff27
Showing
6 changed files
with
101 additions
and
0 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
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,34 @@ | ||
import { InvalidInput, type Command } from '@hicommonwealth/core'; | ||
import * as schemas from '@hicommonwealth/schemas'; | ||
import { randomBytes } from 'crypto'; | ||
import { models } from '../database'; | ||
import { mustExist } from '../middleware/guards'; | ||
|
||
export function CreateReferralLink(): Command< | ||
typeof schemas.CreateReferralLink | ||
> { | ||
return { | ||
...schemas.CreateReferralLink, | ||
auth: [], | ||
secure: true, | ||
body: async ({ actor }) => { | ||
const user = await models.User.findOne({ | ||
where: { id: actor.user.id }, | ||
attributes: ['id', 'referral_link'], | ||
}); | ||
mustExist('User', user); | ||
|
||
if (user.referral_link) | ||
throw new InvalidInput('Referral link already exists'); | ||
|
||
const randomSegment = randomBytes(8).toString('base64url'); | ||
const referral_link = `ref_${user.id}_${randomSegment}`; | ||
|
||
await user.update({ referral_link }); | ||
|
||
return { | ||
referral_link, | ||
}; | ||
}, | ||
}; | ||
} |
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,36 @@ | ||
import { Actor, command, dispose } from '@hicommonwealth/core'; | ||
import { CreateReferralLink } from 'model/src/user/CreateReferralLink.command'; | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
import { seedCommunity } from '../utils/community-seeder'; | ||
|
||
describe('User lifecycle', () => { | ||
let member: Actor; | ||
|
||
beforeAll(async () => { | ||
const { actors } = await seedCommunity({ | ||
roles: ['member'], | ||
}); | ||
member = actors.member; | ||
}); | ||
|
||
afterAll(async () => { | ||
await dispose()(); | ||
}); | ||
|
||
it('should create referral link when user is created', async () => { | ||
const response = await command(CreateReferralLink(), { | ||
actor: member, | ||
payload: {}, | ||
}); | ||
expect(response!.referral_link).toBeDefined(); | ||
}); | ||
|
||
it('should fail to create referral link when one already exists', async () => { | ||
expect( | ||
command(CreateReferralLink(), { | ||
actor: member, | ||
payload: {}, | ||
}), | ||
).rejects.toThrowError('Referral link already exists'); | ||
}); | ||
}); |
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
22 changes: 22 additions & 0 deletions
22
packages/commonwealth/server/migrations/20241114144100-create-referral-link.js
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,22 @@ | ||
'use strict'; | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
up: async ({ sequelize }) => { | ||
await sequelize.transaction(async (transaction) => { | ||
await sequelize.query( | ||
`ALTER TABLE "Users" ADD COLUMN "referral_link" character varying(255);`, | ||
{ transaction }, | ||
); | ||
}); | ||
}, | ||
|
||
down: async ({ sequelize }) => { | ||
await sequelize.transaction(async (transaction) => { | ||
await sequelize.query( | ||
`ALTER TABLE "Users" DROP COLUMN "referral_link";`, | ||
{ transaction }, | ||
); | ||
}); | ||
}, | ||
}; |