Skip to content

Commit

Permalink
Refine responses
Browse files Browse the repository at this point in the history
  • Loading branch information
yuneg11 committed Dec 10, 2019
1 parent 38241b8 commit 7d526c2
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 57 deletions.
25 changes: 16 additions & 9 deletions src/backend/handlers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const list = async (event) => {
} else if (result.length == 0) {
return responseTemplate.itemListFail("등록된 상품이 없습니다.");
} else {
return responseTemplate.itemListSuccess(result);
return responseTemplate.itemListSuccess(result, "list");
}
};

Expand All @@ -44,7 +44,7 @@ const searchCategory = async (event) => {
} else if (result.length == 0) {
return responseTemplate.itemListFail("등록된 상품이 없습니다.");
} else {
return responseTemplate.itemListSuccess(result);
return responseTemplate.itemListSuccess(result, "search");
}
};

Expand All @@ -58,26 +58,33 @@ const searchKeyword = async (event) => {
} else if (result.length == 0) {
return responseTemplate.itemListFail("등록된 상품이 없습니다.");
} else {
return responseTemplate.itemListSuccess(result);
return responseTemplate.itemListSuccess(result, "search");
}
};

const detail = async (event) => {
const extras = parser.getExtras(event);
const mode = extras["mode"];
const userId = extras["userId"];
const itemId = extras["itemId"];

const item = await database.getItem(itemId);
if (item != null) {
return responseTemplate.itemDetail(item);
const user = (mode == "search" ? await database.getUser(userId) : null);
if (user != null) {
return responseTemplate.itemDetail(item, mode, user);
} else {
return responseTemplate.itemListFail("상품 조회에 실패했습니다.");
}
} else {
return responseTemplate.itemListFail("상품 조회에 실패했습니다.");
}
};

module.exports = {
registration,
list,
searchCategory,
searchKeyword,
detail
registration,
list,
searchCategory,
searchKeyword,
detail
};
31 changes: 21 additions & 10 deletions src/backend/package-lock.json

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

2 changes: 1 addition & 1 deletion src/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
"author": "hakuhagu",
"license": "MIT",
"dependencies": {
"aws-sdk": "^2.585.0",
"mysql": "^2.17.1",
"nodemailer": "^6.4.0",
"qrcode": "^1.4.4",
"sync-request": "^6.1.0"
},
"devDependencies": {
"aws-sdk": "^2.586.0",
"serverless-plugin-warmup": "^4.7.1-rc.1"
}
}
2 changes: 1 addition & 1 deletion src/backend/responses/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ module.exports.welcome = () => {
const userInfoCardBody = builder.getBasicCardBody("회원 정보", "회원 정보를 조회하거나 수정합니다.", userInfoThumbnail, [userInfoQueryButton, userInfoOpenProfileButton, userInfoTimeTableButton]);

// Build response
return builder.buildResponse([builder.getCarousel("basicCard", [buyerCardBody, sellerCardBody, userInfoCardBody])]);
return builder.buildResponse([builder.getCarousel("basicCard", [buyerCardBody, sellerCardBody, userInfoCardBody])], false);
};
70 changes: 44 additions & 26 deletions src/backend/responses/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,43 @@ module.exports.itemRegistrationFail = (errorMessage) => {
return builder.buildResponse([resultCard]);
};

module.exports.itemListSuccess = (itemList) => {
// Construct commerce cards
const bodys = [];
for (var key in itemList) {
if (key > 9) {
break;
module.exports.itemListSuccess = (itemList, mode="search") => {
const modes = ["list", "search"];

if (modes.includes(mode)) {
// Construct commerce cards
const bodys = [];
for (var key in itemList) {
if (key > 9) {
break;
}
const item = itemList[key];
const resultUserId = (item.userId != undefined ? item.userId : null);
const resultItemId = item.itemId;
const resultThumbnail = builder.getThumbnail(item.item_image[0]);
const resultTitle = item.item_name;
const resultDescription = item.item_detail + "\n" + item.item_date;
const resultPrice = item.item_price;
const resultNickname = (item.nickname != undefined ? item.nickname : null);
const buttons = [];
if (mode == "list") {
buttons.push(builder.getButton("상세정보", "block", "상세정보", resource.itemDetailBlockId, {itemId: resultItemId, mode: "list"}));
buttons.push(builder.getButton("삭제", "block", "삭제", resource.itemDeleteBlockId, {itemId: resultItemId}));
} else if (mode == "search") {
buttons.push(builder.getButton("구매", "block", "구매", resource.itemBuyBlockId, {userId: resultUserId, itemId: resultItemId}));
buttons.push(builder.getButton("상세정보", "block", "상세정보", resource.itemDetailBlockId, {userId: resultUserId, itemId: resultItemId, mode: "search"}));
// buttons.push(builder.getButton("신고", "block", "신고", resource.itemReportBlockId, {userId: resultUserId, itemId: resultItemId}));
} else if (mode == "transaction") {
buttons.push(builder.getButton("판매", "block", "판매", resource.itemSellBlockId, {itemId: resultItemId}));
}
bodys.push(builder.getCommerceCardBody(resultTitle, resultDescription, resultPrice, resultThumbnail, resultNickname, buttons));
}
const item = itemList[key];
const resultItemId = item.itemId;
const resultThumbnail = builder.getThumbnail(item.item_image[0]);
const resultTitle = item.item_name;
const resultDescription = item.item_detail;
const resultPrice = item.item_price;
const resultNickname = (item.nickname != undefined ? item.nickname : null);
const resultOpenprofile = item.openprofile;
const resultMainMenuButton = builder.getButton("상세정보", "block", "상세정보", "5decfa26ffa74800014b098e", {itemId: resultItemId});
bodys.push(builder.getCommerceCardBody(resultTitle, resultDescription, resultPrice, resultThumbnail, resultNickname, [resultMainMenuButton]));
}

// Build response
return builder.buildResponse([builder.getCarousel("commerceCard", bodys)]);
// Build response
return builder.buildResponse([builder.getCarousel("commerceCard", bodys)]);
} else {
throw "mode type error";
}
};

module.exports.itemListFail = (errorMessage) => {
Expand All @@ -63,15 +79,17 @@ module.exports.itemListFail = (errorMessage) => {
return builder.buildResponse([resultCard]);
};

module.exports.itemDetail = (item) => {
module.exports.itemDetail = (item, mode="list", user=null) => {
// Construct registration guide
const resultThumbnail = builder.getThumbnail(item.item_image[0]);
const resultTitle = item.item_name;
const resultDescription = item.item_detail;
const resultPrice = item.item_price;
const resultMainMenuButton = builder.getButton("처음으로", "block", "처음으로", resource.welcomeBlockId);
const resultCard = builder.getCommerceCard(resultTitle, resultDescription, resultPrice, resultThumbnail, null, [resultMainMenuButton]);
const resultUserId = (user.userId != undefined ? user.userId : null);
const resultItemId = item.itemId;
const resultTitle = item.item_name + "(" + item.item_price + "원)";
const resultDescription = item.item_detail + "\n";// + item.item_date;
const resultMainMenuButton = (mode == "list" ? builder.getButton("삭제", "block", "삭제", resource.itemDeleteBlockId) : builder.getButton("구매", "block", "구매", resource.itemBuyBlockId, {userId: resultUserId, itemId: resultItemId}));
const resultCard1 = builder.getBasicCardBody(resultTitle, resultDescription, "", [resultMainMenuButton, resultMainMenuButton]);
const resultCard2 = builder.getBasicCardBody("", "uiiyfiyiyfi", resultThumbnail);

// Build response
return builder.buildResponse([resultCard]);
return builder.buildResponse([builder.getCarousel("basicCard", [resultCard1, resultCard2, resultCard2])]);
};
51 changes: 41 additions & 10 deletions src/backend/utils/builder.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict';

const buildResponse = (outputs) => {
const resource = require("./resource");

const buildResponse = (outputs, quickMain=true) => {
return {
statusCode: 200,
body: JSON.stringify({
version: "2.0",
template: {
outputs: outputs
outputs: outputs,
quickReplies: (quickMain == true ? [getQuickReply("처음으로", "block", "처음으로", resource.welcomeBlockId)] : [])
}
})
};
Expand All @@ -19,6 +22,25 @@ const buildAWSResponse = (body) => {
};
}

const getQuickReply = (label, action, message, blockId=undefined, extra=undefined) => {
const actionTypes = ["message", "block"];

if (actionTypes.includes(action)) {
var quickReply = {
label: label,
action: action,
messageText: message
};
switch (action) {
case "block": quickReply["blockId"] = blockId;
if (extra !== undefined) quickReply["extra"] = extra;
}
return quickReply;
} else {
throw "action type error";
}
}

const getButton = (label, action, data, blockId=undefined, extra=undefined) => {
const actionTypes = ["webLink", "osLink", "message", "phone", "share", "block"];

Expand Down Expand Up @@ -118,17 +140,26 @@ const getCarousel = (type, cards) => {
}
}

const getCommerceCardBody = (title, description, price, thumbnail, nickname, buttons=[]) => {
const body = {
description: title + "\n" + description,
price: price,
currency: "won",
thumbnails: [ thumbnail ],
buttons: buttons
};
const getCommerceCardBody = (title, description, price, thumbnail, nickname, buttons=null) => {
const body = {};
if (title != null || description != null) {
var str = (title != null ? title + "\n" : "");
str += (description != null ? description : "");
body["description"] = str;
}
if (price != null) {
body["price"] = price;
body["currency"] = "won"
}
if (thumbnail != null) {
body["thumbnails"] = [ thumbnail ];
}
if (nickname != null) {
body["profile"] = { nickname: nickname }
}
if (buttons != null) {
body["buttons"] = buttons;
}
return body;
}

Expand Down
5 changes: 5 additions & 0 deletions src/backend/utils/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ module.exports = {
userInfoQueryBlockId: "5de3d56d8192ac0001d65d18",
userInfoOpenProfileBlockId: "5de3d5958192ac0001d65d1a",
userInfoTimeTableBlockId: "5de3d59d8192ac0001d65d1c",
itemDetailBlockId: "5decfa26ffa74800014b098e",
itemDeleteBlockId: "5decfa26ffa74800014b098e", // Temp
itemBuyBlockId: "5decfa26ffa74800014b098e", // Temp
itemReportBlockId: "5decfa26ffa74800014b098e", // Temp
itemSellBlockId: "5decfa26ffa74800014b098e", // Temp
};

0 comments on commit 7d526c2

Please sign in to comment.