Skip to content

Commit

Permalink
Melhoria no Location
Browse files Browse the repository at this point in the history
  • Loading branch information
rtenorioh committed Oct 14, 2024
1 parent 4cf78a3 commit bf3ed16
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { QueryInterface } from "sequelize";

module.exports = {
up: (queryInterface: QueryInterface) => {
return queryInterface.bulkInsert(
"Integrations",
[
{
key: "apiMaps",
value: "",
createdAt: new Date(),
updatedAt: new Date()
}
],
{}
);
},

down: (queryInterface: QueryInterface) => {
return queryInterface.bulkDelete("Integrations", {});
}
};
68 changes: 40 additions & 28 deletions backend/src/services/WbotServices/wbotMessageListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,46 @@ const verifyMediaMessage = async (
return newMessage;
};

const prepareLocation = (msg: WbotMessage): WbotMessage => {
const getGeocode = async (
latitude: number,
longitude: number
): Promise<string> => {
const apiKey = await Integration.findOne({
where: { key: "apiMaps" }
});

const url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}&key=${apiKey?.value}`;

return new Promise((resolve, reject) => {
request(url, { json: true }, (err: any, res: any, body: any) => {
if (err) {
reject(err);
} else if (body.results && body.results.length > 0) {
resolve(body.results[0].formatted_address);
} else {
resolve(`${latitude}, ${longitude}`);
}
});
});
};

const prepareLocation = async (msg: WbotMessage): Promise<WbotMessage> => {
const gmapsUrl = `https://maps.google.com/maps?q=${msg.location.latitude}%2C${msg.location.longitude}&z=17`;
msg.body = `data:image/png;base64,${msg.body}|${gmapsUrl}`;
msg.body += `|${msg.location.options
? msg.location.options
: `${msg.location.latitude}, ${msg.location.longitude}`
}`;

try {
const address = await getGeocode(
Number(msg.location.latitude),
Number(msg.location.longitude)
);

msg.body = `data:image/png;base64,${msg.body}|${gmapsUrl}`;
msg.body += `|${address || `${msg.location.latitude}, ${msg.location.longitude}`
}`;
} catch (error) {
console.error("Erro ao preparar a localização:", error);
msg.body += `|${msg.location.latitude}, ${msg.location.longitude}`;
}

return msg;
};

Expand All @@ -274,7 +307,7 @@ const verifyMessage = async (
ticket: Ticket,
contact: Contact
) => {
if (msg.type === "location") msg = prepareLocation(msg);
if (msg.type === "location") msg = await prepareLocation(msg);

const quotedMsg = await verifyQuotedMessage(msg);
const messageData = {
Expand All @@ -288,27 +321,6 @@ const verifyMessage = async (
quotedMsgId: quotedMsg?.id
};

// if (msg.fromMe === true) {
// await ticket.update({
// fromMe: msg.fromMe,
// lastMessage:
// msg.type === "location"
// ? msg.location.options
// ? `🢅 Localization - ${msg.location.options}`
// : "🢅 🗺️: Localization"
// : `🢅 ${msg.body}`
// });
// } else {
// await ticket.update({
// lastMessage:
// msg.type === "location"
// ? msg.location.options
// ? `🢇 🗺️: Localization - ${msg.location.options}`
// : "🢇 🗺️: Localization"
// : `🢇 ${msg.body}`
// });
// }

if (msg.fromMe === true) {
await ticket.update({
fromMe: msg.fromMe,
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/pages/Integrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,25 @@ const Integrations = () => {
/>
</Paper>
</Paper>

<Paper className={classes.paper1}>
<Typography align="center" variant="body1">
{i18n.t("integrations.integrations.maps.title")}
</Typography>
<Paper elevation={4} className={classes.paper}>
<TextField
style={{ width: "100%" }}
id="apiMaps"
name="apiMaps"
margin="dense"
label={i18n.t("integrations.integrations.maps.apiMaps")}
variant="outlined"
value={integrations && integrations.length > 0 && getIntegrationValue("apiMaps")}
onChange={handleChangeIntegration}
fullWidth
/>
</Paper>
</Paper>
</Container>
</div>
);
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/translate/languages/pt.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,10 @@ const messages = {
hub: {
title: "Notificame Hub",
hubToken: "Token"
},
maps: {
title: "Api Google Maps",
apiMaps: "Api Key"
}
},
},
Expand Down

0 comments on commit bf3ed16

Please sign in to comment.