Skip to content

Commit

Permalink
add referral link command and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rotorsoft committed Nov 14, 2024
1 parent b1e9fc4 commit dd0ff27
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions libs/model/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export default (sequelize: Sequelize.Sequelize): UserModelStatic =>
selected_community_id: { type: Sequelize.STRING, allowNull: true },
profile: { type: Sequelize.JSONB, allowNull: false },
xp_points: { type: Sequelize.INTEGER, defaultValue: 0, allowNull: true },
referral_link: { type: Sequelize.STRING, allowNull: true },
},
{
timestamps: true,
Expand Down
34 changes: 34 additions & 0 deletions libs/model/src/user/CreateReferralLink.command.ts
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,
};
},
};
}
36 changes: 36 additions & 0 deletions libs/model/test/user/user-lifecycle.spec.ts
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');
});
});
7 changes: 7 additions & 0 deletions libs/schemas/src/commands/user.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ export const DeleteApiKey = {
deleted: z.boolean(),
}),
};

export const CreateReferralLink = {
input: z.object({}),
output: z.object({
referral_link: z.string(),
}),
};
1 change: 1 addition & 0 deletions libs/schemas/src/entities/user.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const User = z.object({

profile: UserProfile,
xp_points: PG_INT.default(0).nullish(),
referral_link: z.string().nullish(),

ProfileTags: z.array(ProfileTags).optional(),
ApiKey: ApiKey.optional(),
Expand Down
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 },
);
});
},
};

0 comments on commit dd0ff27

Please sign in to comment.