Skip to content

Commit

Permalink
fix: working
Browse files Browse the repository at this point in the history
  • Loading branch information
darraghoriordan committed Mar 29, 2023
1 parent df885cd commit 4e3c175
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 54 deletions.
9 changes: 8 additions & 1 deletion src/authorization/services/UserValidation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ export class UserValidationService {
user: User,
authProviderPermissions: string[] | undefined
): RequestUser {
console.debug({list: this.config.superUserIds}, "super user ids");
console.debug(
{
list: this.config.superUserIds,
userId: user.uuid,
userMail: user.email,
},
"super user ids"
);
if (!user?.id) {
throw new Error(
"Unable to authenticate and register a valid user with this auth0 user"
Expand Down
2 changes: 1 addition & 1 deletion src/invitations/invitation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,6 @@ export class InvitationService {
orgId: invitation.organisationMembership.organisation.id,
user: currentUser,
});
return this.invitationRepository.remove(invitation);
return this.invitationRepository.softRemove(invitation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Type} from "class-transformer";
import {
Column,
CreateDateColumn,
DeleteDateColumn,
Entity,
Generated,
Index,
Expand Down Expand Up @@ -73,6 +72,7 @@ export class OrganisationMembership {
@OneToMany(() => MembershipRole, (role) => role.membership, {
eager: true,
cascade: ["insert", "update"],
onDelete: "CASCADE",
orphanedRowAction: "delete",
})
@Type(() => MembershipRole)
Expand All @@ -85,8 +85,4 @@ export class OrganisationMembership {
@UpdateDateColumn()
@ApiProperty()
updateDate!: Date;

@DeleteDateColumn()
@ApiPropertyOptional()
deletedDate?: Date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,7 @@ export class OrganisationMembershipsController {
@Param("membershipUuid") membershipUuid: string,
@Request() request: RequestWithUser
): Promise<BooleanResult> {
const deleteResult = await this.omService.remove(
orgUuid,
membershipUuid,
request.user.id
);
return {
result:
deleteResult !== undefined &&
deleteResult.affected !== undefined &&
deleteResult?.affected !== null &&
deleteResult?.affected > 0,
};
await this.omService.remove(orgUuid, membershipUuid, request.user.id);
return {result: true};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {OrganisationMembershipsController} from "./organisation-memberships.cont
import {OrganisationMembershipsService} from "./organisation-memberships.service.js";
import {Organisation} from "../organisation/entities/organisation.entity.js";
import {MembershipRole} from "../organisation/entities/member-role.entity.js";
import {Invitation} from "../invitations/entities/invitation.entity.js";

@Module({
imports: [
TypeOrmModule.forFeature([
Organisation,
OrganisationMembership,
MembershipRole,
Invitation,
]),
],
controllers: [OrganisationMembershipsController],
Expand Down
17 changes: 12 additions & 5 deletions src/organisation-memberships/organisation-memberships.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Injectable, NotFoundException} from "@nestjs/common";
import {InjectRepository} from "@nestjs/typeorm";
import {DeleteResult, Repository} from "typeorm";
import {Repository} from "typeorm";
import {Invitation} from "../invitations/entities/invitation.entity.js";
import {Roles} from "../organisation/dto/RolesEnum.js";
import {MembershipRole} from "../organisation/entities/member-role.entity.js";
import {Organisation} from "../organisation/entities/organisation.entity.js";
Expand All @@ -13,7 +14,9 @@ export class OrganisationMembershipsService {
@InjectRepository(Organisation)
private orgRepo: Repository<Organisation>,
@InjectRepository(OrganisationMembership)
private membershipRepo: Repository<OrganisationMembership>
private membershipRepo: Repository<OrganisationMembership>,
@InjectRepository(Invitation)
private invitationRepo: Repository<Invitation>
) {}
private notFoundMessage =
"Organisation not found or you are not owner of it";
Expand All @@ -39,7 +42,7 @@ export class OrganisationMembershipsService {
if (!isAMember) {
throw new Error("You are not a member of this organisation");
}
console.log("memberships", memberships);

return memberships;
}
async createOrUpdate(
Expand Down Expand Up @@ -116,7 +119,7 @@ export class OrganisationMembershipsService {
orgUuid: string,
membershipUuid: string,
currentUserId: number
): Promise<DeleteResult> {
): Promise<void> {
// find the org
const org = await this.orgRepo.findOne({
where: {
Expand All @@ -141,6 +144,10 @@ export class OrganisationMembershipsService {
if (!membership) {
throw new Error("Membership not found");
}
return await this.membershipRepo.delete(membership.id);
if (membership.invitations) {
await this.invitationRepo.softRemove(membership.invitations);
}

await this.membershipRepo.remove(membership);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,14 @@ export class OrganisationSubscriptionService {
if (!result) {
throw new NotFoundException("Subscription not found");
}

await this.orgSubRepository.softRemove(result);
await this.queue.add({
organisationUuid: result.organisation.uuid,
subscriptionUuid: result.uuid,
productKey: result.internalSku,
active: false,
});

await this.orgSubRepository.delete(result);
return true;
}
}
13 changes: 2 additions & 11 deletions src/organisation/organisation.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,7 @@ export class OrganisationController {
@Param("uuid") uuid: string,
@Request() request: RequestWithUser
): Promise<BooleanResult> {
const deleteResult = await this.organisationService.remove(
uuid,
request.user.id
);
return {
result:
deleteResult !== undefined &&
deleteResult.affected !== undefined &&
deleteResult?.affected !== null &&
deleteResult?.affected > 0,
};
await this.organisationService.remove(uuid, request.user.id);
return {result: true};
}
}
10 changes: 4 additions & 6 deletions src/organisation/organisation.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Injectable, NotFoundException} from "@nestjs/common";
import {InjectRepository} from "@nestjs/typeorm";
import {DeleteResult, Repository, UpdateResult} from "typeorm";
import {Repository, UpdateResult} from "typeorm";
import {OrganisationMembership} from "../organisation-memberships/entities/organisation-membership.entity.js";
import {CreateOrganisationDto} from "./dto/create-organisation.dto.js";
import {Roles} from "./dto/RolesEnum.js";
Expand Down Expand Up @@ -105,8 +105,8 @@ export class OrganisationService {
return this.repository.update(ownerOfOrg.id, ownerOfOrg);
}

async remove(uuid: string, currentUserId: number): Promise<DeleteResult> {
const ownerOfOrg = await this.repository.findOneOrFail({
async remove(uuid: string, currentUserId: number): Promise<void> {
const ownedOrg = await this.repository.findOneOrFail({
where: {
uuid,
memberships: {
Expand All @@ -120,8 +120,6 @@ export class OrganisationService {
},
});

return this.repository.delete({
id: ownerOfOrg.id,
});
await this.repository.softRemove(ownedOrg);
}
}
3 changes: 3 additions & 0 deletions src/payment-sessions/payment-session.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ export class PaymentSessionReference {
@ApiProperty()
createdDate!: Date;

// note that this is not a database relation
@Column({nullable: true})
organisationUuid?: string;

// This is nullable because it we have a sample "not authenticated" controller
// in this set of modules.
// but you most likely want to make this non-nullable
// also note this is not a relation, it's just a string
@Column({nullable: true})
userUuid?: string;
}
6 changes: 3 additions & 3 deletions src/user-api-key/user-apikey.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export class UserApiKeyService {
if (!userApiKey) {
throw new NotFoundException();
}

const result = await this.repository.remove(userApiKey);
return result.deletedDate !== undefined;
// note, no soft delete here. Once a key is gone it should be gone
await this.repository.remove(userApiKey);
return true;
}
}
7 changes: 1 addition & 6 deletions src/user-api-key/userApiKey.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {ApiProperty, ApiPropertyOptional} from "@nestjs/swagger";
import {ApiProperty} from "@nestjs/swagger";
import {
Column,
CreateDateColumn,
DeleteDateColumn,
Entity,
Generated,
Index,
Expand Down Expand Up @@ -49,8 +48,4 @@ export class UserApiKey {
@UpdateDateColumn()
@ApiProperty()
updateDate!: Date;

@DeleteDateColumn()
@ApiPropertyOptional()
deletedDate?: Date;
}
3 changes: 3 additions & 0 deletions src/user/dto/userResponseDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export class UserDto {
@ApiProperty({type: () => OrganisationMembership, isArray: true})
memberships!: OrganisationMembership[];

@ApiProperty({type: String, isArray: true})
activeSubscriptionProductKeys!: string[];

@ApiProperty()
createdDate!: Date;

Expand Down
13 changes: 13 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ export class UserController {
uuid,
request.user
);
const activePaidForProducts = new Set<string>(
result.memberships
?.flatMap((m) => m.organisation.subscriptionRecords || [])
?.filter((s) => s && s.validUntil > new Date())
?.map((s) => s?.internalSku) || []
);
return {
...result,
activeSubscriptionProductKeys: [...activePaidForProducts],
memberships: request.user.memberships ?? [],
};
}
Expand All @@ -78,6 +85,9 @@ export class UserController {

@Patch(":uuid")
@ApiOkResponse({type: BooleanResult})
@ApiOperation({
description: "Limited to Super Admin role or the user themselves.",
})
async update(
@Param("uuid") uuid: string,
@Body() updateUserDto: UpdateUserDto,
Expand All @@ -92,6 +102,9 @@ export class UserController {
}

@Delete(":uuid")
@ApiOperation({
description: "Limited to Super Admin role or the user themselves.",
})
@ApiOkResponse({type: BooleanResult})
async remove(
@Param("uuid") uuid: string,
Expand Down
4 changes: 2 additions & 2 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ export class UserService {
)
) {
throw new Error(
"Can't remove the owner of an organisation. Un-assign a new owner first."
"Can't remove the owner of an organisation. Assign a new owner first."
);
}
}

return this.repository.remove(user);
return this.repository.softRemove(user);
}

private isCurrentUserGuard(
Expand Down

0 comments on commit 4e3c175

Please sign in to comment.