chore: first commit
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { EntityManager } from "@mikro-orm/postgresql";
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
|
||||
import { BusinessMessage } from "../../../common/enums/message.enum";
|
||||
import { UpdateBusinessSettingsDto } from "../DTO/update-business-settings.dto";
|
||||
import { BusinessStaff } from "../entities/business-staff.entity";
|
||||
import { BusinessRepository } from "../repositories/business.repository";
|
||||
|
||||
@Injectable()
|
||||
export class BusinessesService {
|
||||
constructor(
|
||||
private readonly businessRepository: BusinessRepository,
|
||||
private readonly em: EntityManager,
|
||||
) {}
|
||||
|
||||
async getBusinessByDanakSubscriptionId(danakSubscriptionId: string) {
|
||||
const business = await this.businessRepository.findOne({ danakSubscriptionId, deletedAt: null });
|
||||
if (!business) throw new BadRequestException(BusinessMessage.NOT_FOUND);
|
||||
|
||||
return business;
|
||||
}
|
||||
//************************************************** */
|
||||
|
||||
async getBusinessBySlug(slug: string) {
|
||||
const business = await this.businessRepository.findOne({ slug, deletedAt: null });
|
||||
if (!business) throw new BadRequestException(BusinessMessage.NOT_FOUND);
|
||||
|
||||
return { business };
|
||||
}
|
||||
//************************************************** */
|
||||
async updateBusinessSettings(businessId: string, settingsDto: UpdateBusinessSettingsDto) {
|
||||
const business = await this.businessRepository.findOne({ id: businessId, deletedAt: null });
|
||||
if (!business) throw new BadRequestException(BusinessMessage.NOT_FOUND);
|
||||
|
||||
this.businessRepository.assign(business, settingsDto);
|
||||
await this.em.flush();
|
||||
|
||||
return {
|
||||
message: BusinessMessage.BUSINESS_SETTINGS_UPDATED,
|
||||
business,
|
||||
};
|
||||
}
|
||||
//************************************************** */
|
||||
|
||||
async getBusinessSettings(businessId: string) {
|
||||
const business = await this.businessRepository.findOne({ id: businessId, deletedAt: null });
|
||||
console.log("business", business);
|
||||
if (!business) throw new BadRequestException(BusinessMessage.NOT_FOUND);
|
||||
|
||||
return { business };
|
||||
}
|
||||
//************************************************** */
|
||||
|
||||
async getBusinessById(id: string) {
|
||||
const business = await this.businessRepository.findOne({ id, deletedAt: null });
|
||||
if (!business) throw new BadRequestException(BusinessMessage.NOT_FOUND);
|
||||
|
||||
return business;
|
||||
}
|
||||
|
||||
//************************************************** */
|
||||
|
||||
async findAdminWithLeastTickets() {
|
||||
// First query to get the ID of the business staff with the least tickets
|
||||
const result = await this.em.getConnection().execute(
|
||||
`
|
||||
SELECT bs.id
|
||||
FROM business_staff bs
|
||||
WHERE bs.deleted_at IS NULL
|
||||
ORDER BY (
|
||||
SELECT COUNT(*) FROM ticket t WHERE t.assigned_to_id = bs.id
|
||||
) ASC
|
||||
LIMIT 1
|
||||
`,
|
||||
);
|
||||
|
||||
if (result.length === 0) return null;
|
||||
|
||||
return this.em.findOne(BusinessStaff, { id: result[0].id });
|
||||
}
|
||||
|
||||
/*******************************/
|
||||
async findOneByIdWithEntityManager(staffId: string, em: EntityManager) {
|
||||
const staff = await em.findOne(BusinessStaff, { id: staffId });
|
||||
if (!staff) throw new BadRequestException(BusinessMessage.STAFF_NOT_FOUND);
|
||||
return staff;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user