50 lines
1.9 KiB
TypeScript
50 lines
1.9 KiB
TypeScript
import { Inject, Injectable } from "@nestjs/common";
|
|
import * as repository from "@apihub24/repository";
|
|
import { IOrganization } from "src/contracts/models/organization";
|
|
|
|
@Injectable()
|
|
export class OrganizationService {
|
|
constructor(
|
|
@Inject("@apihub24/organization_repository")
|
|
private readonly organizationRepository: repository.IRepository<IOrganization>
|
|
) {}
|
|
|
|
/**
|
|
* This asynchronous method retrieves organizations that match a given filter.
|
|
* @param filter A function that takes an `IOrganization` object and returns a boolean. It serves as the condition for selecting organizations.
|
|
* @returns A Promise that resolves to an array of `IOrganization` objects that satisfy the filter condition.
|
|
*/
|
|
async getBy(
|
|
filter: (organization: IOrganization) => boolean
|
|
): Promise<IOrganization[]> {
|
|
return await this.organizationRepository.getBy(filter);
|
|
}
|
|
|
|
/**
|
|
* This asynchronous method saves a single `IOrganization` object.
|
|
* @param organization The `IOrganization` object to be saved.
|
|
* @returns A Promise that resolves to the saved `IOrganization` object.
|
|
* @throws An error if the organization could not be saved.
|
|
*/
|
|
async save(organization: IOrganization): Promise<IOrganization> {
|
|
const savedOrganizations = await this.organizationRepository.save([
|
|
organization,
|
|
]);
|
|
if (!savedOrganizations?.length) {
|
|
throw new Error(`organization (${organization.name}) not saved`);
|
|
}
|
|
return savedOrganizations[0];
|
|
}
|
|
|
|
/**
|
|
* This asynchronous method deletes organizations based on a provided filter.
|
|
* @param filter A function that takes an `IOrganization` object and returns a boolean. It specifies which organizations to delete.
|
|
* @returns A Promise that resolves to `true` if the deletion is successful, and `false` otherwise.
|
|
*/
|
|
async deleteBy(
|
|
filter: (organization: IOrganization) => boolean
|
|
): Promise<boolean> {
|
|
return await this.organizationRepository.deleteBy(filter);
|
|
}
|
|
}
|