Skip to content

Instantly share code, notes, and snippets.

@MaxLarue
MaxLarue / event-service.ts
Created July 9, 2022 16:22
event-service asynchronous
export type EventType = string;
export interface Event {
type: EventType;
}
export interface EventPublisher {
publish<T extends Event>(event: Event): Promise<void>
}
@MaxLarue
MaxLarue / rating-appointment.listener.ts
Last active July 9, 2022 15:59
appointment-rating-listener
@Injectable()
export class RatingAppointmentListener {
constructor(@Inject(EVENT_SUBSCRIBE_SERVICE_TOKEN) eventSubscribeService: EventSubscribeService,
@Inject(RatingGateway) private readonly ratingGateway: RatingGateway) {
this.onAppointmentCreated = this.onAppointmentCreated.bind(this)
eventSubscribeService.subscribe(APPOINTMENT_CREATED_EVENT_TYPE, this.onAppointmentCreated)
}
async onAppointmentCreated(event: AppointmentCreatedEvent) {
@MaxLarue
MaxLarue / appointment.service.ts
Last active July 9, 2022 15:52
appointment with events
@Injectable()
export class AppointmentService {
constructor(@InjectRepository(Appointment) private readonly repository: Repository<Appointment>,
@Inject(AppointmentPermissionService) private readonly appointmentPermissionService: AppointmentPermissionService,
@Inject(EVENT_PUBLISHER_TOKEN) private readonly eventPublisher: EventPublisher) {}
async create(values: DeepPartial<Appointment>, currentUser: User) {
await this.appointmentPermissionService.verifyCanCreate(currentUser, values)
const appointment = await this.repository.save(values)
await this.eventPublisher.publish(makeAppointmentCreatedEvent(appointment.id))
export const APPOINTMENT_CREATED_TYPE = 'event/appointment/created'
export interface AppointmentCreatedEvent extends Event {
type: typeof APPOINTMENT_CREATED_TYPE
appointmentId: string
}
export function makeAppointmentCreatedEvent(appointmentId: string) {
return {
type: APPOINTMENT_CREATED_TYPE,
appointmentId
@MaxLarue
MaxLarue / event.service.ts
Created July 9, 2022 15:43
event service
export type EventType = string;
export interface Event {
type: EventType;
}
export interface EventPublisher {
publish<T extends Event>(event: Event): Promise<void>
}
@MaxLarue
MaxLarue / appointment.service.ts
Created July 9, 2022 15:06
appointment-rating
@Injectable()
export class AppointmentService {
constructor(@InjectRepository(Appointment) private readonly repository: Repository<Appointment>,
@Inject(QuotationService) private readonly quotationService: QuotationService,
@Inject(WorkOrderService) private readonly workOrderService: WorkOrderService,
@Inject(NotificationService) private readonly notificationService: NotificationService,
@Inject(StatusChangeHistoryService) private readonly statusChangeHistoryService: StatusChangeHistoryService,
@Inject(AppointmentPermissionService) private readonly appointmentPermissionService: AppointmentPermissionService,
@Inject(RatingServiceGateway) private readonly ratingServiceGateway: RatingServiceGateway) {}
@MaxLarue
MaxLarue / appointment.service.ts
Last active July 9, 2022 14:56
appointment-service 2
@Injectable()
export class AppointmentService {
constructor(@InjectRepository(Appointment) private readonly repository: Repository<Appointment>,
@Inject(QuotationService) private readonly quotationService: QuotationService,
@Inject(WorkOrderService) private readonly workOrderService: WorkOrderService,
@Inject(NotificationService) private readonly notificationService: NotificationService,
@Inject(StatusChangeHistoryService) private readonly statusChangeHistoryService: StatusChangeHistoryService,
@Inject(AppointmentPermissionService) private readonly appointmentPermissionService: AppointmentPermissionService) {}
async create(values: DeepPartial<Appointment>, currentUser: User) {
@Injectable()
export class AppointmentService {
constructor(@InjectRepository(Appointment) private readonly repository: Repository<Appointment>,
@Inject(QuotationService) private readonly quotationService: QuotationService,
@Inject(WorkOrderService) private readonly workOrderService: WorkOrderService,
@Inject(EmailService) private readonly emailService: EmailService,
@Inject(StatusChangeHistoryService) private readonly statusChangeHistoryService: StatusChangeHistoryService,
@Inject(AppointmentPermissionService) private readonly appointmentPermissionService: AppointmentPermissionService) {}
async create(values: DeepPartial<Appointment>, currentUser: User) {
@MaxLarue
MaxLarue / matching.py
Created August 6, 2019 20:37
string matching
def histogram_match(reference, input):
reference_histogram = defaultdict(int)
input_histogram = defaultdict(int)
for letter in reference.lower():
reference_histogram[letter] += 1
for letter in input.lower():
input_histogram[letter] += 1