ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ… (์Šค์ผ€์ค„ ์ฝ”๋“œ๊ด€๋ จ)

2024. 8. 2. 14:06ใ†TIL

๐Ÿ’กํ”„๋กœ์ ํŠธ ์ง„ํ–‰์ค‘ ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ…

 

* ์ƒํ™ฉ 

- ์Šค์ผ€์ค„ ๊ด€๋ฆฌ ์ฝ”๋“œ ์ž‘์„ฑ์ค‘  substoryhistory ํ…Œ์ด๋ธ”์— ๋‹ด๊ธด nextPayAt ๋‚ ์งœ ํ•˜๋ฃจ ์ „๋‚  ์•Œ๋žŒ์„ ๋ณด๋‚ด์ฃผ๊ธฐ ์œ„ํ•ด 

์•Œ๋žŒ์ผ์„ ์„ค์ •ํ•˜๋˜์ค‘ nextPayAt ๋‚ ์งœ์™€ tomorrow ๋ณ€์ˆ˜๊ฐ€ ์ผ์น˜ํ•  ํ•˜๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋Š” ๋ฐ 

 

1. ๋ฌธ์ œ 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ƒ ๋‚ด์ผ์ด ๊ฒฐ์ œ์ผ์ธ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๋ฐ ' ๋‚ด์ผ์ธ ๊ฒฐ์ œ์ด๋ ฅ '์ด ๋นˆ ๋ฐฐ์—ด๋กœ ๋‚˜์˜ค๋Š” ์ƒํ™ฉ ํ™•์ธ.

const tomorrow = new Date();
    tomorrow.setDate(tomorrow.getDate() + 1);
    // nextPayAt: ๊ฒฐ์ œ๋‚ ์งœ
    const subscriptionHistories =
      await this.subscriptionHistoriesRepository.find({
        where: {
          nextPayAt: tomorrow,
        },
        relations: [
          'userSubscription',
          'userSubscription.user',
          'userSubscription.platform',
        ],
      });
    console.log('๋‚ด์ผ์ธ ๊ฒฐ์ œ์ด๋ ฅ', subscriptionHistories);

 

2. ํ•ด๊ฒฐ๋ฐฉ์•ˆ 

- ๊ด€๋ จ ๋‚ด์šฉ์„ ๊ฒ€์ƒ‰ํ•ด๋ณธ ๊ฒฐ๊ณผ new Date()๋กœ ๋‚ ์งœ๋ฅผ ์ง€์ •ํ•ด์ค„ ๊ฒฝ์šฐ ํ˜„์žฌ์‹œ๊ฐ„์ด ํ•จ๊ป˜ ์ €์žฅ๋˜์–ด ex) 2024-08-02 12:50:00 
    ๋‹ค์Œ๊ฒฐ์ œ์ผ์ธ 2024-08-02 00:00:00 ๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์€ ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

-  setHours(0, 0, 0, 0); ์„ ์‚ฌ์šฉํ•˜์—ฌ tomorrow์‹œ๊ฐ„์„ 00:00:00 ๋กœ ์„ค์ •ํ•˜์—ฌ ๋‚ ์งœ๋งŒ ๋น„๊ตํ•˜์—ฌ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ์ˆ˜์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

- ์ฝ”๋“œ ์‹คํ–‰๊ฒฐ๊ณผ ๋‚ด์ผ์ธ ๊ฒฐ์ œ์ด๋ ฅ์„ ์ž˜ ๋ถˆ๋Ÿฌ ์™€์ง€๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.


  const tomorrow = new Date();
    tomorrow.setHours(0, 0, 0, 0); //์‹œ๊ฐ„์„ค์ • ์ถ”๊ฐ€
    tomorrow.setDate(tomorrow.getDate() + 1);

    const subscriptionHistories =
      await this.subscriptionHistoriesRepository.find({
        where: {
          nextPayAt: tomorrow,
        },
        relations: [
          'userSubscription',
          'userSubscription.user',
          'userSubscription.platform',
        ],
      });
    console.log('๋‚ด์ผ์ธ ๊ฒฐ์ œ์ด๋ ฅ', subscriptionHistories);