aviyelverse / Open-Source-Requests

A curation of paid and unpaid requests for the community to work on.
18 stars 4 forks source link

Implement BatchJobService #45

Open ghost opened 2 years ago

ghost commented 2 years ago

Add a BatchJobService. Should have the following methods:

class BatchJobService extends BaseService {

  static Events = {
    CREATED: "batch.created",
    CANCELED: "batch.canceled",
    COMPLETED: "batch.completed",
  }

  // constructor...

  async create(data: BatchJobCreateProps): Promise<BatchJob> {
    // logic...

    await this.eventBus_
        .withTransaction(manager)
        .emit(BatchJobService.Events.CREATED, {
          id: result.id,
        })

    return result
  }

  async update(batchJobId: string, data: BatchJobUpdateProps): Promise<BatchJob> {
    // logic...
  }

  async cancel(
    batchJobId: string,
    userId: string 
  ): Promise<void {
    // logic...
   await this.eventBus_
        .withTransaction(manager)
        .emit(BatchJobService.Events.CREATED, {
          id: result.id,
        })

    return result
  }

  async listAndCount(
    selector: FilterableBatchJobProps,
    config: FindConfig<BatchJob> = { relations: [], skip: 0, take: 20 }
  ): Promise<[BatchJob[], number]> {
    // see other implementations of listAndCount
  }

  async retrieve(
    batchJobId: string,
    userId: string,
    config: FindConfig<BatchJob>
  ): Promise<BatchJob> {
    // retrieve logic...
  }

  /** 
  * if job is started with dry_run: true, then it's required
  * to complete the job before it's written to DB
  */
  async complete(
    batchJobId: string,
    userId: string,
  ): Promise<BatchJob> {
    // logic...

    await this.eventBus_
        .withTransaction(manager)
        .emit(BatchJobService.Events.CREATED, {
          id: result.id,
        })

    return result
  }
}