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

[SPGO-64] add new table SportAreaList #18

Merged
merged 7 commits into from
Oct 24, 2023
Merged
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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"@prisma/client": "^5.3.1",
"@types/bcrypt": "^5.0.0",
"@types/passport-jwt": "^3.0.9",
"amqp-connection-manager": "^4.1.14",
"amqplib": "^0.10.3",
"bcrypt": "^5.1.1",
"grpc_tools_node_protoc_ts": "^5.3.3",
"nestjs-grpc-exceptions": "^0.2.1",
Expand Down
11 changes: 7 additions & 4 deletions src/auth/auth.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ describe('AuthController', () => {
resetPassword: jest.fn(),
register: jest.fn(),
validateGoogle: jest.fn(),
updateUserSportArea: jest.fn(),
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
providers: [{
provide: AuthService,
useValue: mockAuthService,
}]
providers: [
{
provide: AuthService,
useValue: mockAuthService,
},
],
}).compile();

controller = module.get<AuthController>(AuthController);
Expand Down
9 changes: 9 additions & 0 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
ValidateOAuthRequest,
RefreshTokenRequest,
RefreshTokenResponse,
UpdateUserSportAreaResponse,
UpdateUserSportAreaRequest,
} from './auth.pb';
import { AuthService } from './auth.service';

Expand Down Expand Up @@ -66,4 +68,11 @@ export class AuthController {
validateToken(request: ValidateTokenRequest): Promise<ValidateTokenResponse> {
return this.authService.validateToken(request);
}

@GrpcMethod('AuthService', 'UpdateUserSportArea')
updateUserSportArea(
request: UpdateUserSportAreaRequest,
): Promise<UpdateUserSportAreaResponse> {
return this.authService.updateUserSportArea(request);
}
}
9 changes: 7 additions & 2 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { Inject, Module } from '@nestjs/common';

Check warning on line 1 in src/auth/auth.module.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'Inject' is defined but never used
import { PrismaModule } from '../prisma/prisma.module';
import { UserRepository } from '../repository/user.repository';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { JwtModule, JwtService } from '@nestjs/jwt';

Check warning on line 6 in src/auth/auth.module.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'JwtService' is defined but never used
import { AccessTokenStrategy } from './strategies/accessToken.strategy';
import { ConfigService } from '@nestjs/config';
import { BlacklistRepository } from 'src/repository/blacklist.repository';

import { SportAreaListRepository } from 'src/repository/sportAreaList.repository';


import { ClientsModule, Transport } from '@nestjs/microservices';

@Module({
Expand All @@ -21,7 +25,8 @@
}
}
}])],
providers: [AuthService, UserRepository, AccessTokenStrategy, ConfigService, BlacklistRepository],
providers: [AuthService, UserRepository, AccessTokenStrategy, ConfigService, BlacklistRepository,SportAreaListRepository,],

controllers: [AuthController],
})
export class AuthModule { }
export class AuthModule {}
17 changes: 17 additions & 0 deletions src/auth/auth.pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { Observable } from "rxjs";

export const protobufPackage = "auth";

export interface UpdateUserSportAreaRequest {
userId: string;
sportAreaId: string;
}

export interface UpdateUserSportAreaResponse {
userId: string;
sportAreaId: string;
}

export interface RegisterRequest {
firstName: string;
lastName: string;
Expand Down Expand Up @@ -112,6 +122,8 @@ export interface AuthServiceClient {
validateToken(request: ValidateTokenRequest): Observable<ValidateTokenResponse>;

forgotPassword(request: ForgotPasswordRequest): Observable<ForgotPasswordResponse>;

updateUserSportArea(request: UpdateUserSportAreaRequest): Observable<UpdateUserSportAreaResponse>;
}

export interface AuthServiceController {
Expand Down Expand Up @@ -140,6 +152,10 @@ export interface AuthServiceController {
forgotPassword(
request: ForgotPasswordRequest,
): Promise<ForgotPasswordResponse> | Observable<ForgotPasswordResponse> | ForgotPasswordResponse;

updateUserSportArea(
request: UpdateUserSportAreaRequest,
): Promise<UpdateUserSportAreaResponse> | Observable<UpdateUserSportAreaResponse> | UpdateUserSportAreaResponse;
}

export function AuthServiceControllerMethods() {
Expand All @@ -153,6 +169,7 @@ export function AuthServiceControllerMethods() {
"logout",
"validateToken",
"forgotPassword",
"updateUserSportArea",
];
for (const method of grpcMethods) {
const descriptor: any = Reflect.getOwnPropertyDescriptor(constructor.prototype, method);
Expand Down
24 changes: 17 additions & 7 deletions src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,39 @@
import { ConfigService } from '@nestjs/config';
import { UserRepository } from '../repository/user.repository';
import { BlacklistRepository } from '../repository/blacklist.repository';
import { SportAreaListRepository } from '../repository/sportAreaList.repository';
describe('AuthService', () => {
let service: AuthService;
const mockJwtService = {
sign: jest.fn(),
};
const mockBlacklistService = {

Check warning on line 13 in src/auth/auth.service.spec.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'mockBlacklistService' is assigned a value but never used
insertToken: jest.fn(),
check: jest.fn(),
};
const mockUserRepository = {
getUserByEmail: jest.fn(),
updateRefreshToken: jest.fn()
}
updateRefreshToken: jest.fn(),
};

const mockConfigService = {
get: jest.fn(),
}
};
const mockBlacklistRepository = {
addOutdatedToken: jest.fn(),
}
};

const mockSportAreaListRepository = {
addSportArea: jest.fn(),
};

const mockEmailService = {
emit: jest.fn(),
}
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService,
providers: [
AuthService,
{
provide: JwtService,
useValue: mockJwtService,
Expand All @@ -49,10 +55,14 @@
provide: BlacklistRepository,
useValue: mockBlacklistRepository,
},
{
provide: SportAreaListRepository,
useValue: mockSportAreaListRepository,
},
{
provide: 'EMAIL_SERVICE',
useValue: mockEmailService,
}
},
],
}).compile();

Expand Down
31 changes: 25 additions & 6 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@
// ValidateGoogleRequest,
// ValidateGoogleResponse,
ValidateOAuthRequest,
UpdateUserSportAreaRequest,
UpdateUserSportAreaResponse,
} from './auth.pb';
import { ClientProxy, RpcException } from '@nestjs/microservices';
import { status } from '@grpc/grpc-js';
import { $Enums, Prisma } from '@prisma/client';
import { v4 as uuidv4 } from 'uuid';
import { Role } from '@prisma/client';
import { BlacklistRepository } from '../repository/blacklist.repository';
import { SportAreaListRepository } from '../repository/sportAreaList.repository';
import { JwtPayload } from './strategies/accessToken.strategy';
import * as nodemailer from 'nodemailer';

Check warning on line 37 in src/auth/auth.service.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'nodemailer' is defined but never used
import { Observable } from 'rxjs';

Check warning on line 38 in src/auth/auth.service.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'Observable' is defined but never used

@Injectable()
export class AuthService implements AuthServiceController {
Expand All @@ -40,8 +44,9 @@
private blacklistRepo: BlacklistRepository,
private jwtService: JwtService,
private configService: ConfigService,
private sportAreaListRepo: SportAreaListRepository,
@Inject('EMAIL_SERVICE') private client: ClientProxy,
) { }
) {}

public async login(request: LoginRequest): Promise<LoginResponse> {
try {
Expand Down Expand Up @@ -455,13 +460,10 @@
`,
};


this.client.emit('forgot-password', { 'mailOptions': mailOptions });
this.client.emit('forgot-password', { mailOptions: mailOptions });
// await transporter.sendMail(mailOptions);

return { resetPasswordUrl: linkToResetPassword }


return { resetPasswordUrl: linkToResetPassword };
} catch (err: any) {
console.log(err);
if (!(err instanceof RpcException)) {
Expand All @@ -473,4 +475,21 @@
throw err;
}
}
public async updateUserSportArea(
request: UpdateUserSportAreaRequest,
): Promise<UpdateUserSportAreaResponse> {
try {
const user = await this.sportAreaListRepo.addSportArea(request);

Check warning on line 482 in src/auth/auth.service.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'user' is assigned a value but never used
return request;
} catch (err: any) {
console.log(err);
if (!(err instanceof RpcException)) {
throw new RpcException({
code: status.INTERNAL,
message: 'internal server error cant add new sportarea to user',
});
}
throw err;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- CreateTable
CREATE TABLE "SportArea" (
"SportAreaId" TEXT NOT NULL,
"userId" TEXT NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "SportArea_SportAreaId_key" ON "SportArea"("SportAreaId");

-- AddForeignKey
ALTER TABLE "SportArea" ADD CONSTRAINT "SportArea_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
2 changes: 2 additions & 0 deletions src/database/migrations/20231022074911_test/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "ooo" TEXT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:

- You are about to drop the column `ooo` on the `User` table. All the data in the column will be lost.

*/
-- AlterTable
ALTER TABLE "User" DROP COLUMN "ooo";
7 changes: 7 additions & 0 deletions src/database/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ model User {
status Status @default(ACTIVE)
googleID String? @unique
facebookID String? @unique
sportArea SportArea[]
}

model SportArea {
SportAreaId String @unique
userId String
user User @relation(fields: [userId], references: [id])
}

enum Role {
Expand Down
12 changes: 12 additions & 0 deletions src/proto/auth.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,20 @@ service AuthService {
rpc Logout(LogoutRequest) returns (LogoutResponse) {}
rpc ValidateToken(ValidateTokenRequest) returns (ValidateTokenResponse) {}
rpc ForgotPassword(ForgotPasswordRequest) returns (ForgotPasswordResponse) {}
rpc UpdateUserSportArea(UpdateUserSportAreaRequest) returns (UpdateUserSportAreaResponse){}
}


message UpdateUserSportAreaRequest{
string userId = 1;
string sportAreaId=2;
}

message UpdateUserSportAreaResponse{
string userId = 1;
string sportAreaId=2;

}
message RegisterRequest {
string firstName =1;
string lastName =2;
Expand Down
22 changes: 22 additions & 0 deletions src/repository/sportAreaList.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Injectable } from '@nestjs/common';
import { PrismaClient, Status, SportArea } from '@prisma/client';
import { PrismaService } from '../prisma/service/prisma.service';
import { Prisma } from '@prisma/client';
import { UpdateUserSportAreaRequest } from 'src/auth/auth.pb';
import { UserRepository } from './user.repository';

@Injectable()
export class SportAreaListRepository {
constructor(private db: PrismaService) {}

async addSportArea(
updateInfo: UpdateUserSportAreaRequest,
): Promise<SportArea> {
return await this.db.sportArea.create({
data: {
SportAreaId: updateInfo.sportAreaId,
user: { connect: { id: updateInfo.userId } },
},
});
}
}
Loading
Loading