Skip to content

Commit

Permalink
Merge pull request #252 from LarendsD/main
Browse files Browse the repository at this point in the history
added migration to fill null values for slugs
  • Loading branch information
dzencot authored May 3, 2023
2 parents 376ec9b + 7b6c6d6 commit 0d3ca82
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
26 changes: 15 additions & 11 deletions backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ import {
Query,
} from '@nestjs/common';
import { Response } from 'express';
import {
ApiBody,
ApiCookieAuth,
ApiCreatedResponse,
ApiOkResponse,
ApiQuery,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { LocalAuthGuard } from './local-auth.guard';
import { AuthService } from './auth.service';
import { JwtAuthGuard } from './jwt-auth.guard';
import {
ApiBody,
ApiCookieAuth,
ApiCreatedResponse,
ApiOkResponse,
ApiQuery,
ApiTags,
ApiUnauthorizedResponse
} from '@nestjs/swagger';
import { SignUpUserDto } from '../users/dto/signUp-user.dto';

@ApiTags('auth')
Expand All @@ -32,7 +32,9 @@ export class AuthController {
@UseGuards(LocalAuthGuard)
@Post('login')
@ApiBody({ type: SignUpUserDto })
@ApiCreatedResponse({ description: 'Successfully logged in! Token lasts 60 minutes!' })
@ApiCreatedResponse({
description: 'Successfully logged in! Token lasts 60 minutes!',
})
@ApiUnauthorizedResponse({ description: 'Invalid login data!' })
async login(@Req() req, @Res({ passthrough: true }) response: Response) {
return this.authService.login(req.user, response);
Expand All @@ -49,7 +51,9 @@ export class AuthController {

@Get('oauth')
@ApiQuery({ name: 'code', description: 'Auth github code' })
@ApiOkResponse({ description: 'Successfully logged with github! Token lasts 60 minutes!' })
@ApiOkResponse({
description: 'Successfully logged with github! Token lasts 60 minutes!',
})
async oAuth(@Query('code') code, @Req() req, @Res() response: Response) {
if (!code) {
const url = new URL(process.env.OAUTH_AUTHORIZE_URL);
Expand Down
2 changes: 2 additions & 0 deletions backend/src/config/data-source.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Snippets } from '../entities/snippet.entity';
import { migration1663236009774 } from '../migrations/1663236009774-migration';
import { migration1670352324202 } from '../migrations/1670352324202-migration';
import { AdduserRecoverHash1677580680097 } from '../migrations/1677580680097-AdduserRecoverHash';
import { FillNullSlugsSnippets1682678760453 } from '../migrations/1682678760453-fill-null-slugs-snippets';

export default (): DataSourceOptions => {
config();
Expand All @@ -24,6 +25,7 @@ export default (): DataSourceOptions => {
migration1663236009774,
migration1670352324202,
AdduserRecoverHash1677580680097,
FillNullSlugsSnippets1682678760453,
],
};
case 'test':
Expand Down
28 changes: 28 additions & 0 deletions backend/src/migrations/1682678760453-fill-null-slugs-snippets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable no-console */
/* eslint class-methods-use-this: ["error", { "exceptMethods": ["up", "down"] }] */
import { MigrationInterface, QueryRunner } from 'typeorm';
import { generateUniqSlug } from '../snippets/utils/generate-uniq-slug';
import { Snippets } from '../entities/snippet.entity';

export class FillNullSlugsSnippets1682678760453 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
const snippets: Snippets[] = await queryRunner.query(
`SELECT id, slug FROM snippets;`,
);
const nullSnippets = snippets.filter((snippet) => !snippet.slug);

nullSnippets.forEach(async (nullSnippet) => {
const slug = generateUniqSlug(snippets);

await queryRunner.query(
`UPDATE snippets SET slug='${slug}' WHERE id=${nullSnippet.id};`,
);
});
}

public async down(): Promise<void> {
console.log(
`Revert migrations for filling null slugs in snippets is not specified!`,
);
}
}
7 changes: 1 addition & 6 deletions backend/src/snippets/snippets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CreateSnippetDto } from './dto/create-snippet.dto';
import { UpdateSnippetDto } from './dto/update-snippet.dto';
import { Snippets } from '../entities/snippet.entity';
import { User } from '../users/interfaces/users.interface';
import { generateUniqSlug } from './utils/generate-uniq-slug';

@Injectable()
export class SnippetsService {
Expand Down Expand Up @@ -39,12 +40,6 @@ export class SnippetsService {
}

async getSlug(id: number): Promise<string> {
const generateUniqSlug = (snippets: Snippets[]): string => {
const slug = faker.random.alpha({ count: 7, casing: 'mixed' });
return !snippets.find((snippet) => snippet.slug === slug)
? slug
: generateUniqSlug(snippets);
};
const snippets = await this.snippetManager
.createQueryBuilder(Snippets, 'snippet')
.where('snippet.userId= :id', { id })
Expand Down
9 changes: 9 additions & 0 deletions backend/src/snippets/utils/generate-uniq-slug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { faker } from "@faker-js/faker";
import { Snippets } from "../../entities/snippet.entity";

export const generateUniqSlug = (snippets: Snippets[]): string => {
const slug = faker.random.alpha({ count: 7, casing: 'mixed' });
return !snippets.find((snippet) => snippet.slug === slug)
? slug
: generateUniqSlug(snippets);
};
1 change: 0 additions & 1 deletion backend/src/users/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
IsEmail,
IsNotEmpty,
IsString,
Length,
Matches,
Expand Down

0 comments on commit 0d3ca82

Please sign in to comment.