Skip to content

Commit

Permalink
fix(tickets): correctly handle ticket creation failures
Browse files Browse the repository at this point in the history
  • Loading branch information
getlarge committed Apr 15, 2024
1 parent e5d9811 commit 04c5d04
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions apps/tickets/src/app/tickets/tickets.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { InjectModel } from '@nestjs/mongoose';
import { Relationship } from '@ory/client';
import {
EventsMap,
OrderCancelledEvent,
Expand Down Expand Up @@ -54,38 +55,48 @@ export class TicketsService {
}

async create(ticket: CreateTicket, currentUser: User): Promise<Ticket> {
await using manager = await transactionManager(this.ticketModel);
const res = await manager.wrap<Ticket>(async (session) => {
const doc: CreateTicket & { userId: string } = {
...ticket,
userId: currentUser.id,
};
const docs = await this.ticketModel.create([doc], {
session,
});
const newTicket = docs[0].toJSON<Ticket>();
this.logger.debug(`Created ticket ${newTicket.id}`);
let createdRelation: Relationship | undefined;
try {
await using manager = await transactionManager(this.ticketModel);
const res = await manager.wrap<Ticket>(async (session) => {
const doc: CreateTicket & { userId: string } = {
...ticket,
userId: currentUser.id,
};
const docs = await this.ticketModel.create([doc], {
session,
});
const newTicket = docs[0].toJSON<Ticket>();
this.logger.debug(`Created ticket ${newTicket.id}`);

const relationTuple = relationTupleBuilder()
.subject(PermissionNamespaces[Resources.USERS], currentUser.id)
.isIn('owners')
.of(PermissionNamespaces[Resources.TICKETS], newTicket.id);
const createRelationshipBody = createRelationQuery(
relationTuple.toJSON(),
).unwrapOrThrow();
const { data } = await this.oryRelationshipsService.createRelationship({
createRelationshipBody,
});
createdRelation = data;
this.logger.debug(`Created relation ${relationTuple.toString()}`);

const relationTuple = relationTupleBuilder()
.subject(PermissionNamespaces[Resources.USERS], currentUser.id)
.isIn('owners')
.of(PermissionNamespaces[Resources.TICKETS], newTicket.id)
.toJSON();
const createRelationshipBody =
createRelationQuery(relationTuple).unwrapOrThrow();
await this.oryRelationshipsService.createRelationship({
createRelationshipBody,
await lastValueFrom(this.sendEvent(Patterns.TicketCreated, newTicket));
this.logger.debug(`Sent event ${Patterns.TicketCreated}`);
return newTicket;
});
this.logger.debug(`Created relation ${relationTuple.toString()}`);

await lastValueFrom(this.sendEvent(Patterns.TicketCreated, newTicket));
this.logger.debug(`Sent event ${Patterns.TicketCreated}`);
return newTicket;
});
if (res.error) {
throw res.error;
if (res.error) {
throw res.error;
}
return res.value;
} catch (error) {
if (createdRelation) {
await this.oryRelationshipsService.deleteRelationships(createdRelation);
}
throw error;
}
return res.value;
}

paginate(params: PaginateDto = {}): Promise<{
Expand Down

0 comments on commit 04c5d04

Please sign in to comment.