Skip to content

Commit

Permalink
Merge branch 'master' into HAN-113
Browse files Browse the repository at this point in the history
  • Loading branch information
fjpacheco committed May 6, 2024
2 parents 53eaedc + 5a77496 commit aaab3e0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 52 deletions.
103 changes: 52 additions & 51 deletions app/docker/tablas.sql
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
CREATE SCHEMA IF NOT EXISTS users_service;

CREATE TABLE IF NOT EXISTS users_service.users (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255) UNIQUE NOT NULL,
gender VARCHAR(20),
photo VARCHAR(255),
birthdate DATE,
location JSONB,
nickname VARCHAR(30),
biography VARCHAR(255),
device_token VARCHAR(255)
);

INSERT INTO
users_service.users (name, email, birthdate, location)
VALUES ('Agus', 'agus@fi.uba.ar', TO_DATE('1999-01-29', 'YYYY-MM-DD'), '{"lat": 20, "long": 100}'),
('Pach', 'pach@fi.uba.ar', TO_DATE('1999-08-06', 'YYYY-MM-DD'), '{"lat": 10, "long": 200}'),
('Sofi', 'sofi@fi.uba.ar', TO_DATE('1998-04-26', 'YYYY-MM-DD'), '{"lat": 1190, "long": 500}'),
('Violeta', 'violeta@fi.uba.ar', TO_DATE('1998-05-12', 'YYYY-MM-DD'), '{"lat": 330, "long": 2333}');


-- Para users con device_token
-- INSERT INTO
-- users_service.users (name, email, birthdate, location, device_token)
-- VALUES ('Agus', 'agus@fi.uba.ar', TO_DATE('1999-01-29', 'YYYY-MM-DD'), '{"lat": 20, "long": 100}', 'FEFEF'),
-- ('Pach', 'pach@fi.uba.ar', TO_DATE('1999-08-06', 'YYYY-MM-DD'), '{"lat": 10, "long": 200}', 'ASDASD'),
-- ('Sofi', 'sofi@fi.uba.ar', TO_DATE('1998-04-26', 'YYYY-MM-DD'), '{"lat": 1190, "long": 500}', 'QWEQWE'),
-- ('Violeta', 'violeta@fi.uba.ar', TO_DATE('1998-05-12', 'YYYY-MM-DD'), '{"lat": 330, "long": 2333}', 'ZXZXZX');

DROP TABLE IF EXISTS users_service.alarms CASCADE;

CREATE TABLE IF NOT EXISTS users_service.alarms (
id SERIAL PRIMARY KEY,
id_user INT NOT NULL REFERENCES users_service.users(id) ON DELETE CASCADE,
date_time TIMESTAMP WITH TIME ZONE NOT NULL,
content VARCHAR(128) NOT NULL
);
CREATE INDEX idx_alarms_date_time ON users_service.alarms(date_time);

INSERT INTO
users_service.alarms (id_user, date_time, content)
VALUES (1, '2024-04-30 00:50:00-03', 'Wake up 1!'),
(1, '2024-04-30 00:50:00-03', 'Wake up 2!'),
(1, '2024-04-30 00:50:00-03', 'Wake up 3!'),
(1, '2024-04-30 00:50:00-03', 'Wake up 4!');

-- SELECT users_service.alarms.id, users_service.alarms.content, users_service.users.device_token
-- FROM users_service.alarms
-- INNER JOIN users_service.users ON users_service.alarms.id_user = users_service.users.id
-- SQLBook: Code
CREATE SCHEMA IF NOT EXISTS users_service;

CREATE TABLE IF NOT EXISTS users_service.users (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255) UNIQUE NOT NULL,
gender VARCHAR(20),
photo VARCHAR(255),
birthdate DATE,
location JSONB,
nickname VARCHAR(30),
biography VARCHAR(255),
device_token VARCHAR(255)
);

INSERT INTO
users_service.users (name, email, birthdate, location)
VALUES ('Agus', 'agus@fi.uba.ar', TO_DATE('1999-01-29', 'YYYY-MM-DD'), '{"lat": 20, "long": 100}'),
('Pach', 'pach@fi.uba.ar', TO_DATE('1999-08-06', 'YYYY-MM-DD'), '{"lat": 10, "long": 200}'),
('Sofi', 'sofi@fi.uba.ar', TO_DATE('1998-04-26', 'YYYY-MM-DD'), '{"lat": 1190, "long": 500}'),
('Violeta', 'violeta@fi.uba.ar', TO_DATE('1998-05-12', 'YYYY-MM-DD'), '{"lat": 330, "long": 2333}');


-- Para users con device_token
-- INSERT INTO
-- users_service.users (name, email, birthdate, location, device_token)
-- VALUES ('Agus', 'agus@fi.uba.ar', TO_DATE('1999-01-29', 'YYYY-MM-DD'), '{"lat": 20, "long": 100}', 'FEFEF'),
-- ('Pach', 'pach@fi.uba.ar', TO_DATE('1999-08-06', 'YYYY-MM-DD'), '{"lat": 10, "long": 200}', 'ASDASD'),
-- ('Sofi', 'sofi@fi.uba.ar', TO_DATE('1998-04-26', 'YYYY-MM-DD'), '{"lat": 1190, "long": 500}', 'QWEQWE'),
-- ('Violeta', 'violeta@fi.uba.ar', TO_DATE('1998-05-12', 'YYYY-MM-DD'), '{"lat": 330, "long": 2333}', 'ZXZXZX');

DROP TABLE IF EXISTS users_service.alarms CASCADE;

CREATE TABLE IF NOT EXISTS users_service.alarms (
id SERIAL PRIMARY KEY,
id_user INT NOT NULL REFERENCES users_service.users(id) ON DELETE CASCADE,
date_time TIMESTAMP WITH TIME ZONE NOT NULL,
content VARCHAR(128) NOT NULL
);
CREATE INDEX idx_alarms_date_time ON users_service.alarms(date_time);

INSERT INTO
users_service.alarms (id_user, date_time, content)
VALUES (1, '2024-04-30 00:50:00-03', 'Wake up 1!'),
(1, '2024-04-30 00:50:00-03', 'Wake up 2!'),
(1, '2024-04-30 00:50:00-03', 'Wake up 3!'),
(1, '2024-04-30 00:50:00-03', 'Wake up 4!');

-- SELECT users_service.alarms.id, users_service.alarms.content, users_service.users.device_token
-- FROM users_service.alarms
-- INNER JOIN users_service.users ON users_service.alarms.id_user = users_service.users.id
-- WHERE users_service.alarms.datetime = '2024-04-29 02:15:00-03';
28 changes: 28 additions & 0 deletions app/repository/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,31 @@ def __parse_result(self, result):
if not result:
return []
return [r.__dict__ for r in result]

@withSQLExceptionsHandle()
def get_users_to_notify(self,
date_time: datetime) -> list[tuple[int,
str,
str]]:
"""
Retrieves a list of tuples containing the alarm id, content and the
device_token of users who have an alarm scheduled for the
given datetime.
Args:
date_time (datetime): The date and time in ISO 8601 format with
timezone specified. For example, "2021-10-05T10:00:00-03:00"
represents October 5th, 2021 at 10:00 AM in Argentina Time
Zone (UTC-3). Seconds and microseconds are ignored.
Returns:
List[Tuple[int, str, str]]: A list of tuples containing
the alarm id, the alarm content, and the user's device_token.
"""
date_time = date_time.replace(second=0, microsecond=0)
logger.info(f"Searching for alarms at {date_time}")
query = self.session.query(Alarm.id,
Alarm.content,
User.device_token)\
.join(User, User.id == Alarm.id_user)\
.filter(Alarm.datetime == date_time)
result = query.all()
return result
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ httpx
APScheduler>=3.10,<4.0
arq
pytz
python-jose
python-jose

0 comments on commit aaab3e0

Please sign in to comment.