import { injectable } from "inversify"; import BaseService from "../../service"; import { IService, IResourceMapper, IFindParams } from "../../interfaces"; import AssociationEntry, { IAssociationEntry, IAssociationEntryResource, IAssociationEntryResponse } from "./"; import Mapper from "./mapper"; export interface IAssociationEntryService extends IService { bulkInsert(associationId: string, data: IAssociationEntry[]): Promise findByEntryId(associationId: string, entryId: string, options: IFindParams): Promise createAssociationEntry(associationId: string, entry: IAssociationEntry): Promise updateAssociationEntry(associationId: string, entry: IAssociationEntry): Promise deleteAssociationEntry(associationId: string, entryId: string): Promise } @injectable() export default class AssociationEntryService extends BaseService implements IAssociationEntryService { resource: string = "association-entry"; mapper: IResourceMapper = new Mapper(); public url(associationId) { return `${this.host}/associations/${associationId}/entries`; } public async findByEntryId(associationId: string, entryId: string, options: IFindParams = {}) { const resources = await this.http.fetch( `${this.url(associationId)}/${entryId}${this.qs(options)}`, { headers: this.headers(), } ); return this.mapper.map(resources); } public async createAssociationEntry(associationId: string, entry: IAssociationEntry) { const response = await this.http.fetch(`${this.url(associationId)}`, { method: "POST", body: { data: this.mapper.toResource(entry), }, headers: this.headers(), }); return this.mapper.map(response); } public async updateAssociationEntry(associationId: string, entry: IAssociationEntry) { await this.http.fetch( `${this.url(associationId)}/${entry.id}`, { method: "PATCH", body: { data: this.mapper.toResource(entry), }, headers: this.headers(), } ); return; } public async bulkInsert(associationId: string, data: IAssociationEntry[]) { const response = await this.http.fetch(`${this.url(associationId)}`, { method: "PUT", body: { data: data.map(this.mapper.toResource), }, headers: this.headers(), }); return this.mapper.map(response); } public async deleteAssociationEntry(associationId: string, entryId: string) { await this.http.fetch(`${this.url(associationId)}/${entryId}`, { method: "DELETE", body: { data: {}, }, headers: this.headers(), }); return; } }