Skip to content

Commit

Permalink
Added type field to userChallenge entity (#32)
Browse files Browse the repository at this point in the history
* Added type field to userChallenge entity

* Eslint fix

* Change the case of the enum`s values

* Moved the ChallengeTypeValues variable in to a separate file

* Reuse the ChallengeTypeValues in the validation model

---------

Co-authored-by: Viktor Riabkov <vitya.ryabkov@gmail.com>
  • Loading branch information
vmkhitaryanscn and moiskillnadne authored Nov 29, 2024
1 parent 4ea961c commit 254cc24
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/api/userChallenge/validation.schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import z from 'zod';

import { ChallengeTypeValues } from '~/shared/userChallenge';

export interface FindByParams {
id: string;
userId: string;
Expand All @@ -10,6 +12,7 @@ export const CreateChallengeSchema = z.object({
startedAtDate: z.string().date(),
duration: z.number(),
description: z.string().max(500).or(z.null()),
type: z.enum(ChallengeTypeValues as [string, ...string[]]),
});

export type CreateChallengeReqPayload = z.infer<typeof CreateChallengeSchema>;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
await queryInterface.addColumn('userChallenge', 'type', {
type: Sequelize.TEXT,
allowNull: true,
unique: false,
defaultValue: null,
});
},

async down(queryInterface, Sequelize) {
await queryInterface.removeColumn('userChallenge', 'type');
},
};
10 changes: 10 additions & 0 deletions src/database/models/UserChallenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { DataTypes } from 'sequelize';
import Sequelize from '../connection';
import { User } from './User';

import { ChallengeTypeValues } from '~/shared/userChallenge';

export const UserChallenge = Sequelize.define(
'userChallenge',
{
Expand Down Expand Up @@ -45,6 +47,14 @@ export const UserChallenge = Sequelize.define(
},
},

type: {
type: DataTypes.ENUM(...ChallengeTypeValues),
allowNull: true,
validate: {
isIn: [ChallengeTypeValues],
},
},

userId: {
type: DataTypes.UUIDV4,
allowNull: false,
Expand Down
3 changes: 3 additions & 0 deletions src/shared/userChallenge/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ChallengeType } from './types';

export const ChallengeTypeValues = Object.values(ChallengeType);
2 changes: 2 additions & 0 deletions src/shared/userChallenge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types';
export * from './constants';
8 changes: 8 additions & 0 deletions src/shared/userChallenge/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum ChallengeType {
Sport = 'SPORT',
Language = 'LANGUAGE',
Sugar = 'SUGAR',
Water = 'WATER',
Sleep = 'SLEEP',
Other = 'OTHER',
}

0 comments on commit 254cc24

Please sign in to comment.