Skip to content

Commit

Permalink
enh: rename to due date
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Ng <chrng8@gmail.com>
  • Loading branch information
Pytal committed Aug 3, 2023
1 parent beb8a27 commit fa77564
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 34 deletions.
Expand Up @@ -37,6 +37,7 @@ public function __construct(
private ReminderService $reminderService,
) {
parent::__construct($time);

$this->setInterval(60 * 60 * 24);
$this->setTimeSensitivity(IJob::TIME_INSENSITIVE);
}
Expand Down
Expand Up @@ -48,7 +48,7 @@ public function __construct(
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function run($argument) {
$reminders = $this->reminderMapper->findToRemind();
$reminders = $this->reminderMapper->findOverdue();
foreach ($reminders as $reminder) {
try {
$this->reminderService->send($reminder);
Expand Down
4 changes: 2 additions & 2 deletions apps/files_reminders/lib/Command/ListCommand.php
Expand Up @@ -75,12 +75,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$io->table(
['UserId', 'Path', 'RemindAt', 'CreatedAt', 'Notified'],
['UserId', 'Path', 'DueDate', 'CreatedAt', 'Notified'],
array_map(
fn (RichReminder $reminder) => [
$reminder->getUserId(),
$reminder->getNode()->getPath(),
$reminder->getRemindAt()->format(DateTimeInterface::ATOM), // ISO 8601
$reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601
$reminder->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601
$reminder->getNotified() ? 'true' : 'false',
],
Expand Down
12 changes: 6 additions & 6 deletions apps/files_reminders/lib/Controller/ApiController.php
Expand Up @@ -63,13 +63,13 @@ public function get(int $fileId): JSONResponse {
try {
$reminder = $this->reminderService->getDueForUser($user, $fileId);
$reminderData = [
'remindAt' => $reminder->getRemindAt()->format(DateTimeInterface::ATOM), // ISO 8601
'dueDate' => $reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601
];
return new JSONResponse($reminderData, Http::STATUS_OK);
} catch (DoesNotExistException $e) {
// Return null when no reminder is found
$reminderData = [
'remindAt' => null,
'dueDate' => null,
];
return new JSONResponse($reminderData, Http::STATUS_OK);
} catch (Throwable $th) {
Expand All @@ -81,11 +81,11 @@ public function get(int $fileId): JSONResponse {
/**
* Create a reminder
*
* @param string $remindAt ISO 8601 formatted date time string
* @param string $dueDate ISO 8601 formatted date time string
*/
public function create(int $fileId, string $remindAt): JSONResponse {
public function create(int $fileId, string $dueDate): JSONResponse {
try {
$remindAt = (new DateTime($remindAt))->setTimezone(new DateTimeZone('UTC'));
$dueDate = (new DateTime($dueDate))->setTimezone(new DateTimeZone('UTC'));
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return new JSONResponse([], Http::STATUS_BAD_REQUEST);
Expand All @@ -97,7 +97,7 @@ public function create(int $fileId, string $remindAt): JSONResponse {
}

try {
$this->reminderService->create($user, $fileId, $remindAt);
$this->reminderService->create($user, $fileId, $dueDate);
return new JSONResponse([], Http::STATUS_OK);
} catch (Throwable $th) {
$this->logger->error($th->getMessage(), ['exception' => $th]);
Expand Down
8 changes: 4 additions & 4 deletions apps/files_reminders/lib/Db/Reminder.php
Expand Up @@ -36,8 +36,8 @@
* @method void setFileId(int $fileId)
* @method int getFileId()
*
* @method void setRemindAt(DateTime $remindAt)
* @method DateTime getRemindAt()
* @method void setDueDate(DateTime $dueDate)
* @method DateTime getDueDate()
*
* @method void setCreatedAt(DateTime $createdAt)
* @method DateTime getCreatedAt()
Expand All @@ -48,14 +48,14 @@
class Reminder extends Entity {
protected $userId;
protected $fileId;
protected $remindAt;
protected $dueDate;
protected $createdAt;
protected $notified = false;

public function __construct() {
$this->addType('userId', 'string');
$this->addType('fileId', 'integer');
$this->addType('remindAt', 'datetime');
$this->addType('dueDate', 'datetime');
$this->addType('createdAt', 'datetime');
$this->addType('notified', 'boolean');
}
Expand Down
28 changes: 14 additions & 14 deletions apps/files_reminders/lib/Db/ReminderMapper.php
Expand Up @@ -50,13 +50,13 @@ public function markNotified(Reminder $reminder): Reminder {
$reminderUpdate = new Reminder();
$reminderUpdate->setId($reminder->getId());
$reminderUpdate->setNotified(true);
return parent::update($reminderUpdate);
return $this->update($reminderUpdate);
}

public function find(int $id): Reminder {
$qb = $this->db->getQueryBuilder();

$qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified')
$qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified')
->from($this->getTableName())
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));

Expand All @@ -69,7 +69,7 @@ public function find(int $id): Reminder {
public function findDueForUser(IUser $user, int $fileId): Reminder {
$qb = $this->db->getQueryBuilder();

$qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified')
$qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified')
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)))
Expand All @@ -86,9 +86,9 @@ public function findDueForUser(IUser $user, int $fileId): Reminder {
public function findAll() {
$qb = $this->db->getQueryBuilder();

$qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified')
$qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified')
->from($this->getTableName())
->orderBy('remind_at', 'ASC');
->orderBy('due_date', 'ASC');

return $this->findEntities($qb);
}
Expand All @@ -99,39 +99,39 @@ public function findAll() {
public function findAllForUser(IUser $user) {
$qb = $this->db->getQueryBuilder();

$qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified')
$qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified')
->from($this->getTableName())
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR)))
->orderBy('remind_at', 'ASC');
->orderBy('due_date', 'ASC');

return $this->findEntities($qb);
}

/**
* @return Reminder[]
*/
public function findToRemind() {
public function findOverdue() {
$qb = $this->db->getQueryBuilder();

$qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified')
$qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified')
->from($this->getTableName())
->where($qb->expr()->lt('remind_at', $qb->createFunction('NOW()')))
->where($qb->expr()->lt('due_date', $qb->createFunction('NOW()')))
->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)))
->orderBy('remind_at', 'ASC');
->orderBy('due_date', 'ASC');

return $this->findEntities($qb);
}

/**
* @return Reminder[]
*/
public function findToDelete(?int $limit = null) {
public function findNotified(?int $limit = null) {
$qb = $this->db->getQueryBuilder();

$qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified')
$qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified')
->from($this->getTableName())
->where($qb->expr()->eq('notified', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)))
->orderBy('remind_at', 'ASC')
->orderBy('due_date', 'ASC')
->setMaxResults($limit);

return $this->findEntities($qb);
Expand Down
Expand Up @@ -57,7 +57,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
'length' => 20,
'unsigned' => true,
]);
$table->addColumn('remind_at', Types::DATETIME, [
$table->addColumn('due_date', Types::DATETIME, [
'notnull' => true,
]);
$table->addColumn('created_at', Types::DATETIME, [
Expand All @@ -68,7 +68,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt
'default' => false,
]);
$table->setPrimaryKey(['id']);
$table->addUniqueIndex(['user_id', 'file_id', 'remind_at'], 'reminders_uniq_idx');
$table->addUniqueIndex(['user_id', 'file_id', 'due_date'], 'reminders_uniq_idx');

return $schema;
}
Expand Down
2 changes: 1 addition & 1 deletion apps/files_reminders/lib/Model/RichReminder.php
Expand Up @@ -66,7 +66,7 @@ public function jsonSerialize(): array {
return [
'userId' => $this->getUserId(),
'fileId' => $this->getFileId(),
'remindAt' => $this->getRemindAt()->format(DateTimeInterface::ATOM), // ISO 8601
'dueDate' => $this->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601
'createdAt' => $this->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601
'notified' => $this->getNotified(),
];
Expand Down
8 changes: 4 additions & 4 deletions apps/files_reminders/lib/Service/ReminderService.php
Expand Up @@ -78,11 +78,11 @@ public function getAll(?IUser $user = null) {
);
}

public function create(IUser $user, int $fileId, DateTime $remindAt): void {
public function create(IUser $user, int $fileId, DateTime $dueDate): void {
$reminder = new Reminder();
$reminder->setUserId($user->getUID());
$reminder->setFileId($fileId);
$reminder->setRemindAt($remindAt);
$reminder->setDueDate($dueDate);
$reminder->setCreatedAt(new DateTime('now', new DateTimeZone('UTC')));
$this->reminderMapper->insert($reminder);
}
Expand All @@ -108,7 +108,7 @@ public function send(Reminder $reminder): void {
->setUser($user->getUID())
->setObject('reminder', (string)$reminder->getId())
->setSubject('reminder-due')
->setDateTime($reminder->getRemindAt());
->setDateTime($reminder->getDueDate());

try {
$this->notificationManager->notify($notification);
Expand All @@ -119,7 +119,7 @@ public function send(Reminder $reminder): void {
}

public function cleanUp(?int $limit = null): void {
$reminders = $this->reminderMapper->findToDelete($limit);
$reminders = $this->reminderMapper->findNotified($limit);
foreach ($reminders as $reminder) {
$this->reminderMapper->delete($reminder);
}
Expand Down

0 comments on commit fa77564

Please sign in to comment.