Skip to content

Commit

Permalink
2.5.7 admin api for updating banner
Browse files Browse the repository at this point in the history
  • Loading branch information
pauljonescodes committed Oct 27, 2023
1 parent 64ecc17 commit ca319a6
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
95 changes: 95 additions & 0 deletions src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
import {
BadRequestException,
Body,
Controller,
HttpCode,
HttpStatus,
Inject,
InternalServerErrorException,
Logger,
Post,
Query,
UploadedFile,
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { type ConfigType } from '@nestjs/config';
import { AuthGuard } from '@nestjs/passport';
import { FileInterceptor } from '@nestjs/platform-express';
import {
ApiBearerAuth,
ApiBody,
ApiConsumes,
ApiOkResponse,
ApiOperation,
ApiQuery,
ApiSecurity,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { InjectS3, type S3 } from 'nestjs-s3';
import path from 'path';
import { ApiKeyAuthGuard } from '../authentication/apikey-auth.guard.js';
import { AuthenticationService } from '../authentication/authentication.service.js';
import { AuthenticationEmailLoginRequestBody } from '../authentication/dto/authentication-email-login.dto.js';
import { AuthenticationResponse } from '../authentication/types/authentication-response.type.js';
import { AwsS3Config } from '../configs/aws-s3.config.js';
import { AppConfigService } from '../moa-square/services/app-config.service.js';
import { MerchantsSquareService } from '../moa-square/services/merchants.square.service.js';
import { ErrorResponse } from '../utils/error-response.js';
import { AdministratorsGuard } from './administrators.guard.js';
Expand All @@ -38,6 +51,11 @@ export class AdminController {
constructor(
private readonly authenticationService: AuthenticationService,
private readonly merchantsSquareService: MerchantsSquareService,
private readonly appConfigService: AppConfigService,
@InjectS3()
private readonly s3: S3,
@Inject(AwsS3Config.KEY)
protected awsS3Config: ConfigType<typeof AwsS3Config>,
) {
this.logger.verbose(this.constructor.name);
}
Expand Down Expand Up @@ -78,4 +96,81 @@ export class AdminController {
merchantId,
});
}

@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'), AdministratorsGuard)
@ApiUnauthorizedResponse({
description: 'You need to be authenticated to access this endpoint.',
type: ErrorResponse,
})
@Post('banner/upload')
@ApiConsumes('multipart/form-data')
@ApiBody({
schema: {
type: 'object',
properties: {
file: {
type: 'string',
format: 'binary',
},
},
},
})
@UseInterceptors(FileInterceptor('file'))
@ApiOperation({ summary: 'Upload banner', operationId: 'postBannerUpload' })
@ApiQuery({ name: 'merchantId', required: true, type: String })
async postBannerUpload(
@Query('merchantId') merchantId: string,
@UploadedFile() file: Express.Multer.File,
) {
this.logger.verbose(this.postBannerUpload.name);

const fileExtension = path.extname(file.originalname).toLowerCase();
let contentType: string;

if (fileExtension === '.jpg' || fileExtension === '.jpeg') {
contentType = 'image/jpeg';
} else if (fileExtension === '.png') {
contentType = 'image/png';
} else {
throw new BadRequestException('Invalid file type'); // You can customize this message
}

let appConfig = await this.appConfigService.findOne({
where: { merchantId },
});

if (!appConfig) {
appConfig = this.appConfigService.create({
merchantId,
});
}

const sanitizedFilename = file.originalname.replace(/[^\w\s.-]/g, '_');
const key = `${encodeURIComponent(Date.now())}-${encodeURIComponent(
sanitizedFilename,
)}`;

const { defaultBucket, region } = this.awsS3Config;

try {
await this.s3.putObject({
Bucket: defaultBucket,
Key: key,
Body: file.buffer,
ContentType: contentType,
ContentDisposition: 'inline',
});
} catch (error) {
this.logger.error(error);
throw new InternalServerErrorException(error);
}

appConfig.bannerFileKey = key;
appConfig.bannerFileFullUrl = `https://${defaultBucket}.s3.${region}.amazonaws.com/${key}`;
appConfig.bannerFileDisplayName = file.originalname;
appConfig.bannerFileContentType = contentType;

return this.appConfigService.save(appConfig);
}
}
2 changes: 1 addition & 1 deletion src/moa-square/moa-square.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ import { VariationsService } from './services/variations.service.js';
LineItemModifierEntity,
]),
],
exports: [MerchantsSquareService],
exports: [MerchantsSquareService, AppConfigService],
providers: [
CatalogsService,
ItemsService,
Expand Down

0 comments on commit ca319a6

Please sign in to comment.