Skip to content

Commit

Permalink
WBOT: UPDATE PRIVACY MODAL (#137)
Browse files Browse the repository at this point in the history
* WBOT: UPDATE PRIVACY MODAL
---------

Co-authored-by: Claudemir Todo Bom <[email protected]>
  • Loading branch information
brunocgc and allgood authored Aug 15, 2024
1 parent 1c09b6f commit 197642d
Show file tree
Hide file tree
Showing 10 changed files with 3,108 additions and 2,421 deletions.
46 changes: 46 additions & 0 deletions backend/src/controllers/PrivacyController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Request, Response } from "express";
import ShowPrivacyService from "../services/PrivacyService/ShowPrivacyService";
import Whatsapp from "../models/Whatsapp";
import AppError from "../errors/AppError";
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
import UpdatePrivacyWhatsapp from "../services/PrivacyService/UpdatePrivacyWhatsapp";

interface PrivacyData {
readreceipts?: 'all' | 'none';
profile?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
status?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
online?: 'all' | 'match_last_seen';
last?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
groupadd?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
calladd?: 'all' | 'known';
disappearing?: '86400' | '604800' | '7776000' | '0'
whatsappId?: number;
}
export const show = async (req: Request, res: Response): Promise<Response> => {
const { whatsappId } = req.params;
const { companyId } = req.user;

const whatsapp: Whatsapp = await ShowWhatsAppService(whatsappId, companyId);

if(whatsapp) {
const privacy: PrivacyData = await ShowPrivacyService(whatsappId);
return res.status(200).json(privacy);
}

throw new AppError("ERR_NO_PRIVACY_FOUND", 404);
}

export const update = async (req: Request, res: Response): Promise<Response> => {
const { whatsappId } = req.params;
const { companyId } = req.user;

const whatsapp: Whatsapp = await ShowWhatsAppService(whatsappId, companyId);

if(whatsapp) {
const privacyData: PrivacyData = req.body;
const privacy: PrivacyData = await UpdatePrivacyWhatsapp(whatsapp.id, privacyData, true);
return res.status(200).json(privacy);
}

throw new AppError("ERR_NO_PRIVACY_FOUND", 404);
}
10 changes: 7 additions & 3 deletions backend/src/routes/whatsappRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express from "express";
import isAuth from "../middleware/isAuth";

import * as WhatsAppController from "../controllers/WhatsAppController";
import * as PrivacyController from "../controllers/PrivacyController";

const whatsappRoutes = express.Router();

Expand All @@ -16,9 +17,12 @@ whatsappRoutes.get("/whatsapp/:whatsappId", isAuth, WhatsAppController.show);
whatsappRoutes.put("/whatsapp/:whatsappId", isAuth, WhatsAppController.update);

whatsappRoutes.delete(
"/whatsapp/:whatsappId",
isAuth,
WhatsAppController.remove
"/whatsapp/:whatsappId",
isAuth,
WhatsAppController.remove
);

whatsappRoutes.get("/whatsapp/privacy/:whatsappId", isAuth, PrivacyController.show);
whatsappRoutes.put("/whatsapp/privacy/:whatsappId", isAuth, PrivacyController.update);

export default whatsappRoutes;
30 changes: 30 additions & 0 deletions backend/src/services/PrivacyService/ShowPrivacyService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import AppError from "../../errors/AppError";
import { getWbot, Session } from "../../libs/wbot";

interface PrivacyData {
readreceipts?: 'all' | 'none';
profile?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
status?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
online?: 'all' | 'match_last_seen';
last?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
groupadd?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
calladd?: 'all' | 'known';
disappearing?: '86400' | '604800' | '7776000' | '0'
whatsappId?: number;
}

const ShowPrivacyService = async (whatsappId: number | string): Promise<PrivacyData> => {
if(typeof whatsappId === "string"){
whatsappId = parseInt(whatsappId);
}
const wbot:Session = getWbot(whatsappId);
const privacy: PrivacyData = await wbot.fetchPrivacySettings(true)

if (!privacy) {
throw new AppError("ERR_NO_PRIVACY_FOUND", 404);
}

return privacy;
};

export default ShowPrivacyService;
41 changes: 41 additions & 0 deletions backend/src/services/PrivacyService/UpdatePrivacyWhatsapp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { getWbot, Session } from "../../libs/wbot";
import { logger } from "../../utils/logger";
import ShowPrivacyService from "./ShowPrivacyService";

interface Request {
readreceipts?: 'all' | 'none';
profile?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
status?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
online?: 'all' | 'match_last_seen';
last?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
groupadd?: 'all' | 'contacts' | 'contact_blacklist' | 'none';
calladd?: 'all' | 'known';
disappearing?: '86400' | '604800' | '7776000' | '0'
whatsappId?: number;
}

const UpdatePrivacyWhatsapp = async (whatsappId: number, privacySettings: Request, update: boolean = false): Promise<Request> => {
try {
const privacy: Request = await ShowPrivacyService(whatsappId);
if(privacy) {
if(update) {
const wbot: Session = getWbot(whatsappId);
privacy.readreceipts !== privacySettings.readreceipts ? wbot.updateReadReceiptsPrivacy(privacySettings.readreceipts) : null;
privacy.profile !== privacySettings.profile ? wbot.updateProfilePicturePrivacy(privacySettings.profile) : null;
privacy.status !== privacySettings.status ? wbot.updateStatusPrivacy(privacySettings.status) : null;
privacy.online !== privacySettings.online ? wbot.updateOnlinePrivacy(privacySettings.online) : null;
privacy.last !== privacySettings.last ? wbot.updateLastSeenPrivacy(privacySettings.last) : null;
privacy.groupadd !== privacySettings.groupadd ? wbot.updateGroupsAddPrivacy(privacySettings.groupadd) : null;
privacy.calladd !== privacySettings.calladd ? wbot.updateCallPrivacy(privacySettings.calladd) : null;
privacy.disappearing !== privacySettings.disappearing ? wbot.updateDefaultDisappearingMode(parseInt(privacySettings.disappearing)) : null;
}

return privacy;
} else {
return privacy;
}
} catch(err) {
logger.error(err);
}
}
export default UpdatePrivacyWhatsapp;
Loading

0 comments on commit 197642d

Please sign in to comment.