Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unactivated check, etc #262

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions .github/workflows/after-purchase.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: After Purchase CI

on:
# チケット購入が始まって購入情報と照合可能になった際に開放
# 2 時間おきに実行
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✍️

紐付けは 2 時間おきを想定

# schedule:
# - cron: 0 * * * * # https://crontab.guru/every-hour
# - cron: 0 1/2 * * * # https://crontab.guru/#0_1/2_*_*_*
# push:
# branches:
# - main
Expand Down
36 changes: 36 additions & 0 deletions .github/workflows/sales-daily.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Sales Daily CI

on:
# 毎週金曜の 17:00 に実行
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✍️

アーカイヴは毎週金曜の 17:00 を想定

# schedule:
# - cron: 0 17 * * 5 # https://crontab.guru/#0_17_*_*_5
# push:
# branches:
# - main
pull_request:
branches:
- main

jobs:
sales-daily:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.19.0]

steps:
- uses: actions/checkout@v4

- name: Install packages
uses: ./.github/actions/install-packages

- name: Build sales daily
run: bun build-sales-daily
working-directory: apps/peatix-adapter
env:
PEATIX_EVENT_ID: ${{ secrets.PEATIX_EVENT_ID }}
PEATIX_BASIC_EMAIL: ${{ secrets.PEATIX_BASIC_EMAIL }}
PEATIX_BASIC_PASSWORD: ${{ secrets.PEATIX_BASIC_PASSWORD }}
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
36 changes: 36 additions & 0 deletions .github/workflows/unactivated-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Unactivated Check CI

on:
# 毎日 23:00 に実行
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✍️

紐付けされていないかチェックするのは毎日の 23:00 を想定

# schedule:
# - cron: 0 23 * * * # https://crontab.guru/#0_23_*_*_*
# push:
# branches:
# - main
pull_request:
branches:
- main

jobs:
unactivated-check:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.19.0]

steps:
- uses: actions/checkout@v4

- name: Install packages
uses: ./.github/actions/install-packages

- name: Build unactivated check
run: bun build-unactivated-check
working-directory: apps/peatix-adapter
env:
PEATIX_EVENT_ID: ${{ secrets.PEATIX_EVENT_ID }}
PEATIX_BASIC_EMAIL: ${{ secrets.PEATIX_BASIC_EMAIL }}
PEATIX_BASIC_PASSWORD: ${{ secrets.PEATIX_BASIC_PASSWORD }}
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
SUPABASE_KEY: ${{ secrets.SUPABASE_KEY }}
2 changes: 2 additions & 0 deletions apps/peatix-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"scripts": {
"build-api": "nest build",
"build-after-purchase": "nest build && node dist/src/cmd after-purchase",
"build-unactivated-check": "nest build && node dist/src/cmd unactivated-check",
"build-sales-daily": "nest build && node dist/src/cmd sales-daily",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type CommandOptions = {
id?: number;
};

@Command({ name: 'after-purchase', description: 'A after purchase command.' })
@Command({ name: 'after-purchase', description: 'An after purchase command.' })
export class AfterPurchaseCommand extends CommandRunner {
private readonly logger = new Logger(AfterPurchaseCommand.name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { SupabaseModule } from 'src/supabase/supabase.module'
import { SupabaseService } from 'src/supabase/supabase.service'
import { DiscordModule } from 'src/discord/discord.module'
import { DiscordService } from 'src/discord/discord.service'
import { UnactivatedCheckCommand } from 'src/unactivated-check/unactivated-check.command'
import { SalesDailyCommand } from 'src/sales-daily/sales-daily.command'
import { UnactivatedCheckService } from 'src/unactivated-check/unactivated-check.service'
import { SalesDailyService } from 'src/sales-daily/sales-daily.service'

@Module({
imports: [
Expand All @@ -38,7 +42,11 @@ import { DiscordService } from 'src/discord/discord.service'
useClass: PuppeteerService,
},
AfterPurchaseCommand,
UnactivatedCheckCommand,
SalesDailyCommand,
AfterPurchaseService,
UnactivatedCheckService,
SalesDailyService,
SupabaseService,
PeatixOrderService,
DiscordService,
Expand Down
12 changes: 0 additions & 12 deletions apps/peatix-adapter/src/after-purchase/after-purchase.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,6 @@ export class AfterPurchaseService {

this.discordService.send('After Purchase Bot', `${receipts.length}件 購入済`)

// 各日毎の購入数を集計する
// const startedDate = new Date(2024, 7 - 1, 29)
// let archives = [[], [], []]
// for (let date = startedDate; date <= new Date(); date.setDate(date.getDate() + 1)) {
// archives[0].push(`${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`)
// archives[1].push(`${receipts.filter(v => v.role === Constants.PEATIX_GENERAL_ROLE).filter(v => v.date.includes(`${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`)).length}`)
// archives[2].push(`${receipts.filter(v => v.role === Constants.PEATIX_WITH_PARTY_ROLE).filter(v => v.date.includes(`${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`)).length}`)
// }
// for (const archive of archives) {
// this.logger.log(`${archive.join(', ')} `)
// }

let i = 0

// Peatix 購入情報を Supabase へ反映する
Expand Down
21 changes: 21 additions & 0 deletions apps/peatix-adapter/src/sales-daily/sales-daily.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Logger } from '@nestjs/common'
import { CommandRunner, Command } from 'nest-commander'
import { SalesDailyService } from './sales-daily.service'

type CommandOptions = {
id?: number;
};

@Command({ name: 'sales-daily', description: 'A sales daily command.' })
export class SalesDailyCommand extends CommandRunner {
private readonly logger = new Logger(SalesDailyCommand.name)

constructor(readonly salesDailyService: SalesDailyService) {
super()
}

async run(params: string[], options?: CommandOptions): Promise<void> {
this.salesDailyService.apply()
return Promise.resolve()
}
}
47 changes: 47 additions & 0 deletions apps/peatix-adapter/src/sales-daily/sales-daily.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { SalesDailyService } from './sales-daily.service'
import { EnvService } from '../env/env.service'
import {
symbol as IPuppeteerService,
PuppeteerService,
} from '../puppeteer/puppeteer.service'
import { SalesDailyCommand } from './sales-daily.command'
import { configuration } from '../env/utils'
import { EnvModule } from '../env/env.module'
import { HttpModule } from '@nestjs/axios'
import { SupabaseModule } from 'src/supabase/supabase.module'
import { SupabaseService } from 'src/supabase/supabase.service'
import { DiscordModule } from 'src/discord/discord.module'
import { DiscordService } from 'src/discord/discord.service'
import { PeatixOrderService } from 'src/peatix-order/peatix-order.service'
import { PeatixOrderModule } from 'src/peatix-order/peatix-order.module'

@Module({
imports: [
ConfigModule.forRoot({
envFilePath: '.env',
expandVariables: true,
load: [configuration],
}),
EnvModule,
HttpModule,
SupabaseModule,
PeatixOrderModule,
DiscordModule,
],
providers: [
ConfigService,
EnvService,
{
provide: IPuppeteerService,
useClass: PuppeteerService,
},
SalesDailyCommand,
SalesDailyService,
SupabaseService,
PeatixOrderService,
DiscordService,
],
})
export class SalesDailyModule {}
56 changes: 56 additions & 0 deletions apps/peatix-adapter/src/sales-daily/sales-daily.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Injectable, Logger } from '@nestjs/common'
import { SupabaseService } from 'src/supabase/supabase.service'
import { DiscordService } from 'src/discord/discord.service'
import { PeatixOrderService } from 'src/peatix-order/peatix-order.service'
import { AttendeeReceipt } from 'src/types/supabase'
import { Constants } from 'src/constnats'

@Injectable()
export class SalesDailyService {
private readonly logger = new Logger(SalesDailyService.name)

constructor(
private readonly peatixOrderService: PeatixOrderService,
private readonly supabaseService: SupabaseService,
private readonly discordService: DiscordService,
) {
//
}

async apply() {
const orders = await this.peatixOrderService.getOrders()

const receipts: AttendeeReceipt[] = orders
.map((attendee) => {
const appliedAt = attendee[Object.keys(attendee)[Constants.PEATIX_APPLIED_AT_ROW_INDEX]]
const receiptId = attendee[Object.keys(attendee)[Constants.PEATIX_RECEIPT_ID_ROW_INDEX]]
const ticketName = attendee[Object.keys(attendee)[Constants.PEATIX_RECEIPTS_TICKET_NAME_ROW_INDEX]]

const d = new Date(appliedAt)
const appliedDate = `${d.getFullYear()}/${d.getMonth() + 1}/${d.getDate()}`

// チケット名から参加者の種別を特定する
if (ticketName.includes(Constants.PEATIX_GENERAL_TICKET)) {
return { date: appliedDate, role: Constants.PEATIX_GENERAL_ROLE, receipt_id: receiptId }
}
if (ticketName.includes(Constants.PEATIX_WITH_PARTY_TICKET)) {
return { date: appliedDate, role: Constants.PEATIX_WITH_PARTY_ROLE, receipt_id: receiptId }
}

// return { role: 'attendee', receipt_id: receiptId }
})
.filter(v => v) // null は除外

const startedDate = new Date(2024, 7 - 1, 29)
let archives = [[], [], []]
for (let date = startedDate; date <= new Date(); date.setDate(date.getDate() + 1)) {
archives[0].push(`${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`)
archives[1].push(`${receipts.filter(v => v.role === Constants.PEATIX_GENERAL_ROLE).filter(v => v.date.includes(`${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`)).length}`)
archives[2].push(`${receipts.filter(v => v.role === Constants.PEATIX_WITH_PARTY_ROLE).filter(v => v.date.includes(`${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`)).length}`)
}
for (const archive of archives) {
this.logger.log(`${archive.join(', ')} `)
this.discordService.send('After Purchase Bot', `${archive.join(', ')} `)
}
}
}
13 changes: 12 additions & 1 deletion apps/peatix-adapter/src/supabase/supabase.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,18 @@ export class SupabaseService {
const { data, error } = await this.client.from('attendees')
.upsert({ role: targetData.role, activated_at: new Date().toISOString() })
.eq('receipt_id', targetData.receipt_id)
.eq('activated_at', null)
.eq('activated_at', '')
if (error) return { status: false, data: null }

return { status: true, data }
}

public async updateUnactivatedAttendees() {
this.getClient()

const { data, error } = await this.client.from('attendees')
.upsert({ canceled_at: new Date().toISOString() })
.eq('activated_at', '')
if (error) return { status: false, data: null }

return { status: true, data }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Logger } from '@nestjs/common'
import { CommandRunner, Command } from 'nest-commander'
import { UnactivatedCheckService } from './unactivated-check.service'

type CommandOptions = {
id?: number;
};

@Command({ name: 'unactivated-check', description: 'An unactivated check command.' })
export class UnactivatedCheckCommand extends CommandRunner {
private readonly logger = new Logger(UnactivatedCheckCommand.name)

constructor(readonly unactivatedCheckService: UnactivatedCheckService) {
super()
}

async run(params: string[], options?: CommandOptions): Promise<void> {
this.unactivatedCheckService.apply()
return Promise.resolve()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Module } from '@nestjs/common'
import { ConfigModule, ConfigService } from '@nestjs/config'
import { UnactivatedCheckService } from './unactivated-check.service'
import { EnvService } from '../env/env.service'
import {
symbol as IPuppeteerService,
PuppeteerService,
} from '../puppeteer/puppeteer.service'
import { UnactivatedCheckCommand } from './unactivated-check.command'
import { configuration } from '../env/utils'
import { EnvModule } from '../env/env.module'
import { HttpModule } from '@nestjs/axios'
import { SupabaseModule } from 'src/supabase/supabase.module'
import { SupabaseService } from 'src/supabase/supabase.service'
import { DiscordModule } from 'src/discord/discord.module'
import { DiscordService } from 'src/discord/discord.service'

@Module({
imports: [
ConfigModule.forRoot({
envFilePath: '.env',
expandVariables: true,
load: [configuration],
}),
EnvModule,
HttpModule,
SupabaseModule,
DiscordModule,
],
providers: [
ConfigService,
EnvService,
{
provide: IPuppeteerService,
useClass: PuppeteerService,
},
UnactivatedCheckCommand,
UnactivatedCheckService,
SupabaseService,
DiscordService,
],
})
export class UnactivatedCheckModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable, Logger } from '@nestjs/common'
import { SupabaseService } from 'src/supabase/supabase.service'
import { DiscordService } from 'src/discord/discord.service'

@Injectable()
export class UnactivatedCheckService {
private readonly logger = new Logger(UnactivatedCheckService.name)

constructor(
private readonly supabaseService: SupabaseService,
private readonly discordService: DiscordService,
) {
//
}

async apply() {
this.supabaseService.updateUnactivatedAttendees()

this.discordService.send('After Purchase Bot', '未反映者 反映済')
}
}
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"dev": "turbo dev",
"build": "turbo build",
"build-after-purchase": "turbo build-after-purchase",
"build-unactivated-check": "turbo build-unactivated-check",
"build-sales-daily": "turbo build-sales-daily",
"build-docs": "turbo build-docs",
"build-storybook": "turbo build-storybook",
"lint": "turbo lint",
Expand Down
Loading