Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@nestjs/mongoose": "^10.0.2",
"@nestjs/passport": "^10.0.2",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/schedule": "^4.0.0",
"@nestjs/swagger": "^7.1.16",
"@nestjs/typeorm": "^10.0.1",
"@types/passport-jwt": "^3.0.13",
Expand Down
2 changes: 2 additions & 0 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AppService } from './app.service';
import { LoggerMiddleware } from './middlewares/logger.middleware';
import { MongooseModule } from '@nestjs/mongoose';
import { MONGODB_URL } from './constants';
import { ScheduleModule } from '@nestjs/schedule';

@Module({
imports: [
Expand All @@ -20,6 +21,7 @@ import { MONGODB_URL } from './constants';
AuthModule,
ProductModule,
MongooseModule.forRoot(MONGODB_URL),
ScheduleModule.forRoot(),
],
controllers: [AppController],
providers: [AppService],
Expand Down
23 changes: 23 additions & 0 deletions backend/src/product/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Model } from 'mongoose';
import { ProductPriceDto } from 'src/dto/product.price.dto';
import { PriceDataDto } from 'src/dto/price.data.dto';
import { KR_OFFSET, NINETY_DAYS } from 'src/constants';
import { Cron } from '@nestjs/schedule';

const REGEXP_11ST =
/http[s]?:\/\/(?:www\.|m\.)?11st\.co\.kr\/products\/(?:ma\/|m\/|pa\/)?([1-9]\d*)(?:\?.*)?(?:\/share)?/;
Expand Down Expand Up @@ -198,4 +199,26 @@ export class ProductService {
return { time: new Date(time).getTime(), price, isSoldOut };
});
}
@Cron('* */10 * * * *')
async cyclicPriceChecker() {
const productList = await this.productRepository.find({ select: { id: true, productCode: true } });
const productCodeList = productList.map(({ productCode, id }) => getProductInfo11st(productCode, id));
const results = (await Promise.all(productCodeList)).map(({ productId, productPrice, isSoldOut }) => {
return { productId, price: productPrice, isSoldOut };
});
const updatedDataInfo = results.filter(({ productId, price, isSoldOut }) => {
const cache = this.productDataCache.get(productId);
if (!cache || cache.isSoldOut !== isSoldOut || cache.price !== price) {
const lowestPrice = cache ? Math.min(cache.lowestPrice, price) : price;
this.productDataCache.set(productId, {
isSoldOut,
price,
lowestPrice,
});
return true;
}
return false;
});
await this.productPriceModel.insertMany(updatedDataInfo);
}
}
8 changes: 6 additions & 2 deletions backend/src/utils/openapi.11st.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ import { HttpException, HttpStatus } from '@nestjs/common';
function xmlConvert11st(xml: Buffer) {
const xmlUtf8 = iconv.decode(xml, 'EUC-KR').toString();
const {
ProductInfoResponse: { Product },
ProductInfoResponse: { Product, ProductOption },
}: convert.ElementCompact = convert.xml2js(xmlUtf8, {
compact: true,
cdataKey: 'text',
textKey: 'text',
});
Product['isSoldOut'] = ProductOption ? ProductOption['Status'] === 'N' : false;
return Product;
}

function productInfoUrl11st(productCode: string) {
const shopUrl = new URL(BASE_URL_11ST);
shopUrl.searchParams.append('key', OPEN_API_KEY_11ST);
shopUrl.searchParams.append('productCode', productCode);
shopUrl.searchParams.append('options', 'PdOption');
return shopUrl.toString();
}

export async function getProductInfo11st(productCode: string) {
export async function getProductInfo11st(productCode: string, productId?: string) {
const openApiUrl = productInfoUrl11st(productCode);
try {
const xml = await axios.get(openApiUrl, { responseType: 'arraybuffer' });
Expand All @@ -35,6 +37,8 @@ export async function getProductInfo11st(productCode: string) {
productPrice: parseInt(price),
shop: '11번가',
imageUrl: productDetails['BasicImage']['text'],
isSoldOut: productDetails['isSoldOut'],
productId,
};
} catch (e) {
throw new HttpException('존재하지 않는 상품 코드입니다.', HttpStatus.BAD_REQUEST);
Expand Down