Skip to content
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
14 changes: 3 additions & 11 deletions src/api-key/api-key.entity.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { Base } from 'src/common/base.entity';
import { User } from 'src/user/entities/user.entity';
import {
Column,
Entity,
ManyToOne,
JoinColumn,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity('api_keys')
export class APIKey extends Base {
Expand All @@ -16,7 +9,6 @@ export class APIKey extends Base {
@Column({ length: 32, nullable: true })
comment: string;

@ManyToOne(() => User, (user) => user.apiKey)
@JoinColumn({ name: 'user_id' })
user: User;
@Column({ name: 'user_id' })
userId: string;
}
2 changes: 1 addition & 1 deletion src/auth/dto/invitation.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ export class InvitationDto {
@Expose()
@IsString()
@IsOptional()
groupId?: string;
groupId?: string | null;
}
16 changes: 7 additions & 9 deletions src/conversations/conversations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class ConversationsService {

async create(namespaceId: string, user: User) {
const conversation = this.conversationRepository.create({
namespace: { id: namespaceId },
user: { id: user.id },
namespaceId,
userId: user.id,
});
return await this.conversationRepository.save(conversation);
}
Expand All @@ -53,7 +53,7 @@ export class ConversationsService {
options?: { limit?: number; offset?: number; order?: string },
) {
const query: any = {
where: { namespace: { id: namespaceId }, user: { id: userId } },
where: { namespaceId, userId },
order: {
updatedAt: options?.order?.toUpperCase() === 'ASC' ? 'ASC' : 'DESC',
},
Expand All @@ -65,8 +65,8 @@ export class ConversationsService {

async countAll(namespaceId: string, userId: string) {
return await this.conversationRepository.countBy({
namespace: { id: namespaceId },
user: { id: userId },
namespaceId,
userId,
});
}

Expand Down Expand Up @@ -100,7 +100,7 @@ export class ConversationsService {

async createTitle(id: string, userId: string): Promise<{ title: string }> {
const conversation = await this.conversationRepository.findOneOrFail({
where: { id, user: { id: userId } },
where: { id, userId },
});
if (conversation.title) {
return { title: conversation.title };
Expand Down Expand Up @@ -166,7 +166,7 @@ export class ConversationsService {

async getDetail(id: string, user: User): Promise<ConversationDetailDto> {
const conversation = await this.conversationRepository.findOneOrFail({
where: { id, user: { id: user.id } },
where: { id, userId: user.id },
});

const detail: ConversationDetailDto = {
Expand Down Expand Up @@ -241,15 +241,13 @@ export class ConversationsService {
async get(id: string) {
return await this.conversationRepository.findOne({
where: { id },
relations: ['namespace'],
});
}

async listAll(offset: number, limit: number) {
return await this.conversationRepository.find({
skip: offset,
take: limit,
relations: ['user', 'namespace'],
});
}
}
25 changes: 5 additions & 20 deletions src/conversations/entities/conversation.entity.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Namespace } from 'src/namespaces/entities/namespace.entity';
import { User } from 'src/user/entities/user.entity';
import { Message } from 'src/messages/entities/message.entity';
import { Base } from 'src/common/base.entity';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity('conversations')
export class Conversation extends Base {
Expand All @@ -19,14 +9,9 @@ export class Conversation extends Base {
@Column({ nullable: true })
title?: string;

@ManyToOne(() => Namespace)
@JoinColumn({ name: 'namespace_id' })
namespace: Namespace;
@Column({ name: 'namespace_id' })
namespaceId: string;

@ManyToOne(() => User)
@JoinColumn({ name: 'user_id' })
user: User;

@OneToMany(() => Message, (message) => message.conversation)
messages: Message[];
@Column({ name: 'user_id' })
userId: string;
}
28 changes: 6 additions & 22 deletions src/groups/entities/group-user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
import { Base } from 'src/common/base.entity';
import { User } from 'src/user/entities/user.entity';
import { Group } from './group.entity';
import {
Entity,
PrimaryGeneratedColumn,
ManyToOne,
JoinColumn,
Index,
Column,
} from 'typeorm';
import { Namespace } from 'src/namespaces/entities/namespace.entity';
import { Index, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity('group_users')
@Index(['namespace', 'group', 'user'], {
@Index(['namespaceId', 'groupId', 'userId'], {
unique: true,
where: 'deleted_at IS NULL',
})
export class GroupUser extends Base {
@PrimaryGeneratedColumn()
id: number;

@ManyToOne(() => Namespace, { nullable: false })
@JoinColumn({ name: 'namespace_id' })
namespace: Namespace;

@ManyToOne(() => Group, { nullable: false })
@JoinColumn({ name: 'group_id' })
group: Group;
@Column({ name: 'namespace_id' })
namespaceId: string;

@Column({ name: 'group_id', nullable: false })
groupId: string;

@ManyToOne(() => User, { nullable: false })
@JoinColumn({ name: 'user_id' })
user: User;
@Column({ name: 'user_id' })
userId: string;
}
15 changes: 3 additions & 12 deletions src/groups/entities/group.entity.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { Base } from 'src/common/base.entity';
import { Namespace } from 'src/namespaces/entities/namespace.entity';
import generateId from 'src/utils/generate-id';
import {
Entity,
ManyToOne,
JoinColumn,
Column,
PrimaryColumn,
BeforeInsert,
} from 'typeorm';
import { Entity, Column, PrimaryColumn, BeforeInsert } from 'typeorm';

@Entity('groups')
export class Group extends Base {
Expand All @@ -25,7 +17,6 @@ export class Group extends Base {
@Column()
title: string;

@ManyToOne(() => Namespace, { nullable: false })
@JoinColumn({ name: 'namespace_id' })
namespace: Namespace;
@Column({ name: 'namespace_id' })
namespaceId: string;
}
2 changes: 1 addition & 1 deletion src/groups/groups.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class GroupsController {
const invitations =
await this.groupsService.listGroupInvitations(namespaceId);
const invitationMap = new Map(
invitations.map((invitation) => [invitation.group!.id, invitation]),
invitations.map((invitation) => [invitation.groupId, invitation]),
);
return groups.map((group) => {
const groupDto = plainToInstance(GroupDto, group, {
Expand Down
2 changes: 2 additions & 0 deletions src/groups/groups.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GroupUser } from './entities/group-user.entity';
import { NamespacesModule } from 'src/namespaces/namespaces.module';
import { GroupsController } from './groups.controller';
import { Invitation } from 'src/invitations/entities/invitation.entity';
import { UserModule } from 'src/user/user.module';

@Module({
providers: [GroupsService],
Expand All @@ -16,6 +17,7 @@ import { Invitation } from 'src/invitations/entities/invitation.entity';
TypeOrmModule.forFeature([GroupUser]),
TypeOrmModule.forFeature([Invitation]),
NamespacesModule,
UserModule,
],
})
export class GroupsModule {}
69 changes: 40 additions & 29 deletions src/groups/groups.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DataSource, EntityManager, IsNull, Not, Repository } from 'typeorm';
import { Group } from './entities/group.entity';
Expand All @@ -8,6 +8,7 @@ import { UpdateGroupDto } from './dto/update-group.dto';
import { User } from 'src/user/entities/user.entity';
import { NamespacesService } from 'src/namespaces/namespaces.service';
import { Invitation } from 'src/invitations/entities/invitation.entity';
import { UserService } from 'src/user/user.service';

@Injectable()
export class GroupsService {
Expand All @@ -19,36 +20,41 @@ export class GroupsService {
@InjectRepository(Invitation)
private readonly invitationsRepository: Repository<Invitation>,
private readonly dataSource: DataSource,
private readonly UserService: UserService,
private readonly namespaceService: NamespacesService,
) {}

async listGroupInvitations(namespaceId: string): Promise<Invitation[]> {
return await this.invitationsRepository.find({
where: {
namespace: { id: namespaceId },
group: Not(IsNull()),
namespaceId,
groupId: Not(IsNull()),
},
relations: ['group'],
});
}

async listGroups(namespaceId: string): Promise<Group[]> {
return await this.groupRepository.find({
where: {
namespace: { id: namespaceId },
namespaceId,
},
});
}

async get(id: string) {
return await this.groupRepository.findOne({ where: { id } });
}

async listGroupByUser(namespaceId: string, userId: string): Promise<Group[]> {
const groups = await this.groupUserRepository.find({
where: {
namespace: { id: namespaceId },
user: { id: userId },
namespaceId,
userId,
},
relations: ['group'],
});
return groups.map((user) => user.group);
return await Promise.all(groups.map((user) => this.get(user.groupId))).then(
(groups) => groups.filter((group) => !!group),
);
}

async createGroup(
Expand All @@ -57,15 +63,15 @@ export class GroupsService {
): Promise<Group> {
return await this.groupRepository.save(
this.groupRepository.create({
namespace: { id: namespaceId },
namespaceId,
title: createGroupDto.title,
}),
);
}

async getGroupsByTitle(namespaceId: string, title: string): Promise<Group[]> {
const groups = await this.groupRepository.findBy({
namespace: { id: namespaceId },
namespaceId,
title,
});
return groups;
Expand All @@ -78,9 +84,9 @@ export class GroupsService {
): Promise<boolean> {
const user = await this.groupUserRepository.findOne({
where: {
namespace: { id: namespaceId },
group: { id: groupId },
user: { id: userId },
namespaceId,
groupId,
userId,
},
});
return user !== null;
Expand All @@ -92,35 +98,40 @@ export class GroupsService {
updateGroupDto: UpdateGroupDto,
): Promise<Group> {
const group = await this.groupRepository.findOneOrFail({
where: { namespace: { id: namespaceId }, id: groupId },
where: { namespaceId, id: groupId },
});
group.title = updateGroupDto.title;
return await this.groupRepository.save(group);
}

async deleteGroup(namespaceId: string, groupId: string) {
await this.groupRepository.softDelete({
namespace: { id: namespaceId },
namespaceId,
id: groupId,
});
}

async listGroupUsers(namespaceId: string, groupId: string): Promise<User[]> {
const groupUsers = await this.groupUserRepository.find({
where: {
namespace: { id: namespaceId },
group: { id: groupId },
namespaceId,
groupId,
},
relations: ['user'],
});
return await Promise.all(
groupUsers.map((groupUser) =>
this.namespaceService
.getMemberByUserId(namespaceId, groupUser.user.id)
.getMemberByUserId(namespaceId, groupUser.userId)
.then((member) =>
Promise.resolve({
role: member ? member.role : 'member',
...groupUser.user,
this.UserService.find(groupUser.userId).then((user) => {
if (user) {
return Promise.resolve({
role: member ? member.role : 'member',
...user,
});
} else {
throw new NotFoundException('User not found.');
}
}),
),
),
Expand All @@ -140,19 +151,19 @@ export class GroupsService {
.insert()
.into(GroupUser)
.values({
namespace: { id: namespaceId },
group: { id: groupId },
user: { id: userId },
namespaceId,
groupId,
userId,
})
.orIgnore()
.execute();
}

async deleteGroupUser(namespaceId: string, groupId: string, userId: string) {
await this.groupUserRepository.softDelete({
namespace: { id: namespaceId },
group: { id: groupId },
user: { id: userId },
namespaceId,
groupId,
userId,
});
}
}
Loading