Skip to content

Commit

Permalink
add: Get QR Code
Browse files Browse the repository at this point in the history
  • Loading branch information
KAnggara75 committed Jul 11, 2024
1 parent a5090bf commit 07ca695
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"link-preview-js": "^3.0.5",
"pino": "^9.2.0",
"prisma": "^5.16.1",
"qrcode": "^1.5.3",
"qrcode-terminal": "^0.12.0",
"typescript": "^5.5.3",
"uuid": "^10.0.0",
Expand Down
103 changes: 103 additions & 0 deletions pnpm-lock.yaml

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

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 19 additions & 3 deletions src/controllers/qrController.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import QRCode from "qrcode";
import { QRService } from "../services/qrService";
import { isWAConnected } from "../modules/whatsapp";
import type { NextFunction, Request, Response } from "express";

export class QRController {
static async getQR(req: Request, res: Response, next: NextFunction) {
try {
if (!isWAConnected()) {
res.status(200).json({
qr_code: "result",
image_url: "result",
await QRService.getQR().then((qr: string) => {
res.status(200).json({
qr_code: qr,
image_url: `${req.protocol}://${req.get("host")}${req.originalUrl}/show?qrcode=${qr}`,
});
});
} else {
res.status(200).json({
Expand All @@ -18,4 +22,16 @@ export class QRController {
next(e);
}
}

static async showQR(req: Request, res: Response, next: NextFunction) {
try {
const qrcode = req.query.qrcode || "";
const qrCodeImage = await QRCode.toDataURL(qrcode);
res.send(`
<img src="${qrCodeImage}" alt="QR Code" style="display:block;margin-left:auto;margin-right:auto;border-style:solid;border-color:blue;"/>
`);
} catch (e) {
next(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ CREATE TABLE `users` (
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `qr_code` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`qr` VARCHAR(500) NOT NULL,
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`last_update` DATETIME(3) NULL,

PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `messages` (
`id` VARCHAR(191) NOT NULL,
Expand Down
9 changes: 9 additions & 0 deletions src/database/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ model User {
@@map("users")
}

model qrCode {
id Int @id @default(autoincrement())
qr String @db.VarChar(500)
created_at DateTime @default(now())
last_update DateTime? @updatedAt
@@map("qr_code")
}

model Message {
id String @unique
text String
Expand Down
1 change: 1 addition & 0 deletions src/main/routes/publicRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ publicRouter.get("/", getVersion);
publicRouter.post("/users", UserController.register);

publicRouter.get("/qr", QRController.getQR);
publicRouter.get("/qr/show", QRController.showQR);

// Messages
publicRouter.post("/messages", MessageController.sendMessage);
Expand Down
4 changes: 1 addition & 3 deletions src/main/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ app.use("/", publicRouter);
app.use(ErrorMiddleware);
app.disable("x-powered-by");

// app.use(express.static(path.join(__dirname, "..", "..", "public")));
app.set("view engine", "ejs");
// app.set("views", path.join(__dirname, "..", "..", "public"));
// app.use(express.static(path.join(__dirname, "../../../public")));

export { app as web };
Loading

0 comments on commit 07ca695

Please sign in to comment.