Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ PORT=3001
JWT_SECRET=change_me_to_a_random_secret_at_least_32_chars
JWT_EXPIRES_IN=7d

# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=
DB_PASSWORD=postgres
DB_NAME=bytechain
DB_SSL=false

FRONTEND_URL=http://localhost:3000
APP_URL=http://localhost:3001
Expand Down
129 changes: 129 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"pdfkit": "^0.18.0",
"pg": "^8.20.0",
"pino-http": "^11.0.0",
"pino-pretty": "^13.1.3",
"qrcode": "^1.5.4",
Expand Down
34 changes: 29 additions & 5 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { WebhooksModule } from './webhooks/webhooks.module';
DB_USERNAME: Joi.string().default('postgres'),
DB_PASSWORD: Joi.string().default(''),
DB_NAME: Joi.string().default('bytechain'),
DB_SSL: Joi.boolean().default(false),

FRONTEND_URL: Joi.string().uri().default('http://localhost:3000'),
APP_URL: Joi.string().uri().default('http://localhost:3001'),
Expand All @@ -64,11 +65,34 @@ import { WebhooksModule } from './webhooks/webhooks.module';
}),
validationOptions: { abortEarly: false },
}),
TypeOrmModule.forRoot({
type: 'sqlite',
database: 'database.sqlite',
autoLoadEntities: true,
synchronize: true,
TypeOrmModule.forRootAsync({
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
const isTest = configService.get<string>('NODE_ENV') === 'test';

if (isTest) {
return {
type: 'sqlite',
database: ':memory:',
autoLoadEntities: true,
synchronize: true,
};
}

return {
type: 'postgres',
host: configService.get<string>('DB_HOST'),
port: configService.get<number>('DB_PORT'),
username: configService.get<string>('DB_USERNAME'),
password: configService.get<string>('DB_PASSWORD'),
database: configService.get<string>('DB_NAME'),
autoLoadEntities: true,
synchronize: configService.get<string>('NODE_ENV') !== 'production',
ssl: configService.get<boolean>('DB_SSL')
? { rejectUnauthorized: false }
: false,
};
},
}),
LoggerModule.forRootAsync({
inject: [ConfigService],
Expand Down
7 changes: 4 additions & 3 deletions backend/src/certificates/certificates.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('CertificateController', () => {
verifyCertificate: jest.fn(),
getAllCertificates: jest.fn(),
getCertificatesByUser: jest.fn(),
getMyCertificates: jest.fn(),
revokeCertificate: jest.fn(),
},
},
Expand All @@ -32,17 +33,17 @@ describe('CertificateController', () => {
});

describe('getMyCertificates', () => {
it('should call getCertificatesByUser with the user id from the request', async () => {
it('should call getMyCertificates with the user id from the request', async () => {
const mockReq = { user: { id: 'user-123' } };
const mockCertificates = [{ id: 'cert-1' }];
const service = testingModule.get<CertificateService>(CertificateService);
(service.getCertificatesByUser as jest.Mock).mockResolvedValue(
(service.getMyCertificates as jest.Mock).mockResolvedValue(
mockCertificates,
);

const result = await controller.getMyCertificates(mockReq);

expect(service.getCertificatesByUser).toHaveBeenCalledWith('user-123');
expect(service.getMyCertificates).toHaveBeenCalledWith('user-123');
expect(result).toBe(mockCertificates);
});
});
Expand Down
8 changes: 4 additions & 4 deletions backend/src/certificates/entities/certificate.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Certificate {
/**
* Recipient info (denormalized for easy access & PDF rendering)
*/
@Column({ nullable: true })
@Column({ type: 'varchar', nullable: true })
recipientName: string | null;

@Column()
Expand Down Expand Up @@ -57,10 +57,10 @@ export class Certificate {
/**
* Certificate lifecycle
*/
@Column()
@Column({ type: 'datetime' })
issuedAt: Date;

@Column({ nullable: true })
@Column({ type: 'datetime', nullable: true })
expiresAt: Date;

@Column({ default: true })
Expand All @@ -69,7 +69,7 @@ export class Certificate {
/**
* Optional PDF path
*/
@Column({ nullable: true })
@Column({ type: 'varchar', nullable: true })
certificatePath?: string;

/**
Expand Down
4 changes: 3 additions & 1 deletion backend/src/lessons/entities/lesson.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
UpdateDateColumn,
ManyToOne,
OneToOne,
JoinColumn,
} from 'typeorm';

@Entity('lessons')
Expand All @@ -30,10 +31,11 @@ export class Lesson {
@Column({ default: 0 })
order: number;

@Column()
@Column({ type: 'varchar' })
courseId: string;

@ManyToOne(() => Course, (course) => course.lessons, { onDelete: 'CASCADE' })
@JoinColumn({ name: 'courseId' })
course: Course;

@OneToOne(() => Quiz, (quiz) => quiz.lesson)
Expand Down
2 changes: 0 additions & 2 deletions backend/src/progress/progress.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { CertificatesModule } from 'src/certificates/certificates.module';
import { NotificationsModule } from 'src/notifications/notifications.module';
import { RewardsModule } from 'src/rewards/rewards.module';
import { UsersModule } from 'src/users/users.module';
import { WebhooksModule } from 'src/webhooks/webhooks.module';


@Module({
imports: [
Expand Down
5 changes: 3 additions & 2 deletions backend/src/quizzes/entities/question.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm';
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
import { Quiz } from './quiz.entity';

export enum QuestionType {
Expand Down Expand Up @@ -26,9 +26,10 @@ export class Question {
@Column()
correctAnswer: string;

@Column()
@Column({ type: 'varchar' })
quizId: string;

@ManyToOne(() => Quiz, (quiz) => quiz.questions, { onDelete: 'CASCADE' })
@JoinColumn({ name: 'quizId' })
quiz: Quiz;
}
2 changes: 2 additions & 0 deletions backend/src/quizzes/quizzes.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { QuizzesService } from './quizzes.service';
import { AuthModule } from '../auth/auth.module';
import { NotificationsModule } from 'src/notifications/notifications.module';
import { RewardsModule } from 'src/rewards/rewards.module';
import { UsersModule } from 'src/users/users.module';

@Module({
imports: [
Expand All @@ -18,6 +19,7 @@ import { RewardsModule } from 'src/rewards/rewards.module';
AuthModule,
NotificationsModule,
RewardsModule,
UsersModule,
],
controllers: [QuizzesController],
providers: [QuizzesService],
Expand Down
5 changes: 5 additions & 0 deletions backend/src/quizzes/quizzes.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { QuestionType } from '../quizzes/entities/question.entity';
import { SubmitQuizDto } from './dto/submit-quiz.dto';
import { NotificationsService } from 'src/notifications/notifications.service';
import { RewardsService } from 'src/rewards/rewards.service';
import { StreakService } from 'src/users/streak.service';

describe('QuizzesService', () => {
let service: QuizzesService;
Expand Down Expand Up @@ -74,6 +75,10 @@ describe('QuizzesService', () => {
provide: RewardsService,
useValue: { awardXP: jest.fn().mockResolvedValue({ xp: 0 }) },
},
{
provide: StreakService,
useValue: { updateStreak: jest.fn() },
},
],
}).compile();

Expand Down
4 changes: 2 additions & 2 deletions backend/src/quizzes/quizzes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ export class QuizzesService {
});

const savedQuiz = await this.quizRepository.save(quiz);
let quizId = savedQuiz.id;

if (createQuizDto.questions && createQuizDto.questions.length > 0) {
const questions = createQuizDto.questions.map((q) =>
this.questionRepository.create({ ...q, quizId: savedQuiz.id }),
);
await this.questionRepository.save(questions);
savedQuiz.questions = questions;
}

return savedQuiz;
return this.findOne(quizId);
}

async findByLessonId(lessonId: string): Promise<Quiz> {
Expand Down
3 changes: 2 additions & 1 deletion backend/src/rewards/rewards.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('RewardsService', () => {
create: jest.Mock;
save: jest.Mock;
};
let dataSource: { transaction: jest.Mock };
let dataSource: { transaction: jest.Mock; options: { type: string } };

beforeEach(async () => {
userRepository = {
Expand All @@ -55,6 +55,7 @@ describe('RewardsService', () => {
};

dataSource = {
options: { type: 'postgres' },
transaction: jest.fn(async (cb: (m: unknown) => Promise<void>) => {
const manager = {
findOne: jest.fn(),
Expand Down
Loading