Skip to content

Commit

Permalink
Merge branch 'develop' into push-notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
adelinaenache authored May 29, 2024
2 parents b1cc431 + d546502 commit 2e2427f
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"class-validator": "^0.14.1",
"crypto-js": "^4.2.0",
"expo-server-sdk": "^3.10.0",
"express": "^4.19.2",
"install": "^0.13.0",
"ioredis": "^5.4.1",
"jest-mock-extended": "^3.0.5",
Expand Down
49 changes: 49 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions src/auth/auth.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ describe('Auth Controller Tests', () => {
});

afterEach(async () => {
await prisma.userSettings.deleteMany();
await prisma.user.deleteMany();
await prisma.verificationCode.deleteMany();
});
Expand Down
3 changes: 3 additions & 0 deletions src/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ export class AuthService {
name: name,
profilePictureUrl: profilePictureUrl,
authType,
settings: {
create: {},
},
},
select: {
id: true,
Expand Down
10 changes: 10 additions & 0 deletions src/connections/connections.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ export class ConnectionsController {
);
}

/**
* Get logged in user reviewed users
*/

@Get('/reviewed')
async getReviewedConnections(
@CurrentUser() user: User,
): Promise<ConnectionDto[]> {
return this.connectionsService.getReviewedConnections(user.id);
}
/**
* Get a connection.
*/
Expand Down
18 changes: 18 additions & 0 deletions src/connections/connections.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ export class ConnectionsService {
authType: AuthType.EXTERNAL,
headline: profileData.headline,
profilePictureUrl: profileData.profilePictureUrl,
settings: {
create: {},
},
},
include: this.includeWithUserConnection(),
}),
Expand All @@ -191,4 +194,19 @@ export class ConnectionsService {
]);
return this.convertConnectionToDto(newUser);
}

async getReviewedConnections(userId: string): Promise<ConnectionDto[]> {
const connections = await this.prisma.user.findMany({
where: {
reviewsReceived: {
some: {
postedById: userId,
},
},
},
include: this.includeWithUserConnection(),
});

return connections.map((c) => this.convertConnectionToDto(c));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- CreateTable
CREATE TABLE "UserSettings" (
"id" TEXT NOT NULL,
"reviewsVisible" BOOLEAN NOT NULL DEFAULT true,
"anonymous" BOOLEAN NOT NULL DEFAULT true,
"userId" TEXT NOT NULL,

CONSTRAINT "UserSettings_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "UserSettings_userId_key" ON "UserSettings"("userId");

-- AddForeignKey
ALTER TABLE "UserSettings" ADD CONSTRAINT "UserSettings_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "location" TEXT;
10 changes: 10 additions & 0 deletions src/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ model User {
id String @id @default(cuid())
email String? @unique
name String?
location String?
profilePictureUrl String?
socialAccounts SocialAccount[]
authType AuthType
Expand All @@ -41,6 +42,7 @@ model User {
favoriteReviews FavoriteReview[] @relation("userFavoriteReviews")
notifications Notification[] @relation("notifications")
pushToken PushToken[] @relation("pushTokens")
settings UserSettings?
}

model Connection {
Expand Down Expand Up @@ -128,3 +130,11 @@ model PushToken {
userId String
token String @id
}

model UserSettings {
id String @id @default(cuid())
reviewsVisible Boolean @default(true)
anonymous Boolean @default(true)
userId String @unique
user User @relation(fields: [userId], references: [id])
}
1 change: 1 addition & 0 deletions src/reviews/reviews.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('Reviws Controller Tests', () => {
});

beforeEach(async () => {
await prisma.userSettings.deleteMany();
await prisma.user.deleteMany();

await prisma.user.create({
Expand Down
1 change: 1 addition & 0 deletions src/schemas/user.properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const userProperties = {
isEmailVerified: { type: 'boolean' },
headline: { type: 'string' },
joinedAt: { type: 'string', format: 'date-time' },
location: { type: 'string' },
};

export const userExtraProps = {
Expand Down
31 changes: 31 additions & 0 deletions src/user/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BadRequestException,
Body,
Controller,
Delete,
Get,
Put,
Req,
Expand All @@ -26,6 +27,8 @@ import { Public } from '../../decorators/public.decorator';
import { Request, Response } from 'express';
import { LinkedInOAuthStrategyFactory } from '../../oauth/factory/linkedin/linkedin-strategy.factory';
import { AuthGuard } from '@nestjs/passport';
import { UpdateUserSettingsDto } from '../dto/update-user-settings.dto';
import { UserSettingsDto } from '../dto/user-settings.dto';

@Controller('user')
@ApiBearerAuth()
Expand Down Expand Up @@ -120,4 +123,32 @@ export class UserController {

res.status(302).redirect('/api/user/link-social/linkedin/callback');
}

/**
* Get logged in user's settings
*/
@Get('settings')
async getUserSettings(@CurrentUser() user: User): Promise<UserSettingsDto> {
return this.userService.getSettings(user.id);
}

/**
* Update logged in user's settings
*/
@Put('settings')
async updateUserSettings(
@CurrentUser() user: User,
@Body() data: UpdateUserSettingsDto,
): Promise<UserSettingsDto> {
return this.userService.updateSettings(user.id, data);
}

/**
* Delete the entire user account
*/
@Delete()
async deleteUserAccount(@CurrentUser() user: User) {
await this.userService.deleteUser(user.id);
return { ok: true };
}
}
11 changes: 11 additions & 0 deletions src/user/dto/update-user-settings.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { IsBoolean, IsOptional } from 'class-validator';

export class UpdateUserSettingsDto {
@IsBoolean()
@IsOptional()
anonymous?: boolean;

@IsBoolean()
@IsOptional()
reviewsVisible?: boolean;
}
10 changes: 10 additions & 0 deletions src/user/dto/update-user.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ export class UpdateUserDto {
example: 'Software Engineer',
})
headline?: string;

@IsOptional()
@IsString()
@ApiProperty({
name: 'location',
description: 'User location',
required: false,
example: 'Bucharest, Romania',
})
location?: string;
}
9 changes: 9 additions & 0 deletions src/user/dto/user-settings.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IsBoolean } from 'class-validator';

export class UserSettingsDto {
@IsBoolean()
anonymous: boolean;

@IsBoolean()
reviewsVisible: boolean;
}
35 changes: 33 additions & 2 deletions src/user/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3';
import { S3_CLIENT } from '../../provider/s3.provider';
import { REDIS_CLIENT } from '../../provider/redis.provider';
import { Redis } from 'ioredis';
import { v4 } from 'uuid';

import { getMimeType } from '../../utils/image';
import { UpdateUserSettingsDto } from '../dto/update-user-settings.dto';

@Injectable()
export class UserService {
Expand All @@ -35,6 +37,7 @@ export class UserService {
data: {
name: dto.name,
headline: dto.headline,
location: dto.location,
},
});
}
Expand All @@ -50,9 +53,11 @@ export class UserService {
'base64',
);

const key = `profile-pictures/${user.id}-${v4()}`;
const putObjectRequest = new PutObjectCommand({
Bucket: process.env.AWS_S3_BUCKET_NAME,
Key: `profile-pictures/${user.id}`,
Key: key,

Body: buf,
ContentType: type,
});
Expand All @@ -64,7 +69,7 @@ export class UserService {
return await this.prisma.user.update({
where: { id: user.id },
data: {
profilePictureUrl: `https://${process.env.AWS_S3_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/profile-pictures/${user.id}`,
profilePictureUrl: `https://${process.env.AWS_S3_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${key}`,
},
});
} catch (err) {
Expand Down Expand Up @@ -133,4 +138,30 @@ export class UserService {
});
}
}

updateSettings(id: string, data: UpdateUserSettingsDto) {
return this.prisma.userSettings.update({
where: {
userId: id,
},
data,
});
}

getSettings(id: string) {
return this.prisma.userSettings.findUniqueOrThrow({
where: {
userId: id,
},
});
}

async deleteUser(id: string) {
await this.prisma.userSettings.delete({ where: { userId: id } });
return this.prisma.user.delete({
where: {
id,
},
});
}
}
1 change: 1 addition & 0 deletions src/user/user.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ describe('User Controller Tests', () => {

afterAll(async () => {
try {
await prisma.userSettings.deleteMany();
await prisma.user.deleteMany();
await prisma.review.deleteMany();
await app.close();
Expand Down

0 comments on commit 2e2427f

Please sign in to comment.