|
| 1 | +'use server'; |
| 2 | + |
| 3 | +import { Item } from '@/app/api/coupang/route'; |
| 4 | +import SessionBrowserManager from '@/hooks/sessionBrowserManager'; |
| 5 | + |
| 6 | +const BASE_URL = 'https://www.coupang.com/'; |
| 7 | +const DETAIL_BASE_URL = 'https://www.coupang.com/vp/products/'; |
| 8 | + |
| 9 | +const DELAY = 2000; |
| 10 | +const RANGE = 2000; |
| 11 | + |
| 12 | +function delay(ms: number): Promise<void> { |
| 13 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 14 | +} |
| 15 | + |
| 16 | +export const coupangSignIn = async () => { |
| 17 | + const { page, status } = await SessionBrowserManager.getInstance(); |
| 18 | + |
| 19 | + if (status !== 'NOT_SIGNIN') { |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + await page.goto(BASE_URL); |
| 24 | + console.log('Go to Main'); |
| 25 | + await delay(Math.random() * DELAY + RANGE); |
| 26 | + |
| 27 | + await page.locator('.login').click(); |
| 28 | + console.log('Login Button Clicked!'); |
| 29 | + await delay(Math.random() * DELAY + RANGE); |
| 30 | + |
| 31 | + const coupangId = process.env.COUPANG_ID; |
| 32 | + const coupangPw = process.env.COUPANG_PW; |
| 33 | + await page.locator('._loginIdInput').fill(coupangId ?? ''); |
| 34 | + console.log('Login ID Filled!'); |
| 35 | + await delay(Math.random() * DELAY + RANGE); |
| 36 | + |
| 37 | + await page.locator('._loginPasswordInput').fill(coupangPw ?? ''); |
| 38 | + console.log('Login PWD Filled!'); |
| 39 | + await delay(Math.random() * DELAY + RANGE); |
| 40 | + |
| 41 | + await page.locator('.login__button--submit').click(); |
| 42 | + console.log('Login Button Clicked!'); |
| 43 | + await delay(Math.random() * DELAY + RANGE); |
| 44 | +}; |
| 45 | + |
| 46 | +export const coupangGetPincode = async () => { |
| 47 | + const sessionBrowserManager = await SessionBrowserManager.getInstance(); |
| 48 | + const { page, status } = sessionBrowserManager; |
| 49 | + |
| 50 | + if (status !== 'NOT_SIGNIN') { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + const isPresent = |
| 55 | + (await page.locator('.pincode-content__button').count()) > 0; |
| 56 | + |
| 57 | + if (!isPresent) { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + await page.locator('.pincode-content__button').click(); |
| 62 | + await delay(Math.random() * DELAY + RANGE); |
| 63 | + |
| 64 | + if (isPresent) { |
| 65 | + sessionBrowserManager.status = 'PINCODE'; |
| 66 | + return true; |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +export const coupangSetPincode = async (pincode: string) => { |
| 71 | + console.log('🚀 ~ coupangSetPincode ~ pincode:', pincode); |
| 72 | + const sessionBrowserManager = await SessionBrowserManager.getInstance(); |
| 73 | + const { page, status } = sessionBrowserManager; |
| 74 | + |
| 75 | + if (status !== 'PINCODE') { |
| 76 | + if (status === 'NOT_SIGNIN') { |
| 77 | + sessionBrowserManager.status = 'SIGNIN'; |
| 78 | + } |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + await page |
| 83 | + .locator('.pincode-input__pincode-input-box__pincode') |
| 84 | + .fill(pincode); |
| 85 | + await delay(Math.random() * DELAY + RANGE); |
| 86 | + |
| 87 | + await page.locator('.pincode-input__button').click(); |
| 88 | + await delay(Math.random() * DELAY + RANGE); |
| 89 | + sessionBrowserManager.status = 'SIGNIN'; |
| 90 | +}; |
| 91 | + |
| 92 | +export const coupangAddCart = async ({ productId, itemId, quantity }: Item) => { |
| 93 | + const { page, status } = await SessionBrowserManager.getInstance(); |
| 94 | + |
| 95 | + if (status !== 'SIGNIN') { |
| 96 | + return; |
| 97 | + } |
| 98 | + console.log('Add Cart Function!! ~ ', status); |
| 99 | + const url = itemId |
| 100 | + ? DETAIL_BASE_URL + productId + '?vendorItemId=' + itemId |
| 101 | + : DETAIL_BASE_URL + productId; |
| 102 | + await page.goto(url); |
| 103 | + await delay(Math.random() * DELAY + RANGE); |
| 104 | + |
| 105 | + await page.locator('.prod-quantity__input').fill(quantity.toString()); |
| 106 | + console.log('Product Quantity Set'); |
| 107 | + await delay(Math.random() * DELAY + RANGE); |
| 108 | + |
| 109 | + await page.locator('.prod-cart-btn').click(); |
| 110 | + console.log('Product Cart Button Clicked!'); |
| 111 | + await delay(Math.random() * DELAY + RANGE); |
| 112 | +}; |
| 113 | + |
| 114 | +export const coupangPayAll = async () => { |
| 115 | + const sessionBrowserManager = await SessionBrowserManager.getInstance(); |
| 116 | + const { page, status } = sessionBrowserManager; |
| 117 | + |
| 118 | + if (status !== 'SIGNIN') { |
| 119 | + return; |
| 120 | + } |
| 121 | + console.log('Pay All Function!!'); |
| 122 | + |
| 123 | + await page.goto(BASE_URL); |
| 124 | + await delay(Math.random() * DELAY + RANGE); |
| 125 | + |
| 126 | + await page.locator('.mycart-preview-module').click(); |
| 127 | + console.log('MyCart Button Clicked!'); |
| 128 | + await delay(Math.random() * DELAY + RANGE); |
| 129 | + |
| 130 | + await page.locator('.order-buttons').click(); |
| 131 | + console.log('Order Button Clicked!'); |
| 132 | + await delay(Math.random() * DELAY + RANGE); |
| 133 | + |
| 134 | + await page.locator('.paymentBtn-v2-style').click(); |
| 135 | + console.log('Payment Button Clicked!'); |
| 136 | + await delay(Math.random() * DELAY + RANGE); |
| 137 | + |
| 138 | + await delay(Math.random() * (DELAY + 2000) + RANGE); |
| 139 | + const screenshotBuffer = await page |
| 140 | + .locator('#modal-callLGPayment') |
| 141 | + .screenshot(); |
| 142 | + |
| 143 | + sessionBrowserManager.status = 'PAYMENT'; |
| 144 | + return screenshotBuffer; |
| 145 | +}; |
| 146 | + |
| 147 | +export const coupangInsertPassword = async (password: string) => { |
| 148 | + const sessionBrowserManager = await SessionBrowserManager.getInstance(); |
| 149 | + const { page, status } = sessionBrowserManager; |
| 150 | + |
| 151 | + console.log('🚀 ~ coupangPayment ~ ', status); |
| 152 | + |
| 153 | + if (status !== 'PAYMENT') { |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + const iframe = page.frameLocator('#callLGPayment'); |
| 158 | + for (const numpad of password) { |
| 159 | + await iframe.locator(`[data-key="${numpad}"]`).click(); |
| 160 | + console.log('numpad ', numpad, 'clicked!'); |
| 161 | + await delay(Math.random() * 1000 + RANGE); |
| 162 | + } |
| 163 | + return; |
| 164 | +}; |
| 165 | + |
| 166 | +export const coupangClose = async () => { |
| 167 | + await SessionBrowserManager.close(); |
| 168 | +}; |
0 commit comments