edit image business
This commit is contained in:
@@ -70,10 +70,12 @@ export class BusinessesService {
|
||||
async findMine(ownerId: string): Promise<BusinessEntity[]> {
|
||||
const businesses = await this.repo.find({
|
||||
where: { ownerId },
|
||||
relations: { category: true },
|
||||
relations: { category: true, images: true },
|
||||
order: { createdAt: 'DESC' },
|
||||
});
|
||||
await this.attachCoverImages(businesses);
|
||||
for (const business of businesses) {
|
||||
business.images = this.sortImages(business.images ?? []);
|
||||
}
|
||||
return businesses;
|
||||
}
|
||||
|
||||
@@ -85,6 +87,7 @@ export class BusinessesService {
|
||||
if (!business) {
|
||||
throw new NotFoundException('Business not found');
|
||||
}
|
||||
business.images = this.sortImages(business.images ?? []);
|
||||
return business;
|
||||
}
|
||||
|
||||
@@ -282,6 +285,89 @@ export class BusinessesService {
|
||||
return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
async uploadImages(
|
||||
id: string,
|
||||
files: Express.Multer.File[],
|
||||
actor: AuthUser,
|
||||
): Promise<BusinessImageEntity[]> {
|
||||
const business = await this.findOne(id);
|
||||
this.assertCanManageBusiness(business, actor);
|
||||
return this.images.upload(id, files);
|
||||
}
|
||||
|
||||
async uploadMyImages(
|
||||
files: Express.Multer.File[],
|
||||
actor: AuthUser,
|
||||
): Promise<BusinessImageEntity[]> {
|
||||
const business = await this.requireOwnedBusiness(actor);
|
||||
return this.images.upload(business.id, files);
|
||||
}
|
||||
|
||||
async removeImage(
|
||||
id: string,
|
||||
imageId: string,
|
||||
actor: AuthUser,
|
||||
): Promise<void> {
|
||||
const business = await this.findOne(id);
|
||||
this.assertCanManageBusiness(business, actor);
|
||||
await this.images.remove(id, imageId);
|
||||
}
|
||||
|
||||
async removeMyImage(imageId: string, actor: AuthUser): Promise<void> {
|
||||
const business = await this.requireOwnedBusiness(actor);
|
||||
await this.images.remove(business.id, imageId);
|
||||
}
|
||||
|
||||
async setMyCoverImage(
|
||||
imageId: string,
|
||||
actor: AuthUser,
|
||||
): Promise<BusinessImageEntity> {
|
||||
const business = await this.requireOwnedBusiness(actor);
|
||||
return this.images.setCover(business.id, imageId);
|
||||
}
|
||||
|
||||
private async requireOwnedBusiness(actor: AuthUser): Promise<BusinessEntity> {
|
||||
if (actor.role === UserRole.ADMIN) {
|
||||
throw new ForbiddenException('Use admin image endpoints for this account');
|
||||
}
|
||||
if (actor.role !== UserRole.BUSINESS_OWNER) {
|
||||
throw new ForbiddenException('Insufficient role');
|
||||
}
|
||||
|
||||
const business = await this.repo.findOne({
|
||||
where: { ownerId: actor.id },
|
||||
order: { createdAt: 'DESC' },
|
||||
});
|
||||
if (!business) {
|
||||
throw new NotFoundException('No business linked to your account');
|
||||
}
|
||||
return business;
|
||||
}
|
||||
|
||||
private sortImages(images: BusinessImageEntity[]): BusinessImageEntity[] {
|
||||
return [...images].sort((a, b) => {
|
||||
if (a.isCover !== b.isCover) {
|
||||
return a.isCover ? -1 : 1;
|
||||
}
|
||||
return a.sortOrder - b.sortOrder;
|
||||
});
|
||||
}
|
||||
|
||||
private assertCanManageBusiness(
|
||||
business: BusinessEntity,
|
||||
actor?: AuthUser,
|
||||
): void {
|
||||
if (!actor || actor.role === UserRole.ADMIN) {
|
||||
return;
|
||||
}
|
||||
if (
|
||||
actor.role !== UserRole.BUSINESS_OWNER ||
|
||||
business.ownerId !== actor.id
|
||||
) {
|
||||
throw new ForbiddenException('You can only manage your own business');
|
||||
}
|
||||
}
|
||||
|
||||
private assertCanManageAvailability(
|
||||
business: BusinessEntity,
|
||||
actor?: AuthUser,
|
||||
|
||||
Reference in New Issue
Block a user