Skip to content
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

Adds xp tables #9424

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions libs/model/src/models/associations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ export const buildAssociations = (db: DB) => {
onDelete: 'CASCADE',
})
.withMany(db.Wallets)
.withOne(db.ApiKey, {
onDelete: 'CASCADE',
});
.withMany(db.XpLog, { onDelete: 'CASCADE' })
.withOne(db.ApiKey, { onDelete: 'CASCADE' });

db.Address.withMany(db.Thread, {
asOne: 'Address',
Expand Down
2 changes: 2 additions & 0 deletions libs/model/src/models/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import User from './user';
import Vote from './vote';
import Wallets from './wallets';
import Webhook from './webhook';
import XpLog from './xp_log';

export const Factories = {
Address,
Expand Down Expand Up @@ -87,6 +88,7 @@ export const Factories = {
Vote,
Webhook,
Wallets,
XpLog,
};

export type DB = {
Expand Down
1 change: 1 addition & 0 deletions libs/model/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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 },
},
{
timestamps: true,
Expand Down
23 changes: 23 additions & 0 deletions libs/model/src/models/xp_log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { XpLog } from '@hicommonwealth/schemas';
import Sequelize from 'sequelize';
import { z } from 'zod';
import type { ModelInstance } from './types';

export type XpLogInstance = ModelInstance<z.infer<typeof XpLog>>;
export type XpLogModelStatic = Sequelize.ModelStatic<XpLogInstance>;

export default (sequelize: Sequelize.Sequelize) =>
<XpLogModelStatic>sequelize.define<XpLogInstance>(
'XpLog',
{
user_id: { type: Sequelize.INTEGER, primaryKey: true },
created_at: { type: Sequelize.DATE, primaryKey: true },
Rotorsoft marked this conversation as resolved.
Show resolved Hide resolved
event_name: { type: Sequelize.STRING, primaryKey: true },
xp_points: { type: Sequelize.INTEGER, allowNull: false },
},
{
timestamps: false,
tableName: 'XpLogs',
underscored: true,
},
);
8 changes: 8 additions & 0 deletions libs/schemas/src/entities/user.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const User = z.object({
is_welcome_onboard_flow_complete: z.boolean().default(false).optional(),

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

ProfileTags: z.array(ProfileTags).optional(),

Expand Down Expand Up @@ -105,3 +106,10 @@ export const CommunityMember = z.object({
group_ids: z.array(PG_INT),
last_active: z.any().nullish().describe('string or date'),
});

export const XpLog = z.object({
user_id: PG_INT,
created_at: z.coerce.date(),
event_name: z.string(),
xp_points: PG_INT,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.createTable(
'XpLogs',
{
user_id: {
primaryKey: true,
type: Sequelize.INTEGER,
references: {
model: 'Users',
key: 'id',
},
onDelete: 'CASCADE',
},
created_at: { type: Sequelize.DATE, primaryKey: true },
event_name: { type: Sequelize.STRING, primaryKey: true },
xp_points: { type: Sequelize.INTEGER, allowNull: false },
},
{ transaction },
);
await queryInterface.addColumn(
'Users',
'xp_points',
{
type: Sequelize.INTEGER,
defaultValue: 0,
allowNull: true,
},
{ transaction },
);
});
},

async down(queryInterface) {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.dropTable('XpLogs', { transaction });
await queryInterface.removeColumn('Users', 'xp_points', {
transaction,
});
});
},
};
Loading