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

Push message for multiple shopId in wallet address #404

Merged
merged 1 commit into from
Nov 29, 2024
Merged
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
14 changes: 11 additions & 3 deletions packages/relay/src/routers/ETCRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { ResponseMessage } from "../utils/Errors";
import express from "express";
import { body, param, query, validationResult } from "express-validator";

import { HashZero } from "@ethersproject/constants";

export class ETCRouter {
private web_service: WebService;
private readonly config: Config;
Expand Down Expand Up @@ -118,6 +120,7 @@ export class ETCRouter {
try {
const account: string = String(req.body.account).trim();
const type = req.body.type === undefined ? 0 : Number(req.body.type);
const shopId = req.body.shopId === undefined ? HashZero : String(req.body.shopId).trim();
const token: string = String(req.body.token).trim();
const language: string = String(req.body.language).trim();
const os: string = String(req.body.os).trim();
Expand All @@ -130,6 +133,7 @@ export class ETCRouter {
const item = {
account,
type,
shopId,
token,
language,
os,
Expand Down Expand Up @@ -167,11 +171,12 @@ export class ETCRouter {
try {
const account: string = String(req.body.account).trim();
const type: number = Number(req.body.type);
const shopId = req.body.shopId === undefined ? HashZero : String(req.body.shopId).trim();
const title: string = String(req.body.title).trim();
const contents: string = String(req.body.contents).trim();
const contentType: string = String(req.body.contentType).trim();

const mobileData = await this.storage.getMobile(account, type);
const mobileData = await this.storage.getMobile(account, type, shopId);
if (mobileData !== undefined) {
await this.sender.send(mobileData.token, title, contents, { type: contentType });
}
Expand Down Expand Up @@ -204,8 +209,9 @@ export class ETCRouter {
try {
const account: string = String(req.params.account).trim();
const type: number = Number(req.query.type);
const shopId = req.query.shopId === undefined ? HashZero : String(req.query.shopId).trim();

const mobileData = await this.storage.getMobile(account, type);
const mobileData = await this.storage.getMobile(account, type, shopId);
if (mobileData === undefined) {
return res.status(200).json(ResponseMessage.getErrorMessage("2008"));
}
Expand All @@ -215,6 +221,7 @@ export class ETCRouter {
this.makeResponseData(0, {
account: mobileData.account,
type: mobileData.type,
shopId: mobileData.shopId,
token: mobileData.token,
language: mobileData.language,
os: mobileData.os,
Expand Down Expand Up @@ -243,7 +250,8 @@ export class ETCRouter {
try {
const account: string = String(req.params.account).trim();
const type: number = Number(req.query.type);
const mobileData = await this.storage.getMobile(account, type);
const shopId = req.query.shopId === undefined ? HashZero : String(req.query.shopId).trim();
const mobileData = await this.storage.getMobile(account, type, shopId);
const exists = mobileData !== undefined && mobileData.account.toLowerCase() === account.toLowerCase();

this.metrics.add("success", 1);
Expand Down
2 changes: 1 addition & 1 deletion packages/relay/src/routers/PaymentRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ export class PaymentRouter {
);
}

const mobileData = await this.storage.getMobile(shopInfo.account, MobileType.SHOP_APP);
const mobileData = await this.storage.getMobile(shopInfo.account, MobileType.SHOP_APP, shopInfo.shopId);

if (!this.config.relay.testMode && mobileData === undefined) {
return res.status(200).json(ResponseMessage.getErrorMessage("2005"));
Expand Down
4 changes: 2 additions & 2 deletions packages/relay/src/routers/ShopRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ export class ShopRouter {
);
}

const mobileData = await this.storage.getMobile(item.account, MobileType.SHOP_APP);
const mobileData = await this.storage.getMobile(item.account, MobileType.SHOP_APP, item.shopId);

if (!this.config.relay.testMode && mobileData === undefined) {
return res.status(200).json(ResponseMessage.getErrorMessage("2005"));
Expand Down Expand Up @@ -992,7 +992,7 @@ export class ShopRouter {
}

/// 사용자에게 푸쉬 메세지 발송
const mobileData = await this.storage.getMobile(item.account, MobileType.SHOP_APP);
const mobileData = await this.storage.getMobile(item.account, MobileType.SHOP_APP, item.shopId);

if (!this.config.relay.testMode && mobileData === undefined) {
return res.status(200).json(ResponseMessage.getErrorMessage("2005"));
Expand Down
8 changes: 6 additions & 2 deletions packages/relay/src/storage/RelayStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
} from "../types";
import { ContractUtils } from "../utils/ContractUtils";

import { HashZero } from "@ethersproject/constants";

import * as hre from "hardhat";

/**
Expand Down Expand Up @@ -664,6 +666,7 @@ export class RelayStorage extends Storage {
this.queryForMapper("mobile", "postMobile", {
account: item.account,
type: item.type,
shopId: item.shopId,
token: item.token,
language: item.language,
os: item.os,
Expand All @@ -678,15 +681,16 @@ export class RelayStorage extends Storage {
});
}

public getMobile(account: string, type: number): Promise<MobileData | undefined> {
public getMobile(account: string, type: number, shopId: string = HashZero): Promise<MobileData | undefined> {
return new Promise<MobileData | undefined>(async (resolve, reject) => {
this.queryForMapper("mobile", "getMobile", { account, type })
this.queryForMapper("mobile", "getMobile", { account, type, shopId })
.then((result) => {
if (result.rows.length > 0) {
const m = result.rows[0];
return resolve({
account: m.account,
type: m.type,
shopId: m.shopId,
token: m.token,
language: m.language,
os: m.os,
Expand Down
8 changes: 5 additions & 3 deletions packages/relay/src/storage/mapper/mobile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(
"account" ,
"type" ,
"shopId" ,
"token" ,
"language" ,
"os"
Expand All @@ -15,18 +16,19 @@
(
#{account} ,
#{type} ,
#{shopId} ,
#{token} ,
#{language} ,
#{os}
)
ON CONFLICT ("account", "type")
ON CONFLICT ("account", "type", "shopId")
DO UPDATE
SET "token" = #{token},
SET "token" = #{token},
"language" = #{language},
"os" = #{os};
</insert>

<select id="getMobile">
SELECT * FROM mobiles WHERE LOWER("account") = LOWER(#{account}) AND "type" = ${type} ;
SELECT * FROM mobiles WHERE LOWER("account") = LOWER(#{account}) AND "type" = ${type} AND LOWER("shopId") = LOWER(#{shopId}) ;
</select>
</mapper>
3 changes: 2 additions & 1 deletion packages/relay/src/storage/mapper/table.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@
(
"account" VARCHAR(42) NOT NULL,
"type" INTEGER DEFAULT 0,
"shopId" VARCHAR(66) DEFAULT '0x0000000000000000000000000000000000000000000000000000000000000000',
"token" VARCHAR(50) NOT NULL,
"language" VARCHAR(3) NOT NULL,
"os" VARCHAR(12) NOT NULL,
PRIMARY KEY ("account", "type")
PRIMARY KEY ("account", "type", "shopId")
);
</sql>

Expand Down
1 change: 1 addition & 0 deletions packages/relay/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export enum MobileType {
export interface MobileData {
account: string;
type: MobileType;
shopId: string;
token: string;
language: string;
os: string;
Expand Down
7 changes: 7 additions & 0 deletions packages/relay/test/Payment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,12 @@ describe("Test of Server", function () {
config.relay.certifiers = deployments.accounts.certifiers.map((m) => m.privateKey);
config.relay.callbackEndpoint = "http://127.0.0.1:3400/callback";
config.relay.relayEndpoint = `http://127.0.0.1:${config.server.port}`;

client = new TestClient({
headers: {
Authorization: config.relay.accessKey,
},
});
});

before("Create TestServer", async () => {
Expand Down Expand Up @@ -1585,6 +1591,7 @@ describe("Test of Server", function () {
const param = {
account: wallet.address,
type: 1,
shopId: shopData[purchaseOfLoyalty.shopIndex].shopId,
token: "12345678901234567890123456789012345678901234567890",
language: "kr",
os: "iOS",
Expand Down
1 change: 1 addition & 0 deletions packages/relay/test/PaymentV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,7 @@ describe("Test of Server", function () {
const param = {
account: wallet.address,
type: 1,
shopId: shopData[purchaseOfLoyalty.shopIndex].shopId,
token: "12345678901234567890123456789012345678901234567890",
language: "kr",
os: "iOS",
Expand Down