Skip to content

Commit

Permalink
feat: universal logger, declarationMap
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrycool123 committed Feb 23, 2024
1 parent 6a7f1c9 commit 7e0cb7a
Show file tree
Hide file tree
Showing 99 changed files with 3,681 additions and 1,546 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.js
*.mjs
dist/
34 changes: 34 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Run tests

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

- uses: pnpm/action-setup@v2
with:
version: 8

- name: Install dependencies
run: pnpm install

- name: Load test environment variables
run: echo "${{ secrets.OCR_SERVICE_TEST_ENV }}" | base64 --decode >> ./packages/ocr-service/.env.test

- name: Run tests
run: pnpm run test
15 changes: 8 additions & 7 deletions .github/workflows/deploy-sls-check-membership.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
name: Deploy sls-check-membership to AWS Lambda

on:
push:
branches:
- main
workflow_run:
workflows: ['Run tests']
types:
- completed

jobs:
deploy:
runs-on: ubuntu-latest
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand All @@ -24,9 +28,6 @@ jobs:
- name: Install dependencies
run: pnpm install

- name: Build @divine-bridge/common
run: pnpm -F @divine-bridge/common run build

- name: Load environment variables
run: echo "${{ secrets.SLS_CHECK_MEMBERSHIP_ENV }}" | base64 --decode >> ./apps/sls-check-membership/.env.production.local

Expand All @@ -35,4 +36,4 @@ jobs:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: 'us-east-1'
run: pnpm -F sls-check-membership run deploy
run: pnpm run deploy:sls-check-membership
12 changes: 8 additions & 4 deletions .github/workflows/deploy-sls-ocr.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
name: Deploy sls-ocr to AWS Lambda

on:
push:
branches:
- main
workflow_run:
workflows: ['Run tests']
types:
- completed

jobs:
deploy:
runs-on: ubuntu-latest
env:
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
Expand All @@ -32,4 +36,4 @@ jobs:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: 'us-east-1'
run: pnpm -F sls-ocr run deploy
run: pnpm run deploy:sls-ocr
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ node_modules/
.env.production
.env.bak
.env.*.local
.env.test
.serverless/
.aws.env
.DS_Store
.DS_Store

# vercel
.vercel
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"files.exclude": {
"**/node_modules": true,
// "**/node_modules": true,
"**/.turbo": true,
"**/.next": true
},
Expand Down
1 change: 1 addition & 0 deletions apps/server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ GOOGLE_API_KEY=
OCR_API_ENDPOINT=
OCR_API_KEY=
DATA_ENCRYPTION_KEY=
LOGGER_URI=
1 change: 1 addition & 0 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@discordjs/core": "^1.1.1",
"@discordjs/rest": "^2.2.0",
"@divine-bridge/common": "workspace:^",
"@divine-bridge/ocr-service": "workspace:^",
"@panva/hkdf": "^1.1.1",
"@sapphire/framework": "^5.0.7",
"@sapphire/plugin-api": "^6.1.1",
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/commands/add-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { PermissionFlagsBits, RepliableInteraction } from 'discord.js';

import { discordBotApi } from '../utils/discord.js';
import { Utils } from '../utils/index.js';
import { logger } from '../utils/logger.js';
import { Validators } from '../utils/validators.js';

dayjs.extend(utc);
Expand Down Expand Up @@ -150,7 +151,7 @@ export class AddMemberCommand extends Command {
await confirmedInteraction.deferReply({ ephemeral: true });

// Initialize log service and membership service
const appEventLogService = await new AppEventLogService(discordBotApi, guild.id).init();
const appEventLogService = await new AppEventLogService(logger, discordBotApi, guild.id).init();
const membershipService = new MembershipService(discordBotApi, appEventLogService);

// Add membership to user
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/commands/delete-member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PermissionFlagsBits } from 'discord.js';

import { discordBotApi } from '../utils/discord.js';
import { Utils } from '../utils/index.js';
import { logger } from '../utils/logger.js';
import { Validators } from '../utils/validators.js';

dayjs.extend(utc);
Expand Down Expand Up @@ -94,7 +95,7 @@ export class DeleteMemberCommand extends Command {
await confirmedInteraction.deferReply({ ephemeral: true });

// Initialize log service and membership service
const appEventLogService = await new AppEventLogService(discordBotApi, guild.id).init();
const appEventLogService = await new AppEventLogService(logger, discordBotApi, guild.id).init();
const membershipService = new MembershipService(discordBotApi, appEventLogService);

// Remove membership from user
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/commands/delete-role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { PermissionFlagsBits } from 'discord.js';

import { discordBotApi } from '../utils/discord.js';
import { Utils } from '../utils/index.js';
import { logger } from '../utils/logger.js';
import { Validators } from '../utils/validators.js';

export class DeleteRoleCommand extends Command {
Expand Down Expand Up @@ -87,7 +88,7 @@ export class DeleteRoleCommand extends Command {
}

// Initialize log service and membership service
const appEventLogService = await new AppEventLogService(discordBotApi, guild.id).init();
const appEventLogService = await new AppEventLogService(logger, discordBotApi, guild.id).init();
const membershipService = new MembershipService(discordBotApi, appEventLogService);

// Remove membership role from DB
Expand Down
35 changes: 21 additions & 14 deletions apps/server/src/commands/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
MembershipRoleDoc,
YouTubeChannelDoc,
} from '@divine-bridge/common';
import { OCRService, supportedOCRLanguages } from '@divine-bridge/ocr-service';
import { Command } from '@sapphire/framework';
import { Attachment, Guild, RepliableInteraction } from 'discord.js';

import { OCRConstants } from '../services/ocr/constants.js';
import { OCRService } from '../services/ocr/index.js';
import { Env } from '../utils/env.js';
import { Utils } from '../utils/index.js';
import { Validators } from '../utils/validators.js';

Expand All @@ -38,8 +38,8 @@ export class VerifyCommand extends Command {
.setName('language')
.setDescription('The language of the text in your picture')
.addChoices(
...OCRConstants.supportedLanguages.map(({ name, code }) => ({
name,
...supportedOCRLanguages.map(({ language, code }) => ({
name: language,
value: code,
})),
)
Expand Down Expand Up @@ -70,8 +70,8 @@ export class VerifyCommand extends Command {
.setName('language')
.setDescription('The language of the text in your picture')
.addChoices(
...OCRConstants.supportedLanguages.map(({ name, code }) => ({
name,
...supportedOCRLanguages.map(({ language, code }) => ({
name: language,
value: code,
})),
)
Expand Down Expand Up @@ -199,14 +199,14 @@ export class VerifyCommand extends Command {
const logChannel = logChannelResult.data;

// Get language
let selectedLanguage: (typeof OCRConstants.supportedLanguages)[number];
let selectedLanguage: (typeof supportedOCRLanguages)[number];
if (langCode === null) {
selectedLanguage = OCRConstants.supportedLanguages.find(
selectedLanguage = supportedOCRLanguages.find(
({ code }) => code === userDoc.preference.language,
) ?? { name: 'English', code: 'eng' };
) ?? { language: 'English', code: 'eng' };
} else {
selectedLanguage = OCRConstants.supportedLanguages.find(({ code }) => code === langCode) ?? {
name: 'English',
selectedLanguage = supportedOCRLanguages.find(({ code }) => code === langCode) ?? {
language: 'English',
code: 'eng',
};
}
Expand Down Expand Up @@ -243,7 +243,7 @@ export class VerifyCommand extends Command {
const screenshotSubmission = Embeds.screenshotSubmission(
Utils.convertUser(user),
membershipRoleDoc,
selectedLanguage.name,
selectedLanguage.language,
guild.name,
picture.url,
);
Expand All @@ -254,17 +254,24 @@ export class VerifyCommand extends Command {
// Send picture to membership service for OCR
// ? Do not send error to user if OCR failed due to errors that are not related to the user
try {
const recognizedDate = await OCRService.recognizeBillingDate(
const ocrService = new OCRService(Env.OCR_API_ENDPOINT, Env.OCR_API_KEY);
const recognizedResult = await ocrService.recognizeBillingDate(
picture.url,
selectedLanguage.code,
);
if (!recognizedResult.success) {
this.container.logger.error(recognizedResult.error);
}
const recognizedDate = recognizedResult.success
? recognizedResult.date
: { year: null, month: null, day: null };

const adminActionRow = ActionRows.adminVerificationButton();
const membershipVerificationRequestEmbed = Embeds.membershipVerificationRequest(
Utils.convertUser(user),
recognizedDate,
membershipRoleDoc._id,
selectedLanguage.name,
selectedLanguage.language,
picture.url,
);

Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/interaction-handlers/membership-accept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { type ButtonInteraction, EmbedBuilder } from 'discord.js';
import { Constants } from '../constants.js';
import { discordBotApi } from '../utils/discord.js';
import { Utils } from '../utils/index.js';
import { logger } from '../utils/logger.js';
import { Validators } from '../utils/validators.js';

export class MembershipAcceptButtonHandler extends InteractionHandler {
Expand Down Expand Up @@ -101,7 +102,7 @@ export class MembershipAcceptButtonHandler extends InteractionHandler {
} = memberResult;

// Initialize log service and membership service
const appEventLogService = await new AppEventLogService(discordBotApi, guild.id).init();
const appEventLogService = await new AppEventLogService(logger, discordBotApi, guild.id).init();
const membershipService = new MembershipService(discordBotApi, appEventLogService);

// Add membership to user
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/interaction-handlers/membership-reject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type ButtonInteraction, EmbedBuilder, ModalSubmitInteraction } from 'di
import { Constants } from '../constants.js';
import { discordBotApi } from '../utils/discord.js';
import { Utils } from '../utils/index.js';
import { logger } from '../utils/logger.js';
import { Validators } from '../utils/validators.js';

export class MembershipRejectButtonHandler extends InteractionHandler {
Expand Down Expand Up @@ -90,7 +91,7 @@ export class MembershipRejectButtonHandler extends InteractionHandler {
const reason = modalSubmitInteraction.fields.getTextInputValue(modalInputCustomId);

// Initialize log service and membership service
const appEventLogService = await new AppEventLogService(discordBotApi, guild.id).init();
const appEventLogService = await new AppEventLogService(logger, discordBotApi, guild.id).init();
const membershipService = new MembershipService(discordBotApi, appEventLogService);

// Reject membership to user
Expand Down
44 changes: 0 additions & 44 deletions apps/server/src/services/ocr/constants.ts

This file was deleted.

Loading

0 comments on commit 7e0cb7a

Please sign in to comment.