Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sa committed Aug 26, 2024
1 parent 0951980 commit 56c739b
Show file tree
Hide file tree
Showing 80 changed files with 689 additions and 630 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion common/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export interface Address {
readonly street2?: string;
readonly city: string;
readonly state?: string;
readonly zip: string;
readonly zipCode: string;
}
1 change: 0 additions & 1 deletion customer-service-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './lib/dtos';
export * from './lib/entities';
export * from './lib/events';
export * from './lib/replies';
export * from './lib/services';
6 changes: 3 additions & 3 deletions customer-service-api/src/lib/services.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { RestateService } from 'deepkit-restate';
import { UUID } from '@deepkit/type';
import { Email, UUID } from '@deepkit/type';

import { Money, PersonName } from '@ftgo/common';

import { CustomerNotFound, CustomerVerificationFailed } from './replies';

export interface CustomerServiceHandlers {
create(name: PersonName): Promise<UUID>;
create(name: PersonName, email: Email): Promise<UUID>;
validateOrder(
customerId: UUID,
orderId: UUID,
Expand All @@ -15,7 +15,7 @@ export interface CustomerServiceHandlers {
}

export type CustomerServiceApi = RestateService<
'Consumer',
'Customer',
CustomerServiceHandlers,
[CustomerNotFound, CustomerVerificationFailed]
>;
2 changes: 1 addition & 1 deletion customer-service/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"projectType": "application",
"prefix": "ftgo",
"sourceRoot": "customer-service/src",
"tags": [],
"tags": ["app:service"],
"targets": {
"build": {
"executor": "@nx/vite:build",
Expand Down
17 changes: 15 additions & 2 deletions customer-service/src/customer.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { RestateRepository } from '@ftgo/common';
import { Customer } from '@ftgo/customer-service-api';
import { CustomerNotFound } from '@ftgo/customer-service-api';

export class CustomerRepository extends RestateRepository<Customer> {}
import { Customer } from './entities';

export class CustomerRepository extends RestateRepository<Customer> {
/**
* @throws CustomerNotFound
*/
async findById(id: Customer['id']): Promise<Customer> {
const customer = await this.find({ id });
if (!customer) {
throw new CustomerNotFound(id);
}
return customer;
}
}
23 changes: 10 additions & 13 deletions customer-service/src/customer.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { restate, RestateEventsPublisher } from 'deepkit-restate';
import { UUID } from '@deepkit/type';
import { cast, Email, float, UUID } from '@deepkit/type';

import { Money, PersonName } from '@ftgo/common';
import {
Customer,
CustomerCreatedEvent,
CustomerNotFound,
CustomerServiceApi,
CustomerServiceHandlers,
CustomerVerificationFailed,
Expand All @@ -16,27 +14,26 @@ import { CustomerRepository } from './customer.repository';
@restate.service<CustomerServiceApi>()
export class CustomerService implements CustomerServiceHandlers {
constructor(
private readonly consumer: CustomerRepository,
private readonly customer: CustomerRepository,
private readonly events: RestateEventsPublisher,
) {}

@restate.handler()
async create(name: PersonName): Promise<UUID> {
const consumer = await this.consumer.create(name);
await this.events.publish([new CustomerCreatedEvent(consumer)]);
return consumer.id;
async create(name: PersonName, email: Email): Promise<UUID> {
const customer = await this.customer.create(name, email);
await this.events.publish<[CustomerCreatedEvent]>([
cast<CustomerCreatedEvent>(customer),
]);
return customer.id;
}

@restate.handler()
async validateOrder(
customerId: UUID,
orderId: UUID,
orderTotal: Money,
orderTotal: float,
): Promise<void> {
const consumer = await this.consumer.find({ id: customerId });
if (!consumer) {
throw new CustomerNotFound(customerId);
}
const consumer = await this.customer.findById(customerId);
// TODO: validation
throw new CustomerVerificationFailed(customerId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
import {
Email,
entity,
JSONPartial,
PrimaryKey,
Unique,
uuid,
UUID,
} from '@deepkit/type';
import { Email, entity, PrimaryKey, Unique, uuid, UUID } from '@deepkit/type';
import { Writable } from 'type-fest';

import { PersonName } from '@ftgo/common';

import { CustomerDisabled } from './replies';
import { CustomerDisabled } from '@ftgo/customer-service-api';

@entity.name('customer')
export class Customer {
readonly id: UUID & PrimaryKey = uuid();
readonly disabled: boolean = false;

readonly name: PersonName;
readonly email: Email & Unique;
readonly phoneNumber?: string & Unique;

static create(data: JSONPartial<Customer>) {
return Object.assign(new Customer(), data);
}
constructor(
public readonly name: PersonName,
public readonly email: Email & Unique,
public readonly phoneNumber?: string & Unique,
) {}

assertEnabled(): void {
if (!this.disabled) {
Expand Down
13 changes: 2 additions & 11 deletions customer-service/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import { App } from '@deepkit/app';
import { FrameworkModule } from '@deepkit/framework';
import { RestateKafkaProducerModule } from 'deepkit-restate/kafka';
import { RestateModule } from 'deepkit-restate';

import { Customer } from '@ftgo/customer-service-api';
import { provideDatabase } from '@ftgo/common';

import { CustomerServiceConfig } from './config';
import { CustomerService } from './customer.service';
import { CustomerRepository } from './customer.repository';
import { Customer } from './entities';

void new App({
config: CustomerServiceConfig,
imports: [
new FrameworkModule(),
new RestateModule(),
// TODO: should be configurable through RestateModule
new RestateKafkaProducerModule({
clientId: 'customer-service',
brokers: [''],
}),
],
imports: [new FrameworkModule(), new RestateModule()],
controllers: [CustomerService],
providers: [provideDatabase([Customer]), CustomerRepository],
})
Expand Down
1 change: 0 additions & 1 deletion delivery-service-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './lib/entities';
export * from './lib/services';
8 changes: 0 additions & 8 deletions delivery-service-api/src/lib/entities.ts

This file was deleted.

2 changes: 1 addition & 1 deletion delivery-service/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"projectType": "application",
"prefix": "ftgo",
"sourceRoot": "delivery-service/src",
"tags": [],
"tags": ["app:service"],
"targets": {
"build": {
"executor": "@nx/vite:build",
Expand Down
3 changes: 2 additions & 1 deletion delivery-service/src/delivery.repository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RestateRepository } from '@ftgo/common';
import { Delivery } from '@ftgo/delivery-service-api';

import { Delivery } from './entities';

export class DeliveryRepository extends RestateRepository<Delivery> {}
2 changes: 1 addition & 1 deletion delivery-service/src/delivery.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { restate, RestateServiceContext } from 'deepkit-restate';
import { restate } from 'deepkit-restate';

import {
DeliveryServiceApi,
Expand Down
6 changes: 6 additions & 0 deletions delivery-service/src/entities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { entity, PrimaryKey, uuid, UUID } from '@deepkit/type';

@entity.name('delivery')
export class Delivery {
readonly id: UUID & PrimaryKey = uuid();
}
2 changes: 1 addition & 1 deletion delivery-service/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { FrameworkModule } from '@deepkit/framework';
import { RestateModule } from 'deepkit-restate';

import { provideDatabase } from '@ftgo/common';
import { Delivery } from '@ftgo/delivery-service-api';

import { DeliveryServiceConfig } from './config';
import { DeliveryRepository } from './delivery.repository';
import { DeliveryService } from './delivery.service';
import { Delivery } from './entities';

void new App({
config: DeliveryServiceConfig,
Expand Down
4 changes: 2 additions & 2 deletions infra/postgres/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ FROM postgres:16.4-alpine3.19 AS base

FROM base AS ext-build
RUN apk add --no-cache gcc make musl-dev llvm15-dev clang15 unzip
ADD https://github.com/kraftcloud/pg_ukc_scaletozero/archive/refs/heads/stable.zip /tmp/src.zip
ADD https://github.com/kraftcloud/pg_ukc_scaletozero/archive/refs/heads/stable.zip /tmp/src.zipCode
RUN mkdir /src && \
unzip /tmp/src.zip -d /tmp/extract && \
unzip /tmp/src.zipCode -d /tmp/extract && \
mv /tmp/extract/pg_ukc_scaletozero-*/* /src
RUN cd /src && \
make && \
Expand Down
4 changes: 0 additions & 4 deletions infra/restate/config.prod.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@ storage-low-priority-bg-threads = 1
default-thread-pool-size = 1
rocksdb-bg-threads = 1
bootstrap-num-partitions = 1

[worker.invoker.retry-policy]
initial-interval = "100ms"
max-attempts = 5
2 changes: 1 addition & 1 deletion infra/restate/config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
base-dir = "/var/lib/restate"

[worker.invoker.retry-policy]
initial-interval = "50ms"
initial-interval = "100ms"
max-attempts = 3
2 changes: 1 addition & 1 deletion kitchen-service-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './lib/entities';
export * from './lib/replies';
export * from './lib/services';
export * from './lib/types';
7 changes: 7 additions & 0 deletions kitchen-service-api/src/lib/replies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ export class TicketConfirmed {
export class TicketCreated {
constructor(public readonly ticketId: UUID) {}
}

export class TicketRejected {
constructor(
public readonly ticketId: UUID,
public readonly rejectedAt: Date,
) {}
}
20 changes: 6 additions & 14 deletions kitchen-service-api/src/lib/services.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { RestateService } from 'deepkit-restate';
import { UUID } from '@deepkit/type';

import {
Restaurant,
RestaurantCreatedEvent,
RestaurantMenu,
} from '@ftgo/restaurant-service-api';

import { Ticket, TicketDetails } from './entities';
import { TicketCreated, TicketNotFound } from './replies';
import { TicketDetails } from './types';

export interface KitchenServiceHandlers {
createTicket(
Expand All @@ -17,13 +11,11 @@ export interface KitchenServiceHandlers {
details: TicketDetails,
confirmAwakeableId: string,
): Promise<TicketCreated>;
confirmTicket(id: UUID, readyAt: Date): Promise<Ticket>;
cancelTicket(id: UUID): Promise<Ticket>;
beginCancelTicket(restaurantId: UUID, orderId: UUID): Promise<Ticket>;
undoBeginCancelTicket(restaurantId: UUID, orderId: UUID): Promise<Ticket>;
confirmCancelTicket(restaurantId: UUID, orderId: UUID): Promise<Ticket>;
createMenu(event: RestaurantCreatedEvent): Promise<void>;
reviseMenu(event: RestaurantCreatedEvent): Promise<void>;
confirmTicket(id: UUID, readyAt: Date): Promise<void>;
rejectTicket(id: UUID, reason: string): Promise<void>;
beginCancelTicket(restaurantId: UUID, orderId: UUID): Promise<void>;
undoBeginCancelTicket(restaurantId: UUID, orderId: UUID): Promise<void>;
confirmCancelTicket(restaurantId: UUID, orderId: UUID): Promise<void>;
}

export type KitchenServiceApi = RestateService<
Expand Down
11 changes: 11 additions & 0 deletions kitchen-service-api/src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { integer, Positive, UUID } from '@deepkit/type';

export interface TicketLineItem {
readonly quantity: integer & Positive;
readonly menuItemId: UUID;
readonly name: string;
}

export interface TicketDetails {
readonly lineItems: readonly TicketLineItem[];
}
5 changes: 3 additions & 2 deletions kitchen-service/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"projectType": "application",
"prefix": "ftgo",
"sourceRoot": "kitchen-service/src",
"tags": [],
"tags": ["app:service"],
"targets": {
"build": {
"executor": "@nx/vite:build",
Expand Down Expand Up @@ -38,7 +38,8 @@
"watch": true
},
"production": {
"buildTarget": "kitchen-service:build:production"
"buildTarget": "kitchen-service:build:production",
"args": ["server:start"]
},
"staging": {
"buildTarget": "kitchen-service:build:staging"
Expand Down
Loading

0 comments on commit 56c739b

Please sign in to comment.