0
votes

My users can post ads and later promote them with tokens which they buy. I have columns like pro_until, vip_until etc. for every type of promotion.

They are timestamps and are saved to the seconds. When a user promotes his ad to be vip for 3 days I save now()->addDays(3) to the vip_until column.

So what is the problem: My user needs to have a dashboard with notifications some of which will be notifications about the ad promotion expiration. Something like "22/03/2020 15:30:20 - Your VIP for ad 111 expired"

Here are the things I tried/thought about and how they worked out:

Cron jobs:

I can run a cron job every minute and check for expired columns $ad->vip_until->lt(now()).

Problem is: how do I notify about this expired ad only once ?

I can't check for an exact match between now() and {type}_until because the cron is beign executed every minute and I keep the {type}_until column to the exact seconds, so it is totally possible not to have a total match.

Delaying notifications:

I tried queueing the expiration notifications at the moment of creating the promotion https://laravel.com/docs/8.x/notifications#queueing-notifications.

I added the ->delay() callback

Implemented the ShouldQueue interface

Used the Queueable trait

Added a queue worker

but the notifications were pushed immediately.

    $delay = null;

    if ($request->type == 'pro') {
        $delay = $ad->promotePro($request->days);
    }
    if ($request->type == 'top') {
        $delay = $ad->promoteTop($request->days);
    }
    if ($request->type == 'vip') {
        $delay = $ad->promoteVip($request->days);
    }

    Auth::user()->payed -= $price;
    Auth::user()->save();

    if ($delay) {
        Auth::user()->notify((new AdPromotionEndedNotification($ad, $request->type))->delay($delay));
    }
1
This is a question that has a lot of possible solutions and isn't really fit for a FAQ format.Repox
"My user needs to have a dashboard with notifications some of which will be notifications about the ad promotion expiration." so you want to list the ad promotions expiration in some page. In this case you can take only the ads which will expire in next X days and to show them in this dashboard. If you want to send a email or other notification you can add a flag in your table which will mark that the notification is sent and you will not send it again.Emil Georgiev
@EmilGeorgiev I need only to show the ads after they have expired. The message I show is "Your VIP for ad XXXX has expired."Angel Miladinov
Are you sure ur env isn't set to sync ?PsyLogic

1 Answers

0
votes

I didn't get why do you need cron job to show a list of expired ads? Or you need something else like Facebook notifications with read/not read status?

If you want a notification like Facebook - then create a new table with notifications assigned to user and show that to the user