Skip to content

Commit

Permalink
Merge pull request #9 from lucassabreu/feat/rm-devices
Browse files Browse the repository at this point in the history
feat: rm devices
  • Loading branch information
viniciusss authored Aug 19, 2022
2 parents 97d6ea9 + e3d88a0 commit c8dea17
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 30 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
IMAGE=lojassimonetti/notificacoes
all:
docker build --pull . -t $(IMAGE)

push: all
docker push $(IMAGE)
7 changes: 4 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ services:
volumes:
- redis-data:/data
ports:
- 6389:6379
- 6389:${REDIS_PORT:-6379}

app:
image: node:8.7.0-alpine
working_dir: /app
command: ['/bin/sh', '-c', 'npm install && npm run build && npm run start:dev']
command:
['/bin/sh', '-c', 'npm install && npm run build && npm run start:dev']
volumes:
- .:/app
ports:
- 3000:3000
links:
- redis
depends_on:
- redis
- redis
43 changes: 31 additions & 12 deletions src/routes/pushNotification.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
import { Router } from "express";
import beamsClient from "../services/pushNotification";
import { Router } from 'express';

export default Router().get("/", function (req, res) {
try {
if (!beamsClient) throw "Browser Push API needs keys";
import beamsClient from '../services/pushNotification';

const userId = req.query["user_id"];
const beamsToken = beamsClient.generateToken(userId);
res.send(JSON.stringify(beamsToken));
} catch (err) {
const sendFail = (res, err) => {
console.error(err);
res.status(401).send("");
}
});
res.status(401).send('');
};

export default Router()
.get('/', function (req, res) {
console.log('hello', req.query);
try {
if (!beamsClient) throw 'Browser Push API needs keys';

const userId = req.query['user_id'];
const beamsToken = beamsClient.generateToken(userId);
res.send(JSON.stringify(beamsToken));
} catch (err) {
console.error(err);
res.status(401).send('');
}
})
.delete('/', function (req, res) {
console.log('good bye', req.query);
if (!beamsClient) return sendFail(res, 'Browser Push API needs keys');

if (!req.query['user_id']) return sendFail(res, 'Missing user_id');

beamsClient
.deleteUser(req.query['user_id'])
.then(() => res.status(204).send(''))
.catch((err) => sendFail(res, err));
});
36 changes: 21 additions & 15 deletions src/services/notification.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pusher from "./pusher";
import {sendNotification} from "./pushNotification";
import printf from "printf";
import pusher from './pusher';
import { sendNotification } from './pushNotification';
import printf from 'printf';

import {deleteEntry, getData, insertEntry, updateEntry} from "./redis";
import { deleteEntry, getData, insertEntry, updateEntry } from './redis';

const RESOURCE = 'notifications';

Expand All @@ -19,22 +19,29 @@ function dispatchNotification(message) {
}

export function addNotification(notification) {
let {login} = notification;
let { login } = notification;
login = new String(login || '').toLowerCase();

let now = new Date();

notification.criacao = printf(
"%d-%02d-%02d %d:%d:%d",
'%d-%02d-%02d %d:%d:%d',
now.getFullYear(),
1 + now.getMonth(),
now.getDate(),
now.getHours(),
now.getMinutes(),
now.getSeconds()
now.getSeconds(),
);

return insertEntry(RESOURCE, login, notification)
.then(notificacao => getQttyPending(login).then(pendente => ({login, pendente, notificacao})))
.then((notificacao) =>
getQttyPending(login).then((pendente) => ({
login,
pendente,
notificacao,
})),
)
.then(dispatchNotification);
}

Expand All @@ -51,13 +58,12 @@ export function getNotificationsByUser(login) {
}

export function deleteNotification(login, _$key) {
return deleteEntry(RESOURCE, login, {_$key});
return deleteEntry(RESOURCE, login, { _$key });
}

export function getQttyPending(login) {
return getData(RESOURCE, login)
.then(data => ({
qtde: data.length,
registros_sonoros: data.filter(entry => entry.sonoro).length
}));
}
return getData(RESOURCE, login).then((data) => ({
qtde: data.length,
registros_sonoros: data.filter((entry) => entry.sonoro).length,
}));
}

0 comments on commit c8dea17

Please sign in to comment.