Skip to content

Commit

Permalink
Merge pull request #43 from TickLabVN:feature/update-excute-and-check…
Browse files Browse the repository at this point in the history
…out-api

refactor(printing request): add printing request and file ID in some APIs
  • Loading branch information
quannhg authored Dec 4, 2023
2 parents 1d35c77 + 113200c commit 3b8e035
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 16 deletions.
32 changes: 29 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
FASTIFY_PORT=
FASTIFY_TEST_PORT=

POSTGRES_USER=
POSTGRES_PASSWORD=
POSTGRES_DB=
POSTGRES_DB=ssps_db
POSTGRES_PORT=
POSTGRES_TEST_PORT=s
POSTGRES_URL=

CORS_WHITE_LIST=
CORS_WHITE_LIST=[http://localhost:3000, http://localhost:8080]
COOKIE_SECRET=
JWT_SECRET=
JWT_SECRET=

MINIO_URL=
MINIO_SERVER_ENDPOINT=
MINIO_PORT=9000
MINIO_ACCESS_KEY=
MINIO_SECRET_KEY=
MINIO_BUCKET_NAME=ssps

CHECKOUT_ENVIRONMENT=sandbox
PAYPAL_LIVE_ENDPOINT=https://api-m.paypal.com
PAYPAL_SANDBOX_ENDPOINT=https://api-m.sandbox.paypal.com
PAYPAL_CLIENT_ID=
PAYPAL_CLIENT_SECRET=

OPEN_EXCHANGE_RATES_ENDPOINT=https://open.er-api.com
OPEN_EXCHANGE_RATES_APP_ID=

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URL=
UI_HOME_URL=https://ssps.tickflow.net/home
7 changes: 6 additions & 1 deletion .github/workflows/test-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ jobs:
echo PAYPAL_SANDBOX_ENDPOINT=${{ vars.PAYPAL_SANDBOX_ENDPOINT }} >> .env
echo PAYPAL_CLIENT_ID=${{ secrets.PAYPAL_CLIENT_ID }} >> .env
echo PAYPAL_CLIENT_SECRET=${{ secrets.PAYPAL_CLIENT_SECRET }} >> .env
echo >> .env
echo >> .env
echo OPEN_EXCHANGE_RATES_ENDPOINT=${{ vars.OPEN_EXCHANGE_RATES_ENDPOINT }} >> .env
echo OPEN_EXCHANGE_RATES_APP_ID=${{ secrets.OPEN_EXCHANGE_RATES_APP_ID }} >> .env
echo >> .env
echo GOOGLE_CLIENT_ID=${{ secrets.GOOGLE_CLIENT_ID }} >> .env
echo GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_CLIENT_SECRET }} >> .env
Expand Down
2 changes: 2 additions & 0 deletions src/configs/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export const envs = cleanEnv(process.env, {
PAYPAL_SANDBOX_ENDPOINT: url(),
PAYPAL_CLIENT_ID: str(),
PAYPAL_CLIENT_SECRET: str(),
OPEN_EXCHANGE_RATES_ENDPOINT: url(),
OPEN_EXCHANGE_RATES_APP_ID: str(),
GOOGLE_CLIENT_ID: str({ default: 'anc' }),
GOOGLE_CLIENT_SECRET: str(),
GOOGLE_REDIRECT_URL: url(),
Expand Down
3 changes: 2 additions & 1 deletion src/dtos/out/printing/printFile.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Static, Type } from '@sinclair/typebox';

export const PrintingFileResultDto = Type.Object({
status: Type.String(),
message: Type.String()
message: Type.String(),
printingRequestId: Type.String()
});

export type PrintingFileResultDto = Static<typeof PrintingFileResultDto>;
6 changes: 4 additions & 2 deletions src/dtos/out/printing/printingRequest.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ export const DeleteFilePrintingRequestResultDto = Type.Object({
});

export const ExecutePrintingRequestResultDto = Type.Object({
PrintingStatus: Type.Enum(PRINTING_STATUS)
PrintingStatus: Type.Enum(PRINTING_STATUS),
printingRequestId: Type.String()
});

export const CancelPrintingRequestResultDto = Type.Object({
printingStatus: Type.Enum(PRINTING_STATUS)
printingStatus: Type.Enum(PRINTING_STATUS),
printingRequestId: Type.String()
});

export type GetPrintingRequestResultDto = Static<typeof GetPrintingRequestResultDto>;
Expand Down
6 changes: 4 additions & 2 deletions src/dtos/out/printing/uploadFile.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ export const UploadFileResultDto = Type.Object({
});

export const UploadConfigResultDto = Type.Object({
status: Type.String()
status: Type.String(),
fileId: Type.String()
});

export const FilePrintNumberChangeRequestResultDto = Type.Object({
status: Type.String(),
message: Type.Optional(Type.String())
message: Type.Optional(Type.String()),
fileId: Type.Optional(Type.String())
});

export type UploadFileResultDto = Static<typeof UploadFileResultDto>;
Expand Down
19 changes: 13 additions & 6 deletions src/handlers/printingRequest.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ const uploadConfigToPrintingRequest: Handler<UploadConfigResultDto, { Params: Up
await handleUploadingConfig(fileId, fileConfig);

return res.status(200).send({
status: 'Configuration uploaded successfully'
status: 'Configuration uploaded successfully',
fileId
});
} catch (err) {
res.badRequest(err.message);
Expand Down Expand Up @@ -427,7 +428,9 @@ const executePrintingRequest: Handler<PrintingFileResultDto, { Body: PrintingReq
for (let i = 0; i < file.fileNum; i++) await printFileFromBuffer(nodePrinter, configurationBuffer);
});

return res.status(200).send({ status: 'printing', message: 'The printing request is being executed' });
await prisma.printingRequest.update({ where: { id: printingRequestId }, data: { paid: 'paid' } });

return res.status(200).send({ status: 'printing', message: 'The printing request is being executed', printingRequestId });
} catch (err) {
logger.error(err);
return res.status(500).send({ status: 'fail', message: err.message });
Expand Down Expand Up @@ -529,9 +532,11 @@ const deleteFilePrintingRequest: Handler<DeleteFilePrintingRequestResultDto, { P

const cancelPrintingRequest: Handler<CancelPrintingRequestResultDto, { Params: PrintingRequestInputDto }> = async (req, res) => {
try {
const printingRequestId = req.params.printingRequestId;

const printingRequest = await prisma.printingRequest.findUnique({
where: {
id: req.params.printingRequestId
id: printingRequestId
}
});

Expand All @@ -549,15 +554,16 @@ const cancelPrintingRequest: Handler<CancelPrintingRequestResultDto, { Params: P

await prisma.printingRequest.update({
where: {
id: req.params.printingRequestId
id: printingRequestId
},
data: {
status: PRINTING_STATUS.canceled
}
});

return res.status(200).send({
printingStatus: PRINTING_STATUS.canceled
printingStatus: PRINTING_STATUS.canceled,
printingRequestId
});
} catch (err) {
logger.error(err);
Expand Down Expand Up @@ -638,7 +644,8 @@ const filePrintNumberChangeRequest: Handler<FilePrintNumberChangeRequestResultDt
});

return res.status(200).send({
status: 'success'
status: 'success',
fileId
});
} catch (err) {
logger.error(err);
Expand Down
7 changes: 7 additions & 0 deletions src/services/ExchangeRate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { invoke, openExchangeRatesServer } from './common';

export const currencyExchangeRateService = {
getVietNameseExchangeRate: (apiKey: string) => {
return invoke<Record<string, Record<string, string | number>>>(openExchangeRatesServer.get(`/v6/latest/VND?apikey=${apiKey}`));
}
};
6 changes: 5 additions & 1 deletion src/services/common.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { PAYPAL_ENDPOINT } from '@configs';
import { PAYPAL_ENDPOINT, envs } from '@configs';
import axios, { AxiosError, AxiosResponse } from 'axios';

export const paypalServer = axios.create({
baseURL: PAYPAL_ENDPOINT,
withCredentials: true
});

export const openExchangeRatesServer = axios.create({
baseURL: envs.OPEN_EXCHANGE_RATES_ENDPOINT
});

export async function invoke<R = unknown, D = unknown>(call: Promise<AxiosResponse<R, D>>) {
try {
const response = await call;
Expand Down
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
* @file Automatically generated by barrelsby.
*/

export * from './ExchangeRate';
export * from './common';
export * from './paypal';
17 changes: 17 additions & 0 deletions src/utils/CurrencyConvert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { envs } from '@configs';
import { currencyExchangeRateService } from '@services';

export async function convertVNDtoUSD(amountInVND: number): Promise<number> {
const targetCurrency = 'USD';

try {
const currencyExchangeRates = await currencyExchangeRateService.getVietNameseExchangeRate(envs.OPEN_EXCHANGE_RATES_APP_ID);

const exchangeRate = currencyExchangeRates.rates[targetCurrency];
const amountInUSD = amountInVND * Number(exchangeRate) * 1.0;
return amountInUSD;
} catch (error) {
console.error(`Error: ${error.message}`);
throw new Error('An error occurred while processing the conversion');
}
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @file Automatically generated by barrelsby.
*/

export * from './CurrencyConvert';
export * from './createRoutes';
export * from './editPdf';
export * from './googleOAuth';
Expand Down

0 comments on commit 3b8e035

Please sign in to comment.