|
Switch workspaces… (Jiminny Inc) Has new messages
Switch workspaces… (Jiminny Inc) Has new messages
Home
Home
DMs
DMs
Activity
Activity
Files
Files
Later
Later
More…
More
Unreads
Threads
Huddles
Drafts & sent
Directories
jiminny-x-integration-app
platform-inner-team
ai-chapter
ai-team
alerts
backend
c-learning-people
confusion-clinic
curiosity_lab
deal-insights-dev
engineering
frontend
general
infra-changes
jiminny-bg
people-with-copilot-licences
people-with-zoom-phone-licences
platform-team
platform-tickets
product_launches
random
releases
sofia-office
support
thank-yous
the_people_of_jiminny
Aneliya Angelova
,
Nikolay Yankov
,
Steliyan Georgiev
Stefka Stoyanova
Adelina Petrova
Vasil Vasilev
Stoyan Tomov
Galya Dimitrova
Nikolay Yankov
Petko Kashinski
Aneliya Angelova
Nikolay Nikolov
Mario Georgiev
Todor Stamatov
Gabriela Dureva
Jira Cloud
Toast
Messages
Messages
Add canvas
Add canvas
Files
Files
Add and Edit Channel Tabs
Canvas
List
Folder
Jump to date
Lukas Kovalik
Yesterday at 10:11:46 AM
10:11 AM
но реално ако искаме ретроспективно да пуснем нещо седмично което не е минало в сряда ще е от среяда до четвъртък
Aneliya Angelova
Yesterday at 10:13:21 AM
10:13 AM
Галя казва, че по скоро иска винаги да се пускат от понеделник до неделя, за да може да има бекъп вариант ако трябва по някаква причина ръчно да се рерънне седмичен репорт на клиент
Lukas Kovalik
Yesterday at 10:13:47 AM
10:13 AM
да, ами добре ще ги сменя, на старите репорти
(edited)
Aneliya Angelova
Yesterday at 10:14:00 AM
10:14 AM
оки
Yesterday at 10:15:09 AM
10:15
тайтъла на репорта и Data Source секцията ще показват правилния период нали - понеже това се разминаваше до сега
Lukas Kovalik
Yesterday at 10:47:31 AM
10:47 AM
сега забелязах че стария count който се ползва в при контрол на достъп canAccessAiReport брои само ресултати който са пратени, не тези които са генерирани
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Nikolay Yankov
Yesterday at 1:31:46 PM
1:31 PM
какво значи които са пратени?
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Yesterday at 1:32:17 PM
1:32
по принцип условието беше - ако за user-a има генерирано и може да го гледа на ai-reports страницата
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Lukas Kovalik
Yesterday at 1:35:32 PM
1:35 PM
да, говоря за нещо още от преди, count който се гледа дали има user право да гледа ai-reports страница , брои пратени не тези който са генерирани само. Реално ако се праща всичко на ред почти няма да се вижда разлика
(edited)
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Jump to date
New
Aneliya Angelova
Today at 11:13:41 AM
11:13 AM
Лукаш вчера генерирах 3 седмични репорта на. Галя на prod us. Обаче не ги пуснах по емейл и тази сутрин ги е получила по мейл.
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Adelina Petrova, Direct Message, 1 of 7 suggestions
Aneliya Angelova is typing
Firefox• 0FileEditViewHistory→BookmarksProfilesToolsWindowHelpmeet.google.com/agt-teir-cwt?authuser=lukas.kovalik%40jiminny.com•Daily - Platform - now100% K78 • Fri 24 Apr 9:46:13|=Pop out this videoNikolay NikolovStefka StoyanovaGalya DimitrovaLukas Kovalik9:46 AM | Daily - Platform• 0:27...
|
Slack
|
Aneliya Angelova, Nikolay Yankov, Steliyan Georgie Aneliya Angelova, Nikolay Yankov, Steliyan Georgiev (DM) - Jiminny Inc - 1 new item - Slack...
|
NULL
|
76655
|
|
Switch workspaces… (Jiminny Inc) Has new messages
Switch workspaces… (Jiminny Inc) Has new messages
Home
Home
DMs
DMs
Activity
Activity
Files
Files
Later
Later
More…
More
Unreads
Threads
Huddles
Drafts & sent
Directories
jiminny-x-integration-app
platform-inner-team
ai-chapter
ai-team
alerts
backend
c-learning-people
confusion-clinic
curiosity_lab
deal-insights-dev
engineering
frontend
general
infra-changes
jiminny-bg
people-with-copilot-licences
people-with-zoom-phone-licences
platform-team
platform-tickets
product_launches
random
releases
sofia-office
support
thank-yous
the_people_of_jiminny
Aneliya Angelova
,
Nikolay Yankov
,
Steliyan Georgiev
Stefka Stoyanova
Adelina Petrova
Vasil Vasilev
Stoyan Tomov
Galya Dimitrova
Nikolay Yankov
Petko Kashinski
Aneliya Angelova
Nikolay Nikolov
Mario Georgiev
Todor Stamatov
Gabriela Dureva
Jira Cloud
Toast
Messages
Messages
Add canvas
Add canvas
Files
Files
Add and Edit Channel Tabs
Canvas
List
Folder
Jump to date
Lukas Kovalik
Yesterday at 10:11:46 AM
10:11 AM
но реално ако искаме ретроспективно да пуснем нещо седмично което не е минало в сряда ще е от среяда до четвъртък
Aneliya Angelova
Yesterday at 10:13:21 AM
10:13 AM
Галя казва, че по скоро иска винаги да се пускат от понеделник до неделя, за да може да има бекъп вариант ако трябва по някаква причина ръчно да се рерънне седмичен репорт на клиент
Lukas Kovalik
Yesterday at 10:13:47 AM
10:13 AM
да, ами добре ще ги сменя, на старите репорти
(edited)
Aneliya Angelova
Yesterday at 10:14:00 AM
10:14 AM
оки
Yesterday at 10:15:09 AM
10:15
тайтъла на репорта и Data Source секцията ще показват правилния период нали - понеже това се разминаваше до сега
Lukas Kovalik
Yesterday at 10:47:31 AM
10:47 AM
сега забелязах че стария count който се ползва в при контрол на достъп canAccessAiReport брои само ресултати който са пратени, не тези които са генерирани
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
DMSActivityMorerireroxToolsHelpcalMistorbookmarksJiminny …..vXStarredi• jiminny-x-integrati..8 platform-inner-teamE) Channels# ai-chapter# ai-team# alerts# backend# c-learning-peoplei confusion-clinic# curiosity_labadeal-insichts-dev# engineering# frontend# general# infra-changes# jiminny-bg8 people-with-copilo...8 people-with-zoom-# platform-team# platform-tickets# product_launches# random# releases# sofia-office# support# thank-yous# the Deople of iimi...ProtllesWindow& platform-inner-...& 10MessagesChannel OverviewMoreYesterdayjinnylaop Aor 22nd Added by GitHubNikolay Ivanov 3:24 PMнякой нещо да е настроивал по githubactions. Почна да прави къмити вместо менбез да съм му разрешевал?https:/github.com/lminnv/app/pull/1200//changes/a68f42f210859f838a4fdced451f750627besoioИли нещо аз не разбирам?0AA0e 20 replies Last reply 18...Nikolay Yankov 3:50PMreplied to a uhread: някои нешо ла е насто..лол. ами предлагам маи ла му заораним лапускам към всички ла вилятNikolav Yankov 9.38 AMЩе се забавя за дейлито. Започнете без Мен.Aneliva Angelova 9:43 AMIДобро утро, няма да успея да вляза влейлито. Пествам ньлжовете.Message & platform-inner-team+ Aa I..•) New TabAl reports promotion pages by nik• JY-9712 | Nuges to expire after on8 Jiminnyu Userpilot Logged-activityJY-20157 add not enough activ XPipelines - jiminny/app+ New Tab©github.com/jimjiminny / app 8<> Code87 Pull requests 31( Agents |© Actions•• Wiki © Security and quality 32 ~ Insights 3 Settings@ On April 24 we'll start using GitHub Copilot interaction data for Al model training unless you opt out. Review this update and manage your preferences in your GitHub account settings.JY-20157 add not enough activities notification #12011 •$1 Open LakyLak wants to merge 2 commits into master from JY-20157-AJ-report-not-send-notification@) Conversation o• Commits 2|- Checks 21E Files changed 13A © All commits +Q Filter files...apo/Console/Commands/Reports/AutomatedReportsCommand.ohp@ -61,21 +61,29 @ public function handle(): intv = Console/Commands/Renorts|Snow = Carbon: : now();E AutomatedReportsCommand...v Jobs/AutomatedReportsE RequestGenerateAskJiminnyR...SendReportNotGeneratedMail...v @ Listeners/AutomatedReports/U….E TrackAutomatedReportGener...v # Mail/ReportsS1sMondav = Snow->1SMonday)S1sr1rstDayUtMonth = Snow->day === 1;ScurrentMonth = Snow->month.// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);$this->logger->info(self::LOG_PREFIX . ' Checking conditions', [+ ReportNotGenerated.ohp |"1SMonday' => S1SMonday,~ E Services/Kiosk/AutomatedRepo…..AskJiminnyReportActivityServ…'isFirstDay0fMonth' => SisFirstDay0fMonth,'currentMonth' => ScurrentMonth.E AutomatedReportsService.php'isQuarterlyMonth' => SisQuarterlyMonth,~E resources/views/emails/reportsreport-not-generated.blade.php/I Process dailv revortsl• F tests/UnitSthis->processReports(AutomatedReportsService::FREQUENCYDAILY):~ Jobs/AutomatedReportsE ReguestGenerateAsk JiminnvR....v = listeners/AutomatedRenorts/U.₴ TrackAutomatedReportGener..v E Services/Kiosk/AutomatedRepo…..E AskJiminnyReportActivityServ....AutomatedReportsServiceActi…./ Process weekly renorts on Mondavcif (SisMondav) {64 +67 +74 +86 +@40@ Daily - Platform - nowQ Type to search100% C4 8• Fri 24 Apr 9:46:13• Checks pending Code • (Preview) -+384 -52 9000C 0 I 13 viewedSubmit review+10 -2 mane [ Viewed0 ...Snow = Carhon:.nowdSisMondav = Snow->1SMonday)"Sisweekend = $now->isWeekend():SisFirstDay0fMonth = Snow->day === 1;ScurrentMonth = Snow->month.SisManualTrigger = $this->option('report-id') !== null;// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);Sthis->loager->info(self::L0G PREFIX . ' Checkina conditions'. [I"isMonday' => SisMonday,'isweekend' => $isWeekend,'isFirstDay0fMonth' => $isFirstDay0fMonth,'currentMonth' => ScurrentMonth.l'isQuarterlyMonth' => SisQuarterlyMonth,/ Process dailv renorts on weekdavs onlv (skio Saturdav/Sundav)...// Manual triggers via --report-id bypass the weekend skip.if (I Sisweekend || SisManualTriager) {Sthis->processReports(AutomatedReportsService::FREQUENCY_DAILY):} else {ISthis->logger->info(self::L0G PREFIX . ' Skipping daily reports on weekend'):/ Process weekly renorts on Mondavslif (SisMonday) {...
|
Slack
|
Aneliya Angelova, Nikolay Yankov, Steliyan Georgie Aneliya Angelova, Nikolay Yankov, Steliyan Georgiev (DM) - Jiminny Inc - 1 new item - Slack...
|
NULL
|
76656
|
|
Switch workspaces… (Jiminny Inc) Has new messages
Switch workspaces… (Jiminny Inc) Has new messages
Home
Home
DMs
DMs
Activity
Activity
Files
Files
Later
Later
More…
More
Unreads
Threads
Huddles
Drafts & sent
Directories
jiminny-x-integration-app
platform-inner-team
ai-chapter
ai-team
alerts
backend
c-learning-people
confusion-clinic
curiosity_lab
deal-insights-dev
engineering
frontend
general
infra-changes
jiminny-bg
people-with-copilot-licences
people-with-zoom-phone-licences
platform-team
platform-tickets
product_launches
random
releases
sofia-office
support
thank-yous
the_people_of_jiminny
Aneliya Angelova
,
Nikolay Yankov
,
Steliyan Georgiev
Stefka Stoyanova
Adelina Petrova
Vasil Vasilev
Stoyan Tomov
Galya Dimitrova
Nikolay Yankov
Petko Kashinski
Aneliya Angelova
Nikolay Nikolov
Mario Georgiev
Todor Stamatov
Gabriela Dureva
Jira Cloud
Toast
Messages
Messages
Add canvas
Add canvas
Files
Files
Add and Edit Channel Tabs
Canvas
List
Folder
Jump to date
Aneliya Angelova
Yesterday at 10:13:21 AM
10:13 AM
Галя казва, че по скоро иска винаги да се пускат от понеделник до неделя, за да може да има бекъп вариант ако трябва по някаква причина ръчно да се рерънне седмичен репорт на клиент
Lukas Kovalik
Yesterday at 10:13:47 AM
10:13 AM
да, ами добре ще ги сменя, на старите репорти
(edited)
Aneliya Angelova
Yesterday at 10:14:00 AM
10:14 AM
оки
Yesterday at 10:15:09 AM
10:15
тайтъла на репорта и Data Source секцията ще показват правилния период нали - понеже това се разминаваше до сега
Lukas Kovalik
Yesterday at 10:47:31 AM
10:47 AM
сега забелязах че стария count който се ползва в при контрол на достъп canAccessAiReport брои само ресултати който са пратени, не тези които са генерирани
Nikolay Yankov
Yesterday at 1:31:46 PM
1:31 PM
какво значи които са пратени?
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Yesterday at 1:32:17 PM
1:32
по принцип условието беше - ако за user-a има генерирано и може да го гледа на ai-reports страницата
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Lukas Kovalik
Yesterday at 1:35:32 PM
1:35 PM
да, говоря за нещо още от преди, count който се гледа дали има user право да гледа ai-reports страница , брои пратени не тези който са генерирани само. Реално ако се праща всичко на ред почти няма да се вижда разлика
(edited)
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Jump to date
New
Aneliya Angelova
Today at 11:13:41 AM
11:13 AM
Лукаш вчера генерирах 3 седмични репорта на. Галя на prod us. Обаче не ги пуснах по емейл и тази сутрин ги е получила по мейл.
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Lukas Kovalik
Today at 11:15:05 AM
11:15 AM
седмични не са ли в понеделник само
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Aneliya Angelova
is typing
Adelina Petrova, Direct Message, 1 of 7 suggestions
Aneliya Angelova is typing
Firefox• 0FileEditViewHistory→BookmarksProfilesToolsWindowHelpmeet.google.com/agt-teir-cwt?authuser=lukas.kovalik%40jiminny.com•Daily - Platform - now100% K78 • Fri 24 Apr 9:46:13|=Pop out this videoNikolay NikolovStefka StoyanovaGalya DimitrovaLukas Kovalik9:46 AM | Daily - Platform• 0:27...
|
Slack
|
Aneliya Angelova, Nikolay Yankov, Steliyan Georgie Aneliya Angelova, Nikolay Yankov, Steliyan Georgiev (DM) - Jiminny Inc - 1 new item - Slack...
|
NULL
|
76657
|
|
Switch workspaces… (Jiminny Inc) Has new messages
Switch workspaces… (Jiminny Inc) Has new messages
Home
Home
DMs
DMs
Activity
Activity
Files
Files
Later
Later
More…
More
Unreads
Threads
Huddles
Drafts & sent
Directories
jiminny-x-integration-app
platform-inner-team
ai-chapter
ai-team
alerts
backend
c-learning-people
confusion-clinic
curiosity_lab
deal-insights-dev
engineering
frontend
general
infra-changes
jiminny-bg
people-with-copilot-licences
people-with-zoom-phone-licences
platform-team
platform-tickets
product_launches
random
releases
sofia-office
support
thank-yous
the_people_of_jiminny
Aneliya Angelova
,
Nikolay Yankov
,
Steliyan Georgiev
Stefka Stoyanova
Adelina Petrova
Vasil Vasilev
Stoyan Tomov
Galya Dimitrova
Nikolay Yankov
Petko Kashinski
Aneliya Angelova
Nikolay Nikolov
Mario Georgiev
Todor Stamatov
Gabriela Dureva
Jira Cloud
Toast
Messages
Messages
Add canvas
Add canvas
Files
Files
Add and Edit Channel Tabs
Canvas
List
Folder
Jump to date
Aneliya Angelova
Yesterday at 10:13:21 AM
10:13 AM
Галя казва, че по скоро иска винаги да се пускат от понеделник до неделя, за да може да има бекъп вариант ако трябва по някаква причина ръчно да се рерънне седмичен репорт на клиент
Lukas Kovalik
Yesterday at 10:13:47 AM
10:13 AM
да, ами добре ще ги сменя, на старите репорти
(edited)
Aneliya Angelova
Yesterday at 10:14:00 AM
10:14 AM
оки
Yesterday at 10:15:09 AM
10:15
тайтъла на репорта и Data Source секцията ще показват правилния период нали - понеже това се разминаваше до сега
Lukas Kovalik
Yesterday at 10:47:31 AM
10:47 AM
сега забелязах че стария count който се ползва в при контрол на достъп canAccessAiReport брои само ресултати който са пратени, не тези които са генерирани
Nikolay Yankov
Yesterday at 1:31:46 PM
1:31 PM
какво значи които са пратени?
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Yesterday at 1:32:17 PM
1:32
по принцип условието беше - ако за user-a има генерирано и може да го гледа на ai-reports страницата
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Lukas Kovalik
Yesterday at 1:35:32 PM
1:35 PM
да, говоря за нещо още от преди, count който се гледа дали има user право да гледа ai-reports страница , брои пратени не тези който са генерирани само. Реално ако се праща всичко на ред почти няма да се вижда разлика
(edited)
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Jump to date
New
Aneliya Angelova
Today at 11:13:41 AM
11:13 AM
Лукаш вчера генерирах 3 седмични репорта на. Галя на prod us. Обаче не ги пуснах по емейл и тази сутрин ги е получила по мейл.
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Lukas Kovalik
Today at 11:15:05 AM
11:15 AM
седмични не са ли в понеделник само
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Aneliya Angelova
is typing
Adelina Petrova, Direct Message, 1 of 7 suggestions
Aneliya Angelova is typing
DMSActivityMorerireroxToolsHelpcalMistorbookmarksJiminny …..vXStarredi• jiminny-x-integrati..8 platform-inner-teamE) Channels# ai-chapter# ai-team# alerts# backend# c-learning-peoplei confusion-clinic# curiosity_labadeal-insichts-dev# engineering# frontend# general# infra-changes# jiminny-bg8 people-with-copilo...8 people-with-zoom-# platform-team# platform-tickets# product_launches# random# releases# sofia-office# support# thank-yous# the Deople of iimi...ProtllesWindow& platform-inner-...& 10MessagesChannel OverviewMoreYesterdayjinnylaop Aor 22nd Added by GitHubNikolay Ivanov 3:24 PMнякой нещо да е настроивал по githubactions. Почна да прави къмити вместо менбез да съм му разрешевал?https:/github.com/lminnv/app/pull/1200//changes/a68f42f210859f838a4fdced451f750627besoioИли нещо аз не разбирам?0AA0e 20 replies Last reply 18...Nikolay Yankov 3:50PMreplied to a uhread: някои нешо ла е насто..лол. ами предлагам маи ла му заораним лапускам към всички ла вилятNikolav Yankov 9.38 AMЩе се забавя за дейлито. Започнете без Мен.Aneliva Angelova 9:43 AMIДобро утро, няма да успея да вляза влейлито. Пествам ньлжовете.Message & platform-inner-team+ Aa I..•) New TabAl reports promotion pages by nik• JY-9712 | Nuges to expire after on8 Jiminnyu Userpilot Logged-activityJY-20157 add not enough activ XPipelines - jiminny/app+ New Tab©github.com/jimjiminny / app 8<> Code87 Pull requests 31( Agents |© Actions•• Wiki © Security and quality 32 ~ Insights 3 Settings@ On April 24 we'll start using GitHub Copilot interaction data for Al model training unless you opt out. Review this update and manage your preferences in your GitHub account settings.JY-20157 add not enough activities notification #12011 •$1 Open LakyLak wants to merge 2 commits into master from JY-20157-AJ-report-not-send-notification@) Conversation o• Commits 2|- Checks 21E Files changed 13A © All commits +Q Filter files...apo/Console/Commands/Reports/AutomatedReportsCommand.ohp@ -61,21 +61,29 @ public function handle(): intv = Console/Commands/Renorts|Snow = Carbon: : now();E AutomatedReportsCommand...v Jobs/AutomatedReportsE RequestGenerateAskJiminnyR...SendReportNotGeneratedMail...v @ Listeners/AutomatedReports/U….E TrackAutomatedReportGener...v # Mail/ReportsS1sMondav = Snow->1SMonday)S1sr1rstDayUtMonth = Snow->day === 1;ScurrentMonth = Snow->month.// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);$this->logger->info(self::LOG_PREFIX . ' Checking conditions', [+ ReportNotGenerated.ohp |"1SMonday' => S1SMonday,~ E Services/Kiosk/AutomatedRepo…..AskJiminnyReportActivityServ…'isFirstDay0fMonth' => SisFirstDay0fMonth,'currentMonth' => ScurrentMonth.E AutomatedReportsService.php'isQuarterlyMonth' => SisQuarterlyMonth,~E resources/views/emails/reportsreport-not-generated.blade.php/I Process dailv revortsl• F tests/UnitSthis->processReports(AutomatedReportsService::FREQUENCYDAILY):~ Jobs/AutomatedReportsE ReguestGenerateAsk JiminnvR....v = listeners/AutomatedRenorts/U.₴ TrackAutomatedReportGener..v E Services/Kiosk/AutomatedRepo…..E AskJiminnyReportActivityServ....AutomatedReportsServiceActi…./ Process weekly renorts on Mondavcif (SisMondav) {64 +67 +74 +86 +@40@ Daily - Platform - nowQ Type to search100% C4 8• Fri 24 Apr 9:46:13• Checks pending Code • (Preview) -+384 -52 9000C 0 I 13 viewedSubmit review+10 -2 mane [ Viewed0 ...Snow = Carhon:.nowdSisMondav = Snow->1SMonday)"Sisweekend = $now->isWeekend():SisFirstDay0fMonth = Snow->day === 1;ScurrentMonth = Snow->month.SisManualTrigger = $this->option('report-id') !== null;// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);Sthis->loager->info(self::L0G PREFIX . ' Checkina conditions'. [I"isMonday' => SisMonday,'isweekend' => $isWeekend,'isFirstDay0fMonth' => $isFirstDay0fMonth,'currentMonth' => ScurrentMonth.l'isQuarterlyMonth' => SisQuarterlyMonth,/ Process dailv renorts on weekdavs onlv (skio Saturdav/Sundav)...// Manual triggers via --report-id bypass the weekend skip.if (I Sisweekend || SisManualTriager) {Sthis->processReports(AutomatedReportsService::FREQUENCY_DAILY):} else {ISthis->logger->info(self::L0G PREFIX . ' Skipping daily reports on weekend'):/ Process weekly renorts on Mondavslif (SisMonday) {...
|
Slack
|
Aneliya Angelova, Nikolay Yankov, Steliyan Georgie Aneliya Angelova, Nikolay Yankov, Steliyan Georgiev (DM) - Jiminny Inc - 1 new item - Slack...
|
NULL
|
76658
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
13
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Http\Controllers\API\UserAutomatedReports;
use Illuminate\Support\Carbon;
use Illuminate\Http\JsonResponse;
use Jiminny\Exceptions\ApplicationException;
use Jiminny\Http\Controllers\Controller;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Models\User;
use Jiminny\Repositories\AutomatedReportsRepository;
use Jiminny\Services\ApiResponseService;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSort;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSortDirection;
use Illuminate\Http\Request;
use Throwable;
class UserAutomatedReportsController extends Controller
{
public const int RESULTS_PER_PAGE = 25;
public const string SORT_COLUMN = 'sort_column';
public const string SORT_DIRECTION = 'sort_direction';
public function __construct(
private readonly AutomatedReportsRepository $automatedReportsRepository,
private readonly AutomatedReportsService $automatedReportsService,
private readonly ApiResponseService $apiResponseService,
private readonly Response $response
) {
parent::__construct();
}
/**
* @throws ApplicationException
*/
public function list(Request $request): JsonResponse
{
/** @var User $user */
$user = $request->user();
$teamIds = $request->has('team')
? (array) $request->get('team')
: [];
$reportTypes = $request->has('report_type')
? (array) $request->get('report_type')
: [];
$name = $request->has('name') ? trim($request->get('name', '')) : null;
try {
$fromDate = $request->has('from_date') ? Carbon::parse($request->get('from_date')) : null;
$toDate = $request->has('to_date') ? Carbon::parse($request->get('to_date')) : null;
} catch (\Exception) {
return $this->response->errorWrongArgs('Invalid date.');
}
$page = $request->has('page') ? (int) $request->get('page') : 1;
$sort = ReportSort::tryFrom(
$request->get(self::SORT_COLUMN, '')
) ?? ReportSort::GENERATED_AT;
$sortDirection = ReportSortDirection::tryFrom(
strtolower($request->get(self::SORT_DIRECTION, ''))
) ?? ReportSortDirection::DESC;
$paginatedUserReports = $this->automatedReportsRepository->getPaginatedUserReports(
user: $user,
sort: $sort,
sortDirection: $sortDirection,
resultsPerPage: self::RESULTS_PER_PAGE,
page: $page,
fromDate: $fromDate,
toDate: $toDate,
teamIds: array_map('intval', $teamIds),
reportTypes: $reportTypes,
name: $name,
);
$reportResults = $this->automatedReportsService->transformReportResults(
$paginatedUserReports->getCollection()
);
$team = $user->getTeam();
$reportTypeFilter = $this->automatedReportsService->getReportTypeFieldData(
shortVersion: true,
team: $team
);
$data = $this->apiResponseService->fromPaginatorToArray(
paginator: $paginatedUserReports,
data: $reportResults,
moreMeta: [
self::SORT_COLUMN => $sort->value,
self::SORT_DIRECTION => $sortDirection->value,
],
filters: [
$reportTypeFilter['id'] => $reportTypeFilter,
],
);
return $this->response->withArray($data);
}
public function delete(Request $request, string $uuid): JsonResponse
{
/** @var User $user */
$user = $request->user();
try {
$result = $this->automatedReportsRepository->findResultByUuidForUser($uuid, $user);
if ($result === null) {
return new JsonResponse(
data: ['error' => 'Report not found'],
status: JsonResponse::HTTP_NOT_FOUND
);
}
$result->delete();
return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
} catch (Throwable $e) {
return new JsonResponse(
data: ['error' => 'Failed to delete report result'],
status: JsonResponse::HTTP_INTERNAL_SERVER_ERROR
);
}
}
}
Execute
Explain Plan
Browse Query History
View Parameters
Open Query Execution Settings…
In-Editor Results
Tx: Auto
Cancel Running Statements
Playground
jiminny
Sync Changes
Hide This Notification
Code changed:
Hide
37
1
35
63
Previous Highlighted Error
Next Highlighted Error
SELECT * FROM teams WHERE name LIKE '%litify%'; # 1069, 994, 24993
SELECT * FROM users WHERE id = 25061;
SELECT * FROM crm_profiles WHERE crm_configuration_id = 994;
SELECT * FROM crm_profiles WHERE user_id = 25061;
select * from crm_configurations where id = 834;
SELECT * FROM teams WHERE id = 882;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 882 and sa.provider = 'hubspot';
SELECT * FROM crm_configurations where id = 882; # 933 - GoGlobal
SELECT * FROM opportunities WHERE team_id = 933 order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 933 and sa.provider = 'hubspot';
SELECT * FROM crm_configurations WHERE provider = 'hubspot' and crm_provider_id = 7270388;
SELECT * FROM contacts where crm_configuration_id = 834;
SELECT * FROM opportunities WHERE team_id = 933
# AND crm_provider_id IN ('20131586060','46017317898','52543911090','53451356564','54101251892','54323768459');
AND id IN (8482561,18352941,19042734,19232139,19445140,19472541);
SELECT * FROM opportunity_contacts
WHERE opportunity_id IN (8482561,18352941,19042734,19232139,19445140,19472541);
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 485; #
SELECT * FROM opportunities WHERE team_id = 933 order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 933 and sa.provider = 'hubspot';
select crm.provider, l.* from leads l join crm_configurations crm on l.crm_configuration_id = crm.id
where crm.provider NOT IN ('salesforce', 'integration-app', 'bullhorn', 'copper')
# and l.converted_at IS NOT NULL
;
# [PASSWORD_DOTS]
SELECT * FROM activities a WHERE type IN ('email-inbound', 'email-outbound')
and opportunity_id IS NULL
order by id desc;
SELECT * FROM teams WHERE id = 604; # 598
SELECT * FROM activities WHERE id = 74410828; # [EMAIL]
SELECT * FROM accounts WHERE id = 20068382;
SELECT * FROM accounts WHERE id = 35186038;
SELECT * FROM contacts WHERE team_id = 852 and updated_at > '2026-01-23 12:30:00' order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 559 and sa.provider = 'hubspot';
SELECT * FROM activities WHERE uuid_to_bin('cb6342b6-a183-401c-b0af-ede92b2ae763') = uuid;
select * from sidekick_settings where team_id = 781;
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id = 26651871; # Teya
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id = 7562435;
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id = 8420347; # opflit 2100
SELECT * FROM crm_layouts WHERE crm_configuration_id = 711;
SELECT * FROM activities where crm_configuration_id = 711 and crm_provider_id IS NULL
and is_internal = 0 and status = 'completed'
order by id desc;
SELECT * FROM crm_layout_entities
WHERE crm_layout_id IN (2352, 2353);
;
SELECT * FROM crm_configurations where provider = 'hubspot' and id = 530;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 556 and sa.provider = 'hubspot';
SELECT * FROM activities WHERE uuid_to_bin('c6ca4b22-7738-4563-a95d-b8a9598924ae') = uuid;
SELECT * FROM activities WHERE uuid_to_bin('442abb2b-28bd-4be8-9c25-19e9bf02766d') = uuid;
select * from contacts
where crm_configuration_id = 530
and crm_provider_id = 872252;
select * from activities where crm_configuration_id = 530
and user_id = 14343 and type like '%softphone%'
and created_at between '2026-01-28 15:00:00' and '2026-01-28 15:10:00';
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id = 25666868; # Teya
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id = 8646335; # Teya
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id IN (5933397);
SELECT t.name, t.id, t.owner_id, c.id, c.provider, c.crm_base_url FROM teams t
JOIN crm_configurations c ON t.id = c.team_id
WHERE t.status = 'active';
SELECT * FROM teams where id = 1091;
SELECT * FROM crm_configurations where team_id = 1091;
SELECT * FROM activity_providers where team_id = 1091;
SELECT * FROM activities where crm_configuration_id = 1024 and type IN ('softphone', 'softphone-outbound')
and provider NOT IN ('hubspot', 'aircall')
# and telephony_provider_id = '019c1131-a22f-4792-b9ea-20adf6a02ed0'
order by id desc;
SELECT * FROM teams WHERE name LIKE '%Leadventure%';
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1091 and sa.provider = 'salesforce';
SELECT * FROM teams WHERE name LIKE '%Wilson%'; # 862, 812
SELECT * FROM teams where id = 862;
SELECT * FROM crm_configurations where team_id = 862;
SELECT * FROM activity_providers where team_id = 862;
SELECT * FROM activities where crm_configuration_id = 812 and type IN ('softphone', 'softphone-outbound')
and provider NOT IN ('hubspot', 'aircall')
# and telephony_provider_id = '019c1131-a22f-4792-b9ea-20adf6a02ed0'
order by id desc;
SELECT t.id, crm.id, crm.provider, ap.* FROM teams t
join crm_configurations crm on t.id = crm.team_id
join activity_providers ap on t.id = ap.team_id
where t.status = 'active' and ap.is_enabled = 1
and crm.provider = 'hubspot'
and ap.provider NOT IN ('hubspot', 'aircall', 'uploader', 'gong', 'twilio', 'zoom-bot', 'google-meet', 'ms-teams',
'outreach', 'close', 'ringcentral', 'dialpad', 'zoom-phone');
SELECT * FROM teams where id = 1068;
SELECT * FROM crm_configurations where team_id = 1068;
SELECT * FROM activity_providers where team_id = 1068;
SELECT * FROM activities a
where crm_configuration_id = 993 and type IN ('softphone', 'softphone-outbound')
and a.provider NOT IN ('hubspot', 'uploader', 'gong', 'twilio', 'google-meet', 'ms-teams','close'
)
# and telephony_provider_id = '019c1131-a22f-4792-b9ea-20adf6a02ed0'
order by a.id desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1068 and sa.provider = 'hubspot';
# [PASSWORD_DOTS]
# [PASSWORD_DOTS]
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 882; # 933 - GoGlobal , portalId: 6017093
SELECT * FROM opportunities WHERE team_id = 933 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 933 and updated_at > '2026-02-06 00:00:00' order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 933 and sa.provider = 'hubspot';
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 834; # 882 - AnyVan , portalId: 5468262
SELECT * FROM contacts WHERE crm_configuration_id = 834 and updated_at > '2026-03-30 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE crm_configuration_id = 834 and updated_at > '2026-03-04 08:00:00' order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 882 and sa.provider = 'hubspot';
select * from crm_layouts where crm_configuration_id = 834;
select * from crm_layout_entities where crm_layout_id = 2780;
select * from crm_fields where id IN (321153,321192,321193,321194);
SELECT * FROM opportunities WHERE crm_configuration_id = 834 and id = 10993426;
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 988; # 1057 - Teya (543ce4f4-168c-4571-91ea-5b35c253f06f) , portalId: 26651871
SELECT * FROM opportunities WHERE team_id = 1057 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1057 and updated_at > '2026-02-04 00:00:00' order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1057 and sa.provider = 'hubspot';
SELECT * FROM crm_configurations where id = 533; # 559 - Connectd , portalId: 6710988
SELECT * FROM opportunities WHERE team_id = 559 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 559 and updated_at > '2026-02-04 00:00:00' order by updated_at desc;
SELECT * FROM crm_configurations where id = 801; # 852 - Rise Vision , portalId: 2700250
SELECT * FROM opportunities WHERE team_id = 852 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 852 and updated_at > '2026-02-04 00:00:00' order by updated_at desc; # 6th last
SELECT * FROM crm_configurations where id = 962; # 1034 - evergrowth.io , portalId: 143180990
SELECT * FROM opportunities WHERE team_id = 1034 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1034 and updated_at > '2026-02-04 00:00:00' order by updated_at desc;
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 1037; # 1102 - Jibble , portalId: 6649755
SELECT * FROM opportunities WHERE team_id = 1102 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1102 and updated_at > '2026-02-04 12:30:00' order by updated_at desc; # 8
SELECT * FROM crm_configurations where id = 1015; # 1049 - Travefy , portalId: 48904401
SELECT * FROM opportunities WHERE team_id = 1049 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1049 and updated_at > '2026-02-04 12:30:00' order by updated_at desc; # 20
SELECT * FROM crm_configurations where id = 64; # 70 - SalaryFinance , portalId: 3404115
SELECT * FROM opportunities WHERE team_id = 70 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 70 and updated_at > '2026-02-04 12:30:00' order by updated_at desc; # 6th last
SELECT * FROM crm_configurations where id = 802; # 853 - Street Group , portalId: 7658438
SELECT * FROM opportunities WHERE team_id = 853 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 853 and updated_at > '2026-02-04 12:30:00' order by updated_at desc; # 10
SELECT * FROM crm_configurations where id = 872; # 921 - In Professional Development , portalId: 9238273
SELECT * FROM opportunities WHERE team_id = 921 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 921 and updated_at > '2026-02-04 12:30:00' order by updated_at desc; # 2
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 550; # 576 - SeedLegals , portalId: 3028661
SELECT * FROM opportunities WHERE team_id = 576 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 576 and updated_at > '2026-02-05 14:00:00' order by updated_at desc;
SELECT * FROM crm_configurations where id = 989; # 1058 - rtaoutdoor.com , portalId: 22371204
SELECT * FROM opportunities WHERE team_id = 1058 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1058 and updated_at > '2026-02-05 14:00:00' order by updated_at desc;
SELECT * FROM crm_configurations where id = 896; # 946 - Mintago , portalId: 6621281
SELECT * FROM opportunities WHERE team_id = 946 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 946 and updated_at > '2026-02-05 14:00:00' order by updated_at desc;
SELECT * FROM crm_configurations where id = 617; # 641 - PCS , portalId: 5244937
SELECT * FROM opportunities WHERE team_id = 641 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 641 and updated_at > '2026-02-05 14:00:00' order by updated_at desc; # 7th
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 649; # 670 - Eventeny , portalId: 4492849
SELECT * FROM opportunities WHERE team_id = 670 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 670 and updated_at > '2026-02-09 08:00:00' order by updated_at desc; #
SELECT * FROM crm_configurations where id = 48; # 51 - CleanCloud , portalId: 4373137
SELECT * FROM opportunities WHERE team_id = 51 and updated_at > '2026-03-04 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 51 and updated_at > '2026-02-09 08:00:00' order by updated_at desc;
select * from users where team_id = 51; # 7783
SELECT * FROM groups WHERE uuid_to_bin('8a8d2cb6-8b55-4fa3-8b5c-5f0e3d8de59a') = uuid; # 1130
select * from activity_searches where user_id = 7783;
select * from activity_search_filters where activity_search_id IN (32291, 32292);
SELECT asf.activity_search_id, asf.id, asf.value
FROM activity_search_filters asf
WHERE asf.filter = 'group_id'
AND asf.value IN (
SELECT CONCAT(
HEX(SUBSTR(uuid, 5, 4)), '-',
HEX(SUBSTR(uuid, 3, 2)), '-',
HEX(SUBSTR(uuid, 1, 2)), '-',
HEX(SUBSTR(uuid, 9, 2)), '-',
HEX(SUBSTR(uuid, 11))
)
FROM groups
WHERE deleted_at IS NOT NULL
);
SELECT * FROM crm_configurations where id = 272; # 290 - Bonham & Brook , portalId: 5705856
SELECT * FROM opportunities WHERE team_id = 290 and updated_at > '2026-02-05 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 290 and updated_at > '2026-02-09 08:00:00' order by updated_at desc; # 6th
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where provider = 'hubspot';
SELECT * FROM crm_configurations where id = 1056; # 1119 - Chromatic , portalId: 45602133
SELECT * FROM opportunities WHERE team_id = 1119 and remotely_created_at > '2026-02-01 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1119 and updated_at > '2026-02-09 09:00:00' order by updated_at desc; # null
# [PASSWORD_DOTS]
select * from contacts where crm_provider_id = '003Uu00000ojD4NIAU';
select
cp.*
# DISTINCT t.id
# cp.id, cp.user_id, t.id, cp.crm_configuration_id, cp.contact_fields
FROM crm_profiles cp
JOIN crm_configurations crm on crm.id = cp.crm_configuration_id
JOIN users u on u.id = cp.user_id
JOIN teams t ON t.id = crm.team_id
WHERE crm.provider = 'salesforce' and t.status = 'active'
and cp.archived_at IS NULL and u.deleted_at IS NULL
and t.id NOT IN (1093)
and t.id = 2
and cp.contact_fields IS NULL;
# and c.crm_provider_id = '003Uu00000ojD4NIAU';
SELECT * FROM users WHERE id = 26484;
SELECT * FROM crm_profiles WHERE user_id = 26484;
SELECT * FROM social_accounts WHERE sociable_id = 26484;
SELECT * FROM crm_configurations where provider = 'salesforce';
select * from users where id IN (10022, 10403);
select * from users where team_id IN (526);
select * from teams where id IN (526, 532);
select * from crm_configurations where id IN (500, 516);
select * from crm_profiles where crm_configuration_id IN (500, 516) and user_id IN (10022, 10403);
select * from contacts where crm_configuration_id IN (500, 516) and crm_provider_id = '003Uu00000ojD4NIAU';
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 526 and sa.provider = 'salesforce';
select * from team_settings where team_id IN (526, 532);
select * from users where id IN (22824);
select * from crm_profiles where crm_configuration_id IN (1026);
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1093 and sa.provider = 'salesforce';
select * from teams where id = 1099;
select * from users where id = 29643
select * from activity_processing_states;
SELECT * FROM teams where name LIKE '%Fare%'; # 233
SELECT * FROM opportunities where crm_configuration_id = 215
# and crm_provider_id = 'oppo_ogESZf2P50nDrd1nGPvKDXeA6sSaTN5v51Lp4ayVzKR'
;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1088 and sa.provider = 'hubspot';
SELECT * FROM teams order by updated_at DESC
SELECT * FROM crm_configurations WHERE id = 1019; # SimpleConsign 1088 - no social account
select * from crm_configurations where provider = 'pipedrive';
select * from teams where id = 957;
select * from crm_configurations where id = 957;
SELECT * FROM teams WHERE name LIKE '%Prolific%'; # 544, 518, 10743
SELECT * FROM opportunities where crm_configuration_id = 518 order by id desc;
select * from users where team_id = 1; # 26726 - Gabriela Dureva
SELECT * FROM opportunities where user_id = 26726; # 16834447 - Prolific
select * from activities where user_id = 26726 order by id desc;
select * from contacts where crm_configuration_id = 1
and email IN ('[EMAIL]', '[EMAIL]'); # 2094416, 2093620
SELECT * FROM contacts WHERE id = 6284931;
SELECT p.* FROM activities a JOIN participants p ON a.id = p.activity_id
WHERE a.user_id = 26726 and p.lead_id IN (2094416, 2093620) and a.created_at > '2026-01-01 00:00:00' order by p.email;
select * from activities where id IN (75509259,75509261,75509261,75511034,75026464,75517602,75517605);
select * from crm_configurations where id = 1;
43801692-1aeb-32ce-acba-5b80a479701a
44c3c9cf-6f5e-75f3-8179-bc9f75dd2b1b
405975c0-b3d0-7aaa-821f-09d59cae6dd1
4caf848d-4bed-2299-b248-7788d41f9fca
49bedc3f-f196-eef3-89c3-dea6a3b4aa63
43420989-a09d-b8f8-9806-c8bbf7a02aac
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1 and sa.provider = 'salesforce';
SELECT * FROM activities WHERE id = 75461988;
SELECT * FROM activities WHERE uuid_to_bin('d6c5052e-e972-49e9-8912-26f2f7d6c5f6') = uuid;
select * from contacts where id = 17900517;
select * from contact_roles cr join crm_configurations crm on cr.crm_configuration_id = crm.id
where crm.provider != 'salesforce';
select * from users where id = 21047;
SELECT * FROM crm_configurations WHERE id = 892;
SELECT * FROM teams WHERE id = 942;
select * from opportunities where team_id = 942 order by updated_at desc;
select * from contacts where team_id = 942 order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 942 and sa.provider = 'hubspot';
SELECT * FROM opportunities where team_id = 1 and crm_provider_id IN ('006Pq00000NeH6XIAV', '006Pq000007z8kdIAA'); # 10697889, 6621430
SELECT * FROM crm_configurations WHERE id = 1;
SELECT * FROM teams WHERE crm_id = 1;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1 and sa.provider = 'salesforce';
select id, user_id, opportunity_fields from crm_profiles where crm_configuration_id = 1
SELECT * FROM opportunities where team_id = 1 order by updated_at desc; # 10697889, 6621430
select * from teams where id = 852;
select * from groups where id = 2286;
select * from sidekick_settings where team_id = 852;
select * from default_activity_types where team_id = 852;
SELECT cc.provider, cc.id, p.id, u.*
FROM users u
LEFT JOIN crm_profiles p ON u.id = p.user_id AND p.id IS NULL -- no profile
INNER JOIN teams t ON u.team_id = t.id AND t.status = 'active' -- team is active
INNER JOIN crm_configurations cc ON t.crm_id = cc.id
WHERE u.status = 1 AND u.deleted_at IS NULL
AND u.crm_required = 1
AND u.team_id = 1
ORDER BY u.team_id;
SELECT * FROM crm_profiles cp where cp.crm_configuration_id = 1 and cp.user_id IN (
18481
);
SELECT cc.provider, cc.id, p.id, u.*
FROM users u
LEFT JOIN crm_profiles p ON u.id = p.user_id
INNER JOIN teams t ON u.team_id = t.id AND t.status = 'active'
INNER JOIN crm_configurations cc ON t.crm_id = cc.id
WHERE u.status = 1
AND u.deleted_at IS NULL
AND u.crm_required = 1
# AND u.team_id = 1
AND p.id IS NULL -- Move this condition to WHERE clause
ORDER BY u.team_id;
SELECT * FROM opportunities WHERE id = 20002609;
select * from teams where id = 1122; # Velatir, 29953 - [EMAIL]
select * from crm_configurations where id = 1060;
select * from crm_layouts where crm_configuration_id = 1060;
SELECT * FROM crm_layout_entities WHERE crm_layout_id = 3596;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1122 and sa.provider = 'hubspot';
select * from opportunities where team_id = 1122 order by updated_at desc;
select * from crm_field_data where object_type = 'contact';
SELECT * FROM activities WHERE uuid_to_bin('374fc8ed-3315-4c9f-9b25-318b7fd2928f') = uuid; # 76584262
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 248 and sa.provider = 'salesforce';
SELECT * FROM crm_profiles where user_id = 24115; # 005QF000002CswMYAS
SELECT * FROM users where id = 24115;
SELECT * FROM accounts where id = 4002896;
SELECT * FROM teams WHERE name LIKE '%adswerve%';
SELECT * FROM opportunities where crm_configuration_id = 230 AND crm_provider_id IN ("0069N000003GIQ9QAO","0061r000019yGP9AAM","0066900001S2KWlAAN","0066900001TDpj2AAD","0066900001b8uEwAAI","0069N000001rQi0QAE","006QF00000KD40mYAD","006QF00000LzpRJYAZ","0069N000002uomtQAA","0069N000002xlMLQAY","0066900001NV6ubAAD","0061r00001HJp45AAD","006QF00000uTlUoYAK","006QF00000v0bZqYAI");
SELECT * FROM opportunities WHERE crm_configuration_id = 230 AND crm_provider_id = '0069N000003GIQ9QAO'; # 6272203
SELECT u.id, u.email, ac.name, a.* FROM activities a
JOIN users u ON a.user_id = u.id
JOIN accounts ac ON a.account_id = ac.id
WHERE
uuid_to_bin('e3269598-b562-44fb-b5e9-9d2694dc63e0') = a.uuid or
uuid_to_bin('66ddc3ab-4e15-45aa-af0c-248c1eece593') = a.uuid or
uuid_to_bin('826bd328-e1cc-4213-b8d8-572454cacc07') = a.uuid;
select * from users where id = 5825;
SELECT * FROM activities WHERE uuid_to_bin('e56aa2e8-231a-421b-ab1f-cb38ed2bf573') = uuid;
select * from activities where uuid_to_bin('91e13b2f-2d1b-45f8-b1fd-1141b6563782') = uuid;
19594, 862
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 862 and sa.provider = 'salesforce';
select * from automated_reports where id = 36;
select ar.frequency, r.*, ar.* from automated_report_results r
join automated_reports ar on r.report_id = ar.id
where ar.frequency != 'one_off';
select s.* from activity_searches s join users u ON s.user_id = u.id where u.team_id = 882;
select * from nudges n where n.activity_search_id
select * from teams where created_at > '2026-03-09';
SELECT * FROM crm_layouts WHERE crm_configuration_id = 1065; # 1065
SELECT * FROM crm_layout_entities WHERE crm_layout_id = 3617;
select * from users where team_id = 1 and name like '%Lukas%'; # 7160
SELECT * FROM teams WHERE id = 575;
select * from opportunities where team_id = 575;
SELECT * FROM teams WHERE name LIKE '%Integrum ESG%'; # 1126, 1065,
select * from opportunities where team_id = 1126;
SELECT * FROM teams WHERE name LIKE '%Base%'; # 1125, 1063,
select * from opportunities where team_id = 1125;
select * from contacts c
where c.team_id = 882;
SELECT * FROM activities WHERE id = 76822967;
SELECT * FROM crm_profiles WHERE user_id = 15440;
SELECT * FROM crm_profiles WHERE crm_configuration_id = 555;
SELECT * FROM crm_configurations WHERE id = 555;
SELECT * FROM users WHERE id = 15440; # team. 581, gr. 15440, pl. 3911, act. field 162182
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 581 and sa.provider = 'salesforce';
SELECT * FROM automated_report_results order by id desc;
select * from features;
select * from team_features where feature_id = 40;
select * from teams where id = 556;
select * from automated_reports;
where id = 54; # 4fdd41f6-dcf0-30d0-b339-7345381b6044 , ["pdf","podcast"]
SELECT * FROM automated_report_results WHERE uuid_to_bin('822fa41b-afd3-43a9-a248-86b0e36f3131') = uuid;
select * from automated_report_results order by id desc;
SELECT * FROM automated_report_results WHERE id = 1919;
select * from automated_report_results WHERE report_id = 54;
select * from opportunities where id = 7594349;
SELECT * FROM teams WHERE name LIKE '%Les%'; # 711, 692, 16067 - [EMAIL]
select * from playbooks where team_id = 711; # event 226147
SELECT * FROM playbook_categories WHERE playbook_id = 5515;
SELECT * FROM crm_fields WHERE crm_configuration_id = 692 and object_type = 'event';
SELECT * FROM crm_fields WHERE id = 226147;
SELECT * FROM crm_field_values WHERE crm_field_id = 226147;
SELECT * FROM crm_configurations WHERE id = 692;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 711 and sa.provider = 'salesforce';
SELECT * FROM crm_profiles cp JOIN users u on u.id = cp.user_id WHERE u.team_id = 711;
select * from leads;
select * from calendars;
SELECT
t.id AS team_id,
t.name,
LOWER(SUBSTRING_INDEX(c.calendar_provider_id, '@', -1)) AS calendar_domain
FROM teams t
JOIN users u ON u.team_id = t.id
JOIN calendars c ON c.user_id = u.id AND c.status = 'active' AND c.calendar_provider_id LIKE '%@%'
LEFT JOIN team_domains td
ON td.team_id = t.id
AND td.deleted_at IS NULL
AND td.domain = LOWER(SUBSTRING_INDEX(c.calendar_provider_id, '@', -1))
GROUP BY t.id, t.name, calendar_domain
ORDER BY t.name, calendar_domain;
select * from users u join calendars c on c.user_id = u.id
where u.team_id = 882;
select * from activities where id = 74049485; # team 563 crm 537
select * from activities where id = 73272382; # team 563 crm 537
select * from activities where id = 64400389; # team 563 crm 537
select * from activities where id = 58081273; # team 563 crm 537
select * from activities where id = 54520297; # team 563 crm 537
select * from participants where activity_id = 58081273;
select * from activities where crm_configuration_id = 537 and provider = 'aircall'
and account_id = 19003658 order by updated_at desc;
select * from contacts where crm_configuration_id = 537 and id = 35957759;
select * from accounts where crm_configuration_id = 537 and id = 19003658;
select * from automated_report_results where id = 1976;
select * from automated_reports where id = 583;
select * from activity_searches where id = 87714;
select * from activity_search_filters where activity_search_id = 87714;
SELECT * FROM activities WHERE uuid_to_bin('8827f672-202d-4162-9d04-73ff5f0566a9') = uuid
or uuid_to_bin('47842446-af51-4bcb-854f-cc6560290101') = uuid;
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – UserAutomatedReportsController.php
|
NULL
|
76659
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
13
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Http\Controllers\API\UserAutomatedReports;
use Illuminate\Support\Carbon;
use Illuminate\Http\JsonResponse;
use Jiminny\Exceptions\ApplicationException;
use Jiminny\Http\Controllers\Controller;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Models\User;
use Jiminny\Repositories\AutomatedReportsRepository;
use Jiminny\Services\ApiResponseService;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSort;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSortDirection;
use Illuminate\Http\Request;
use Throwable;
class UserAutomatedReportsController extends Controller
{
public const int RESULTS_PER_PAGE = 25;
public const string SORT_COLUMN = 'sort_column';
public const string SORT_DIRECTION = 'sort_direction';
public function __construct(
private readonly AutomatedReportsRepository $automatedReportsRepository,
private readonly AutomatedReportsService $automatedReportsService,
private readonly ApiResponseService $apiResponseService,
private readonly Response $response
) {
parent::__construct();
}
/**
* @throws ApplicationException
*/
public function list(Request $request): JsonResponse
{
/** @var User $user */
$user = $request->user();
$teamIds = $request->has('team')
? (array) $request->get('team')
: [];
$reportTypes = $request->has('report_type')
? (array) $request->get('report_type')
: [];
$name = $request->has('name') ? trim($request->get('name', '')) : null;
try {
$fromDate = $request->has('from_date') ? Carbon::parse($request->get('from_date')) : null;
$toDate = $request->has('to_date') ? Carbon::parse($request->get('to_date')) : null;
} catch (\Exception) {
return $this->response->errorWrongArgs('Invalid date.');
}
$page = $request->has('page') ? (int) $request->get('page') : 1;
$sort = ReportSort::tryFrom(
$request->get(self::SORT_COLUMN, '')
) ?? ReportSort::GENERATED_AT;
$sortDirection = ReportSortDirection::tryFrom(
strtolower($request->get(self::SORT_DIRECTION, ''))
) ?? ReportSortDirection::DESC;
$paginatedUserReports = $this->automatedReportsRepository->getPaginatedUserReports(
user: $user,
sort: $sort,
sortDirection: $sortDirection,
resultsPerPage: self::RESULTS_PER_PAGE,
page: $page,
fromDate: $fromDate,
toDate: $toDate,
teamIds: array_map('intval', $teamIds),
reportTypes: $reportTypes,
name: $name,
);
$reportResults = $this->automatedReportsService->transformReportResults(
$paginatedUserReports->getCollection()
);
$team = $user->getTeam();
$reportTypeFilter = $this->automatedReportsService->getReportTypeFieldData(
shortVersion: true,
team: $team
);
$data = $this->apiResponseService->fromPaginatorToArray(
paginator: $paginatedUserReports,
data: $reportResults,
moreMeta: [
self::SORT_COLUMN => $sort->value,
self::SORT_DIRECTION => $sortDirection->value,
],
filters: [
$reportTypeFilter['id'] => $reportTypeFilter,
],
);
return $this->response->withArray($data);
}
public function delete(Request $request, string $uuid): JsonResponse
{
/** @var User $user */
$user = $request->user();
try {
$result = $this->automatedReportsRepository->findResultByUuidForUser($uuid, $user);
if ($result === null) {
return new JsonResponse(
data: ['error' => 'Report not found'],
status: JsonResponse::HTTP_NOT_FOUND
);
}
$result->delete();
return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
} catch (Throwable $e) {
return new JsonResponse(
data: ['error' => 'Failed to delete report result'],
status: JsonResponse::HTTP_INTERNAL_SERVER_ERROR
);
}
}
}
Execute
Explain Plan
Browse Query History
View Parameters
Open Query Execution Settings…
In-Editor Results
Tx: Auto
Cancel Running Statements
Playground
jiminny
Sync Changes
Hide This Notification
Code changed:
Hide
37
1
35
63
Previous Highlighted Error
Next Highlighted Error
SELECT * FROM teams WHERE name LIKE '%litify%'; # 1069, 994, 24993
SELECT * FROM users WHERE id = 25061;
SELECT * FROM crm_profiles WHERE crm_configuration_id = 994;
SELECT * FROM crm_profiles WHERE user_id = 25061;
select * from crm_configurations where id = 834;
SELECT * FROM teams WHERE id = 882;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 882 and sa.provider = 'hubspot';
SELECT * FROM crm_configurations where id = 882; # 933 - GoGlobal
SELECT * FROM opportunities WHERE team_id = 933 order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 933 and sa.provider = 'hubspot';
SELECT * FROM crm_configurations WHERE provider = 'hubspot' and crm_provider_id = 7270388;
SELECT * FROM contacts where crm_configuration_id = 834;
SELECT * FROM opportunities WHERE team_id = 933
# AND crm_provider_id IN ('20131586060','46017317898','52543911090','53451356564','54101251892','54323768459');
AND id IN (8482561,18352941,19042734,19232139,19445140,19472541);
SELECT * FROM opportunity_contacts
WHERE opportunity_id IN (8482561,18352941,19042734,19232139,19445140,19472541);
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 485; #
SELECT * FROM opportunities WHERE team_id = 933 order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 933 and sa.provider = 'hubspot';
select crm.provider, l.* from leads l join crm_configurations crm on l.crm_configuration_id = crm.id
where crm.provider NOT IN ('salesforce', 'integration-app', 'bullhorn', 'copper')
# and l.converted_at IS NOT NULL
;
# [PASSWORD_DOTS]
SELECT * FROM activities a WHERE type IN ('email-inbound', 'email-outbound')
and opportunity_id IS NULL
order by id desc;
SELECT * FROM teams WHERE id = 604; # 598
SELECT * FROM activities WHERE id = 74410828; # [EMAIL]
SELECT * FROM accounts WHERE id = 20068382;
SELECT * FROM accounts WHERE id = 35186038;
SELECT * FROM contacts WHERE team_id = 852 and updated_at > '2026-01-23 12:30:00' order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 559 and sa.provider = 'hubspot';
SELECT * FROM activities WHERE uuid_to_bin('cb6342b6-a183-401c-b0af-ede92b2ae763') = uuid;
select * from sidekick_settings where team_id = 781;
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id = 26651871; # Teya
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id = 7562435;
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id = 8420347; # opflit 2100
SELECT * FROM crm_layouts WHERE crm_configuration_id = 711;
SELECT * FROM activities where crm_configuration_id = 711 and crm_provider_id IS NULL
and is_internal = 0 and status = 'completed'
order by id desc;
SELECT * FROM crm_layout_entities
WHERE crm_layout_id IN (2352, 2353);
;
SELECT * FROM crm_configurations where provider = 'hubspot' and id = 530;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 556 and sa.provider = 'hubspot';
SELECT * FROM activities WHERE uuid_to_bin('c6ca4b22-7738-4563-a95d-b8a9598924ae') = uuid;
SELECT * FROM activities WHERE uuid_to_bin('442abb2b-28bd-4be8-9c25-19e9bf02766d') = uuid;
select * from contacts
where crm_configuration_id = 530
and crm_provider_id = 872252;
select * from activities where crm_configuration_id = 530
and user_id = 14343 and type like '%softphone%'
and created_at between '2026-01-28 15:00:00' and '2026-01-28 15:10:00';
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id = 25666868; # Teya
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id = 8646335; # Teya
SELECT * FROM crm_configurations where provider = 'hubspot' and crm_provider_id IN (5933397);
SELECT t.name, t.id, t.owner_id, c.id, c.provider, c.crm_base_url FROM teams t
JOIN crm_configurations c ON t.id = c.team_id
WHERE t.status = 'active';
SELECT * FROM teams where id = 1091;
SELECT * FROM crm_configurations where team_id = 1091;
SELECT * FROM activity_providers where team_id = 1091;
SELECT * FROM activities where crm_configuration_id = 1024 and type IN ('softphone', 'softphone-outbound')
and provider NOT IN ('hubspot', 'aircall')
# and telephony_provider_id = '019c1131-a22f-4792-b9ea-20adf6a02ed0'
order by id desc;
SELECT * FROM teams WHERE name LIKE '%Leadventure%';
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1091 and sa.provider = 'salesforce';
SELECT * FROM teams WHERE name LIKE '%Wilson%'; # 862, 812
SELECT * FROM teams where id = 862;
SELECT * FROM crm_configurations where team_id = 862;
SELECT * FROM activity_providers where team_id = 862;
SELECT * FROM activities where crm_configuration_id = 812 and type IN ('softphone', 'softphone-outbound')
and provider NOT IN ('hubspot', 'aircall')
# and telephony_provider_id = '019c1131-a22f-4792-b9ea-20adf6a02ed0'
order by id desc;
SELECT t.id, crm.id, crm.provider, ap.* FROM teams t
join crm_configurations crm on t.id = crm.team_id
join activity_providers ap on t.id = ap.team_id
where t.status = 'active' and ap.is_enabled = 1
and crm.provider = 'hubspot'
and ap.provider NOT IN ('hubspot', 'aircall', 'uploader', 'gong', 'twilio', 'zoom-bot', 'google-meet', 'ms-teams',
'outreach', 'close', 'ringcentral', 'dialpad', 'zoom-phone');
SELECT * FROM teams where id = 1068;
SELECT * FROM crm_configurations where team_id = 1068;
SELECT * FROM activity_providers where team_id = 1068;
SELECT * FROM activities a
where crm_configuration_id = 993 and type IN ('softphone', 'softphone-outbound')
and a.provider NOT IN ('hubspot', 'uploader', 'gong', 'twilio', 'google-meet', 'ms-teams','close'
)
# and telephony_provider_id = '019c1131-a22f-4792-b9ea-20adf6a02ed0'
order by a.id desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1068 and sa.provider = 'hubspot';
# [PASSWORD_DOTS]
# [PASSWORD_DOTS]
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 882; # 933 - GoGlobal , portalId: 6017093
SELECT * FROM opportunities WHERE team_id = 933 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 933 and updated_at > '2026-02-06 00:00:00' order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 933 and sa.provider = 'hubspot';
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 834; # 882 - AnyVan , portalId: 5468262
SELECT * FROM contacts WHERE crm_configuration_id = 834 and updated_at > '2026-03-30 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE crm_configuration_id = 834 and updated_at > '2026-03-04 08:00:00' order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 882 and sa.provider = 'hubspot';
select * from crm_layouts where crm_configuration_id = 834;
select * from crm_layout_entities where crm_layout_id = 2780;
select * from crm_fields where id IN (321153,321192,321193,321194);
SELECT * FROM opportunities WHERE crm_configuration_id = 834 and id = 10993426;
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 988; # 1057 - Teya (543ce4f4-168c-4571-91ea-5b35c253f06f) , portalId: 26651871
SELECT * FROM opportunities WHERE team_id = 1057 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1057 and updated_at > '2026-02-04 00:00:00' order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1057 and sa.provider = 'hubspot';
SELECT * FROM crm_configurations where id = 533; # 559 - Connectd , portalId: 6710988
SELECT * FROM opportunities WHERE team_id = 559 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 559 and updated_at > '2026-02-04 00:00:00' order by updated_at desc;
SELECT * FROM crm_configurations where id = 801; # 852 - Rise Vision , portalId: 2700250
SELECT * FROM opportunities WHERE team_id = 852 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 852 and updated_at > '2026-02-04 00:00:00' order by updated_at desc; # 6th last
SELECT * FROM crm_configurations where id = 962; # 1034 - evergrowth.io , portalId: 143180990
SELECT * FROM opportunities WHERE team_id = 1034 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1034 and updated_at > '2026-02-04 00:00:00' order by updated_at desc;
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 1037; # 1102 - Jibble , portalId: 6649755
SELECT * FROM opportunities WHERE team_id = 1102 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1102 and updated_at > '2026-02-04 12:30:00' order by updated_at desc; # 8
SELECT * FROM crm_configurations where id = 1015; # 1049 - Travefy , portalId: 48904401
SELECT * FROM opportunities WHERE team_id = 1049 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1049 and updated_at > '2026-02-04 12:30:00' order by updated_at desc; # 20
SELECT * FROM crm_configurations where id = 64; # 70 - SalaryFinance , portalId: 3404115
SELECT * FROM opportunities WHERE team_id = 70 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 70 and updated_at > '2026-02-04 12:30:00' order by updated_at desc; # 6th last
SELECT * FROM crm_configurations where id = 802; # 853 - Street Group , portalId: 7658438
SELECT * FROM opportunities WHERE team_id = 853 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 853 and updated_at > '2026-02-04 12:30:00' order by updated_at desc; # 10
SELECT * FROM crm_configurations where id = 872; # 921 - In Professional Development , portalId: 9238273
SELECT * FROM opportunities WHERE team_id = 921 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 921 and updated_at > '2026-02-04 12:30:00' order by updated_at desc; # 2
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 550; # 576 - SeedLegals , portalId: 3028661
SELECT * FROM opportunities WHERE team_id = 576 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 576 and updated_at > '2026-02-05 14:00:00' order by updated_at desc;
SELECT * FROM crm_configurations where id = 989; # 1058 - rtaoutdoor.com , portalId: 22371204
SELECT * FROM opportunities WHERE team_id = 1058 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1058 and updated_at > '2026-02-05 14:00:00' order by updated_at desc;
SELECT * FROM crm_configurations where id = 896; # 946 - Mintago , portalId: 6621281
SELECT * FROM opportunities WHERE team_id = 946 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 946 and updated_at > '2026-02-05 14:00:00' order by updated_at desc;
SELECT * FROM crm_configurations where id = 617; # 641 - PCS , portalId: 5244937
SELECT * FROM opportunities WHERE team_id = 641 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 641 and updated_at > '2026-02-05 14:00:00' order by updated_at desc; # 7th
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where id = 649; # 670 - Eventeny , portalId: 4492849
SELECT * FROM opportunities WHERE team_id = 670 and updated_at > '2026-02-18 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 670 and updated_at > '2026-02-09 08:00:00' order by updated_at desc; #
SELECT * FROM crm_configurations where id = 48; # 51 - CleanCloud , portalId: 4373137
SELECT * FROM opportunities WHERE team_id = 51 and updated_at > '2026-03-04 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 51 and updated_at > '2026-02-09 08:00:00' order by updated_at desc;
select * from users where team_id = 51; # 7783
SELECT * FROM groups WHERE uuid_to_bin('8a8d2cb6-8b55-4fa3-8b5c-5f0e3d8de59a') = uuid; # 1130
select * from activity_searches where user_id = 7783;
select * from activity_search_filters where activity_search_id IN (32291, 32292);
SELECT asf.activity_search_id, asf.id, asf.value
FROM activity_search_filters asf
WHERE asf.filter = 'group_id'
AND asf.value IN (
SELECT CONCAT(
HEX(SUBSTR(uuid, 5, 4)), '-',
HEX(SUBSTR(uuid, 3, 2)), '-',
HEX(SUBSTR(uuid, 1, 2)), '-',
HEX(SUBSTR(uuid, 9, 2)), '-',
HEX(SUBSTR(uuid, 11))
)
FROM groups
WHERE deleted_at IS NOT NULL
);
SELECT * FROM crm_configurations where id = 272; # 290 - Bonham & Brook , portalId: 5705856
SELECT * FROM opportunities WHERE team_id = 290 and updated_at > '2026-02-05 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 290 and updated_at > '2026-02-09 08:00:00' order by updated_at desc; # 6th
# [PASSWORD_DOTS]
SELECT * FROM crm_configurations where provider = 'hubspot';
SELECT * FROM crm_configurations where id = 1056; # 1119 - Chromatic , portalId: 45602133
SELECT * FROM opportunities WHERE team_id = 1119 and remotely_created_at > '2026-02-01 00:00:00' order by updated_at desc;
SELECT * FROM opportunities WHERE team_id = 1119 and updated_at > '2026-02-09 09:00:00' order by updated_at desc; # null
# [PASSWORD_DOTS]
select * from contacts where crm_provider_id = '003Uu00000ojD4NIAU';
select
cp.*
# DISTINCT t.id
# cp.id, cp.user_id, t.id, cp.crm_configuration_id, cp.contact_fields
FROM crm_profiles cp
JOIN crm_configurations crm on crm.id = cp.crm_configuration_id
JOIN users u on u.id = cp.user_id
JOIN teams t ON t.id = crm.team_id
WHERE crm.provider = 'salesforce' and t.status = 'active'
and cp.archived_at IS NULL and u.deleted_at IS NULL
and t.id NOT IN (1093)
and t.id = 2
and cp.contact_fields IS NULL;
# and c.crm_provider_id = '003Uu00000ojD4NIAU';
SELECT * FROM users WHERE id = 26484;
SELECT * FROM crm_profiles WHERE user_id = 26484;
SELECT * FROM social_accounts WHERE sociable_id = 26484;
SELECT * FROM crm_configurations where provider = 'salesforce';
select * from users where id IN (10022, 10403);
select * from users where team_id IN (526);
select * from teams where id IN (526, 532);
select * from crm_configurations where id IN (500, 516);
select * from crm_profiles where crm_configuration_id IN (500, 516) and user_id IN (10022, 10403);
select * from contacts where crm_configuration_id IN (500, 516) and crm_provider_id = '003Uu00000ojD4NIAU';
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 526 and sa.provider = 'salesforce';
select * from team_settings where team_id IN (526, 532);
select * from users where id IN (22824);
select * from crm_profiles where crm_configuration_id IN (1026);
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1093 and sa.provider = 'salesforce';
select * from teams where id = 1099;
select * from users where id = 29643
select * from activity_processing_states;
SELECT * FROM teams where name LIKE '%Fare%'; # 233
SELECT * FROM opportunities where crm_configuration_id = 215
# and crm_provider_id = 'oppo_ogESZf2P50nDrd1nGPvKDXeA6sSaTN5v51Lp4ayVzKR'
;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1088 and sa.provider = 'hubspot';
SELECT * FROM teams order by updated_at DESC
SELECT * FROM crm_configurations WHERE id = 1019; # SimpleConsign 1088 - no social account
select * from crm_configurations where provider = 'pipedrive';
select * from teams where id = 957;
select * from crm_configurations where id = 957;
SELECT * FROM teams WHERE name LIKE '%Prolific%'; # 544, 518, 10743
SELECT * FROM opportunities where crm_configuration_id = 518 order by id desc;
select * from users where team_id = 1; # 26726 - Gabriela Dureva
SELECT * FROM opportunities where user_id = 26726; # 16834447 - Prolific
select * from activities where user_id = 26726 order by id desc;
select * from contacts where crm_configuration_id = 1
and email IN ('[EMAIL]', '[EMAIL]'); # 2094416, 2093620
SELECT * FROM contacts WHERE id = 6284931;
SELECT p.* FROM activities a JOIN participants p ON a.id = p.activity_id
WHERE a.user_id = 26726 and p.lead_id IN (2094416, 2093620) and a.created_at > '2026-01-01 00:00:00' order by p.email;
select * from activities where id IN (75509259,75509261,75509261,75511034,75026464,75517602,75517605);
select * from crm_configurations where id = 1;
43801692-1aeb-32ce-acba-5b80a479701a
44c3c9cf-6f5e-75f3-8179-bc9f75dd2b1b
405975c0-b3d0-7aaa-821f-09d59cae6dd1
4caf848d-4bed-2299-b248-7788d41f9fca
49bedc3f-f196-eef3-89c3-dea6a3b4aa63
43420989-a09d-b8f8-9806-c8bbf7a02aac
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1 and sa.provider = 'salesforce';
SELECT * FROM activities WHERE id = 75461988;
SELECT * FROM activities WHERE uuid_to_bin('d6c5052e-e972-49e9-8912-26f2f7d6c5f6') = uuid;
select * from contacts where id = 17900517;
select * from contact_roles cr join crm_configurations crm on cr.crm_configuration_id = crm.id
where crm.provider != 'salesforce';
select * from users where id = 21047;
SELECT * FROM crm_configurations WHERE id = 892;
SELECT * FROM teams WHERE id = 942;
select * from opportunities where team_id = 942 order by updated_at desc;
select * from contacts where team_id = 942 order by updated_at desc;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 942 and sa.provider = 'hubspot';
SELECT * FROM opportunities where team_id = 1 and crm_provider_id IN ('006Pq00000NeH6XIAV', '006Pq000007z8kdIAA'); # 10697889, 6621430
SELECT * FROM crm_configurations WHERE id = 1;
SELECT * FROM teams WHERE crm_id = 1;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1 and sa.provider = 'salesforce';
select id, user_id, opportunity_fields from crm_profiles where crm_configuration_id = 1
SELECT * FROM opportunities where team_id = 1 order by updated_at desc; # 10697889, 6621430
select * from teams where id = 852;
select * from groups where id = 2286;
select * from sidekick_settings where team_id = 852;
select * from default_activity_types where team_id = 852;
SELECT cc.provider, cc.id, p.id, u.*
FROM users u
LEFT JOIN crm_profiles p ON u.id = p.user_id AND p.id IS NULL -- no profile
INNER JOIN teams t ON u.team_id = t.id AND t.status = 'active' -- team is active
INNER JOIN crm_configurations cc ON t.crm_id = cc.id
WHERE u.status = 1 AND u.deleted_at IS NULL
AND u.crm_required = 1
AND u.team_id = 1
ORDER BY u.team_id;
SELECT * FROM crm_profiles cp where cp.crm_configuration_id = 1 and cp.user_id IN (
18481
);
SELECT cc.provider, cc.id, p.id, u.*
FROM users u
LEFT JOIN crm_profiles p ON u.id = p.user_id
INNER JOIN teams t ON u.team_id = t.id AND t.status = 'active'
INNER JOIN crm_configurations cc ON t.crm_id = cc.id
WHERE u.status = 1
AND u.deleted_at IS NULL
AND u.crm_required = 1
# AND u.team_id = 1
AND p.id IS NULL -- Move this condition to WHERE clause
ORDER BY u.team_id;
SELECT * FROM opportunities WHERE id = 20002609;
select * from teams where id = 1122; # Velatir, 29953 - [EMAIL]
select * from crm_configurations where id = 1060;
select * from crm_layouts where crm_configuration_id = 1060;
SELECT * FROM crm_layout_entities WHERE crm_layout_id = 3596;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 1122 and sa.provider = 'hubspot';
select * from opportunities where team_id = 1122 order by updated_at desc;
select * from crm_field_data where object_type = 'contact';
SELECT * FROM activities WHERE uuid_to_bin('374fc8ed-3315-4c9f-9b25-318b7fd2928f') = uuid; # 76584262
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 248 and sa.provider = 'salesforce';
SELECT * FROM crm_profiles where user_id = 24115; # 005QF000002CswMYAS
SELECT * FROM users where id = 24115;
SELECT * FROM accounts where id = 4002896;
SELECT * FROM teams WHERE name LIKE '%adswerve%';
SELECT * FROM opportunities where crm_configuration_id = 230 AND crm_provider_id IN ("0069N000003GIQ9QAO","0061r000019yGP9AAM","0066900001S2KWlAAN","0066900001TDpj2AAD","0066900001b8uEwAAI","0069N000001rQi0QAE","006QF00000KD40mYAD","006QF00000LzpRJYAZ","0069N000002uomtQAA","0069N000002xlMLQAY","0066900001NV6ubAAD","0061r00001HJp45AAD","006QF00000uTlUoYAK","006QF00000v0bZqYAI");
SELECT * FROM opportunities WHERE crm_configuration_id = 230 AND crm_provider_id = '0069N000003GIQ9QAO'; # 6272203
SELECT u.id, u.email, ac.name, a.* FROM activities a
JOIN users u ON a.user_id = u.id
JOIN accounts ac ON a.account_id = ac.id
WHERE
uuid_to_bin('e3269598-b562-44fb-b5e9-9d2694dc63e0') = a.uuid or
uuid_to_bin('66ddc3ab-4e15-45aa-af0c-248c1eece593') = a.uuid or
uuid_to_bin('826bd328-e1cc-4213-b8d8-572454cacc07') = a.uuid;
select * from users where id = 5825;
SELECT * FROM activities WHERE uuid_to_bin('e56aa2e8-231a-421b-ab1f-cb38ed2bf573') = uuid;
select * from activities where uuid_to_bin('91e13b2f-2d1b-45f8-b1fd-1141b6563782') = uuid;
19594, 862
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 862 and sa.provider = 'salesforce';
select * from automated_reports where id = 36;
select ar.frequency, r.*, ar.* from automated_report_results r
join automated_reports ar on r.report_id = ar.id
where ar.frequency != 'one_off';
select s.* from activity_searches s join users u ON s.user_id = u.id where u.team_id = 882;
select * from nudges n where n.activity_search_id
select * from teams where created_at > '2026-03-09';
SELECT * FROM crm_layouts WHERE crm_configuration_id = 1065; # 1065
SELECT * FROM crm_layout_entities WHERE crm_layout_id = 3617;
select * from users where team_id = 1 and name like '%Lukas%'; # 7160
SELECT * FROM teams WHERE id = 575;
select * from opportunities where team_id = 575;
SELECT * FROM teams WHERE name LIKE '%Integrum ESG%'; # 1126, 1065,
select * from opportunities where team_id = 1126;
SELECT * FROM teams WHERE name LIKE '%Base%'; # 1125, 1063,
select * from opportunities where team_id = 1125;
select * from contacts c
where c.team_id = 882;
SELECT * FROM activities WHERE id = 76822967;
SELECT * FROM crm_profiles WHERE user_id = 15440;
SELECT * FROM crm_profiles WHERE crm_configuration_id = 555;
SELECT * FROM crm_configurations WHERE id = 555;
SELECT * FROM users WHERE id = 15440; # team. 581, gr. 15440, pl. 3911, act. field 162182
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 581 and sa.provider = 'salesforce';
SELECT * FROM automated_report_results order by id desc;
select * from features;
select * from team_features where feature_id = 40;
select * from teams where id = 556;
select * from automated_reports;
where id = 54; # 4fdd41f6-dcf0-30d0-b339-7345381b6044 , ["pdf","podcast"]
SELECT * FROM automated_report_results WHERE uuid_to_bin('822fa41b-afd3-43a9-a248-86b0e36f3131') = uuid;
select * from automated_report_results order by id desc;
SELECT * FROM automated_report_results WHERE id = 1919;
select * from automated_report_results WHERE report_id = 54;
select * from opportunities where id = 7594349;
SELECT * FROM teams WHERE name LIKE '%Les%'; # 711, 692, 16067 - [EMAIL]
select * from playbooks where team_id = 711; # event 226147
SELECT * FROM playbook_categories WHERE playbook_id = 5515;
SELECT * FROM crm_fields WHERE crm_configuration_id = 692 and object_type = 'event';
SELECT * FROM crm_fields WHERE id = 226147;
SELECT * FROM crm_field_values WHERE crm_field_id = 226147;
SELECT * FROM crm_configurations WHERE id = 692;
SELECT
CONCAT(u.id, CASE WHEN u.id = t.owner_id THEN ' (owner)' ELSE '' END) AS user_id,
u.email,
sa.*,
t.owner_id FROM social_accounts sa
JOIN users u on u.id = sa.sociable_id
JOIN teams t on t.id = u.team_id
WHERE u.team_id = 711 and sa.provider = 'salesforce';
SELECT * FROM crm_profiles cp JOIN users u on u.id = cp.user_id WHERE u.team_id = 711;
select * from leads;
select * from calendars;
SELECT
t.id AS team_id,
t.name,
LOWER(SUBSTRING_INDEX(c.calendar_provider_id, '@', -1)) AS calendar_domain
FROM teams t
JOIN users u ON u.team_id = t.id
JOIN calendars c ON c.user_id = u.id AND c.status = 'active' AND c.calendar_provider_id LIKE '%@%'
LEFT JOIN team_domains td
ON td.team_id = t.id
AND td.deleted_at IS NULL
AND td.domain = LOWER(SUBSTRING_INDEX(c.calendar_provider_id, '@', -1))
GROUP BY t.id, t.name, calendar_domain
ORDER BY t.name, calendar_domain;
select * from users u join calendars c on c.user_id = u.id
where u.team_id = 882;
select * from activities where id = 74049485; # team 563 crm 537
select * from activities where id = 73272382; # team 563 crm 537
select * from activities where id = 64400389; # team 563 crm 537
select * from activities where id = 58081273; # team 563 crm 537
select * from activities where id = 54520297; # team 563 crm 537
select * from participants where activity_id = 58081273;
select * from activities where crm_configuration_id = 537 and provider = 'aircall'
and account_id = 19003658 order by updated_at desc;
select * from contacts where crm_configuration_id = 537 and id = 35957759;
select * from accounts where crm_configuration_id = 537 and id = 19003658;
select * from automated_report_results where id = 1976;
select * from automated_reports where id = 583;
select * from activity_searches where id = 87714;
select * from activity_search_filters where activity_search_id = 87714;
SELECT * FROM activities WHERE uuid_to_bin('8827f672-202d-4162-9d04-73ff5f0566a9') = uuid
or uuid_to_bin('47842446-af51-4bcb-854f-cc6560290101') = uuid;
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – UserAutomatedReportsController.php
|
NULL
|
76660
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
13
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Http\Controllers\API\UserAutomatedReports;
use Illuminate\Support\Carbon;
use Illuminate\Http\JsonResponse;
use Jiminny\Exceptions\ApplicationException;
use Jiminny\Http\Controllers\Controller;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Models\User;
use Jiminny\Repositories\AutomatedReportsRepository;
use Jiminny\Services\ApiResponseService;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSort;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSortDirection;
use Illuminate\Http\Request;
use Throwable;
class UserAutomatedReportsController extends Controller
{
public const int RESULTS_PER_PAGE = 25;
public const string SORT_COLUMN = 'sort_column';
public const string SORT_DIRECTION = 'sort_direction';
public function __construct(
private readonly AutomatedReportsRepository $automatedReportsRepository,
private readonly AutomatedReportsService $automatedReportsService,
private readonly ApiResponseService $apiResponseService,
private readonly Response $response
) {
parent::__construct();
}
/**
* @throws ApplicationException
*/
public function list(Request $request): JsonResponse
{
/** @var User $user */
$user = $request->user();
$teamIds = $request->has('team')
? (array) $request->get('team')
: [];
$reportTypes = $request->has('report_type')
? (array) $request->get('report_type')
: [];
$name = $request->has('name') ? trim($request->get('name', '')) : null;
try {
$fromDate = $request->has('from_date') ? Carbon::parse($request->get('from_date')) : null;
$toDate = $request->has('to_date') ? Carbon::parse($request->get('to_date')) : null;
} catch (\Exception) {
return $this->response->errorWrongArgs('Invalid date.');
}
$page = $request->has('page') ? (int) $request->get('page') : 1;
$sort = ReportSort::tryFrom(
$request->get(self::SORT_COLUMN, '')
) ?? ReportSort::GENERATED_AT;
$sortDirection = ReportSortDirection::tryFrom(
strtolower($request->get(self::SORT_DIRECTION, ''))
) ?? ReportSortDirection::DESC;
$paginatedUserReports = $this->automatedReportsRepository->getPaginatedUserReports(
user: $user,
sort: $sort,
sortDirection: $sortDirection,
resultsPerPage: self::RESULTS_PER_PAGE,
page: $page,
fromDate: $fromDate,
toDate: $toDate,
teamIds: array_map('intval', $teamIds),
reportTypes: $reportTypes,
name: $name,
);
$reportResults = $this->automatedReportsService->transformReportResults(
$paginatedUserReports->getCollection()
);
$team = $user->getTeam();
$reportTypeFilter = $this->automatedReportsService->getReportTypeFieldData(
shortVersion: true,
team: $team
);
$data = $this->apiResponseService->fromPaginatorToArray(
paginator: $paginatedUserReports,
data: $reportResults,
moreMeta: [
self::SORT_COLUMN => $sort->value,
self::SORT_DIRECTION => $sortDirection->value,
],
filters: [
$reportTypeFilter['id'] => $reportTypeFilter,
],
);
return $this->response->withArray($data);
}
public function delete(Request $request, string $uuid): JsonResponse
{
/** @var User $user */
$user = $request->user();
try {
$result = $this->automatedReportsRepository->findResultByUuidForUser($uuid, $user);
if ($result === null) {
return new JsonResponse(
data: ['error' => 'Report not found'],
status: JsonResponse::HTTP_NOT_FOUND
);
}
$result->delete();
return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
} catch (Throwable $e) {
return new JsonResponse(
data: ['error' => 'Failed to delete report result'],
status: JsonResponse::HTTP_INTERNAL_SERVER_ERROR
);
}
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All...
|
PhpStorm
|
faVsco.js – custom.log
|
NULL
|
76661
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
DMSActivityMorerireroxToolsHelpcalMistorbookmarksJiminny …..vXStarredi• jiminny-x-integrati..8 platform-inner-teamE) Channels# ai-chapter# ai-team# alerts# backend# c-learning-peoplei confusion-clinic# curiosity_labadeal-insichts-dev# engineering# frontend# general# infra-changes# jiminny-bg8 people-with-copilo...8 people-with-zoom-# platform-team# platform-tickets# product_launches# random# releases# sofia-office# support# thank-yous# the Deople of iimi...ProtllesWindow& platform-inner-...& 10MessagesChannel OverviewMoreYesterdayjinnylaop Aor 22nd Added by GitHubNikolay Ivanov 3:24 PMнякой нещо да е настроивал по githubactions. Почна да прави къмити вместо менбез да съм му разрешевал?https:/github.com/lminnv/app/pull/1200//changes/a68f42f210859f838a4fdced451f750627besoioИли нещо аз не разбирам?0AA0e 20 replies Last reply 18...Nikolay Yankov 3:50PMreplied to a uhread: някои нешо ла е насто..лол. ами предлагам маи ла му заораним лапускам към всички ла вилятNikolav Yankov 9.38 AMЩе се забавя за дейлито. Започнете без Мен.Aneliva Angelova 9:43 AMIДобро утро, няма да успея да вляза влейлито. Пествам ньлжовете.Message & platform-inner-team+ Aa I..•) New TabAl reports promotion pages by nik• JY-9712 | Nuges to expire after on8 Jiminnyu Userpilot Logged-activityJY-20157 add not enough activ XPipelines - jiminny/app+ New Tab©github.com/jimjiminny / app 8<> Code87 Pull requests 31( Agents |© Actions•• Wiki © Security and quality 32 ~ Insights 3 Settings@ On April 24 we'll start using GitHub Copilot interaction data for Al model training unless you opt out. Review this update and manage your preferences in your GitHub account settings.JY-20157 add not enough activities notification #12011 •$1 Open LakyLak wants to merge 2 commits into master from JY-20157-AJ-report-not-send-notification@) Conversation o• Commits 2|- Checks 21E Files changed 13A © All commits +Q Filter files...apo/Console/Commands/Reports/AutomatedReportsCommand.ohp@ -61,21 +61,29 @ public function handle(): intv = Console/Commands/Renorts|Snow = Carbon: : now();E AutomatedReportsCommand...v Jobs/AutomatedReportsE RequestGenerateAskJiminnyR...SendReportNotGeneratedMail...v @ Listeners/AutomatedReports/U….E TrackAutomatedReportGener...v # Mail/ReportsS1sMondav = Snow->1SMonday)S1sr1rstDayUtMonth = Snow->day === 1;ScurrentMonth = Snow->month.// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);$this->logger->info(self::LOG_PREFIX . ' Checking conditions', [+ ReportNotGenerated.ohp |"1SMonday' => S1SMonday,~ E Services/Kiosk/AutomatedRepo…..AskJiminnyReportActivityServ…'isFirstDay0fMonth' => SisFirstDay0fMonth,'currentMonth' => ScurrentMonth.E AutomatedReportsService.php'isQuarterlyMonth' => SisQuarterlyMonth,~E resources/views/emails/reportsreport-not-generated.blade.php/I Process dailv revortsl• F tests/UnitSthis->processReports(AutomatedReportsService::FREQUENCYDAILY):~ Jobs/AutomatedReportsE ReguestGenerateAsk JiminnvR....v = listeners/AutomatedRenorts/U.₴ TrackAutomatedReportGener..v E Services/Kiosk/AutomatedRepo…..E AskJiminnyReportActivityServ....AutomatedReportsServiceActi…./ Process weekly renorts on Mondavcif (SisMondav) {64 +67 +74 +86 +@40@ Daily - Platform - nowQ Type to search100% C4 8• Fri 24 Apr 9:46:13• Checks pending Code • (Preview) -+384 -52 9000C 0 I 13 viewedSubmit review+10 -2 mane [ Viewed0 ...Snow = Carhon:.nowdSisMondav = Snow->1SMonday)"Sisweekend = $now->isWeekend():SisFirstDay0fMonth = Snow->day === 1;ScurrentMonth = Snow->month.SisManualTrigger = $this->option('report-id') !== null;// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);Sthis->loager->info(self::L0G PREFIX . ' Checkina conditions'. [I"isMonday' => SisMonday,'isweekend' => $isWeekend,'isFirstDay0fMonth' => $isFirstDay0fMonth,'currentMonth' => ScurrentMonth.l'isQuarterlyMonth' => SisQuarterlyMonth,/ Process dailv renorts on weekdavs onlv (skio Saturdav/Sundav)...// Manual triggers via --report-id bypass the weekend skip.if (I Sisweekend || SisManualTriager) {Sthis->processReports(AutomatedReportsService::FREQUENCY_DAILY):} else {ISthis->logger->info(self::L0G PREFIX . ' Skipping daily reports on weekend'):/ Process weekly renorts on Mondavslif (SisMonday) {...
|
PhpStorm
|
faVsco.js – custom.log
|
NULL
|
76662
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
13
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Http\Controllers\API\UserAutomatedReports;
use Illuminate\Support\Carbon;
use Illuminate\Http\JsonResponse;
use Jiminny\Exceptions\ApplicationException;
use Jiminny\Http\Controllers\Controller;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Models\User;
use Jiminny\Repositories\AutomatedReportsRepository;
use Jiminny\Services\ApiResponseService;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSort;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSortDirection;
use Illuminate\Http\Request;
use Throwable;
class UserAutomatedReportsController extends Controller
{
public const int RESULTS_PER_PAGE = 25;
public const string SORT_COLUMN = 'sort_column';
public const string SORT_DIRECTION = 'sort_direction';
public function __construct(
private readonly AutomatedReportsRepository $automatedReportsRepository,
private readonly AutomatedReportsService $automatedReportsService,
private readonly ApiResponseService $apiResponseService,
private readonly Response $response
) {
parent::__construct();
}
/**
* @throws ApplicationException
*/
public function list(Request $request): JsonResponse
{
/** @var User $user */
$user = $request->user();
$teamIds = $request->has('team')
? (array) $request->get('team')
: [];
$reportTypes = $request->has('report_type')
? (array) $request->get('report_type')
: [];
$name = $request->has('name') ? trim($request->get('name', '')) : null;
try {
$fromDate = $request->has('from_date') ? Carbon::parse($request->get('from_date')) : null;
$toDate = $request->has('to_date') ? Carbon::parse($request->get('to_date')) : null;
} catch (\Exception) {
return $this->response->errorWrongArgs('Invalid date.');
}
$page = $request->has('page') ? (int) $request->get('page') : 1;
$sort = ReportSort::tryFrom(
$request->get(self::SORT_COLUMN, '')
) ?? ReportSort::GENERATED_AT;
$sortDirection = ReportSortDirection::tryFrom(
strtolower($request->get(self::SORT_DIRECTION, ''))
) ?? ReportSortDirection::DESC;
$paginatedUserReports = $this->automatedReportsRepository->getPaginatedUserReports(
user: $user,
sort: $sort,
sortDirection: $sortDirection,
resultsPerPage: self::RESULTS_PER_PAGE,
page: $page,
fromDate: $fromDate,
toDate: $toDate,
teamIds: array_map('intval', $teamIds),
reportTypes: $reportTypes,
name: $name,
);
$reportResults = $this->automatedReportsService->transformReportResults(
$paginatedUserReports->getCollection()
);
$team = $user->getTeam();
$reportTypeFilter = $this->automatedReportsService->getReportTypeFieldData(
shortVersion: true,
team: $team
);
$data = $this->apiResponseService->fromPaginatorToArray(
paginator: $paginatedUserReports,
data: $reportResults,
moreMeta: [
self::SORT_COLUMN => $sort->value,
self::SORT_DIRECTION => $sortDirection->value,
],
filters: [
$reportTypeFilter['id'] => $reportTypeFilter,
],
);
return $this->response->withArray($data);
}
public function delete(Request $request, string $uuid): JsonResponse
{
/** @var User $user */
$user = $request->user();
try {
$result = $this->automatedReportsRepository->findResultByUuidForUser($uuid, $user);
if ($result === null) {
return new JsonResponse(
data: ['error' => 'Report not found'],
status: JsonResponse::HTTP_NOT_FOUND
);
}
$result->delete();
return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
} catch (Throwable $e) {
return new JsonResponse(
data: ['error' => 'Failed to delete report result'],
status: JsonResponse::HTTP_INTERNAL_SERVER_ERROR
);
}
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide
app ~/jiminny/app, folder
.circleci, folder
.cursor, folder
.github
.sonarlint, folder
.vscode, folder
.windsurf, folder
app, sources root
Actions, folder
Component, folder
Acl, folder
ActionItems, folder
Activity, folder
ActivityAnalytics, folder
ActivitySearch, folder
AiActivityType, folder
AiAutomation, folder
AiCallScoring, folder
AskAnything, folder
Dtos, folder
Events, folder
AskAnythingPromptService.php, class
HistoryService.php, class
AskJiminnyAi, folder
AWS, folder
BillingManagement, folder
Cache, folder
CoachingFeedback, folder
Country, folder
CustomerApi, folder
Database, folder
Datadog, folder
DateTime, folder
DealInsights, folder
DealRisks, folder
ElasticSearch, folder
Eloquent, folder
Encoding, folder
Encryption, folder
ES, folder
Faker, folder
FeatureFlags, folder
FFMpeg, folder
FileSystem, folder
Gecko, folder
Gong, folder
GuzzleHttp, folder
KeyPoints, folder
Kiosk, folder
LanguageDetection, folder
LiveFeed, folder
Locks, folder
Math, folder
MediaPipeline, folder
MeetingBot, folder
MobileSettings, folder
Model, folder
Notification, folder
Nudge, folder
ParagraphBreaker, folder
ParticipantSpeech, folder
PartitionedCookie, folder
PlaybackPage, folder
Playlist, folder...
|
PhpStorm
|
faVsco.js – custom.log
|
NULL
|
76663
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
13
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Http\Controllers\API\UserAutomatedReports;
use Illuminate\Support\Carbon;
use Illuminate\Http\JsonResponse;
use Jiminny\Exceptions\ApplicationException;
use Jiminny\Http\Controllers\Controller;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Models\User;
use Jiminny\Repositories\AutomatedReportsRepository;
use Jiminny\Services\ApiResponseService;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSort;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSortDirection;
use Illuminate\Http\Request;
use Throwable;
class UserAutomatedReportsController extends Controller
{
public const int RESULTS_PER_PAGE = 25;
public const string SORT_COLUMN = 'sort_column';
public const string SORT_DIRECTION = 'sort_direction';
public function __construct(
private readonly AutomatedReportsRepository $automatedReportsRepository,
private readonly AutomatedReportsService $automatedReportsService,
private readonly ApiResponseService $apiResponseService,
private readonly Response $response
) {
parent::__construct();
}
/**
* @throws ApplicationException
*/
public function list(Request $request): JsonResponse
{
/** @var User $user */
$user = $request->user();
$teamIds = $request->has('team')
? (array) $request->get('team')
: [];
$reportTypes = $request->has('report_type')
? (array) $request->get('report_type')
: [];
$name = $request->has('name') ? trim($request->get('name', '')) : null;
try {
$fromDate = $request->has('from_date') ? Carbon::parse($request->get('from_date')) : null;
$toDate = $request->has('to_date') ? Carbon::parse($request->get('to_date')) : null;
} catch (\Exception) {
return $this->response->errorWrongArgs('Invalid date.');
}
$page = $request->has('page') ? (int) $request->get('page') : 1;
$sort = ReportSort::tryFrom(
$request->get(self::SORT_COLUMN, '')
) ?? ReportSort::GENERATED_AT;
$sortDirection = ReportSortDirection::tryFrom(
strtolower($request->get(self::SORT_DIRECTION, ''))
) ?? ReportSortDirection::DESC;
$paginatedUserReports = $this->automatedReportsRepository->getPaginatedUserReports(
user: $user,
sort: $sort,
sortDirection: $sortDirection,
resultsPerPage: self::RESULTS_PER_PAGE,
page: $page,
fromDate: $fromDate,
toDate: $toDate,
teamIds: array_map('intval', $teamIds),
reportTypes: $reportTypes,
name: $name,
);
$reportResults = $this->automatedReportsService->transformReportResults(
$paginatedUserReports->getCollection()
);
$team = $user->getTeam();
$reportTypeFilter = $this->automatedReportsService->getReportTypeFieldData(
shortVersion: true,
team: $team
);
$data = $this->apiResponseService->fromPaginatorToArray(
paginator: $paginatedUserReports,
data: $reportResults,
moreMeta: [
self::SORT_COLUMN => $sort->value,
self::SORT_DIRECTION => $sortDirection->value,
],
filters: [
$reportTypeFilter['id'] => $reportTypeFilter,
],
);
return $this->response->withArray($data);
}
public function delete(Request $request, string $uuid): JsonResponse
{
/** @var User $user */
$user = $request->user();
try {
$result = $this->automatedReportsRepository->findResultByUuidForUser($uuid, $user);
if ($result === null) {
return new JsonResponse(
data: ['error' => 'Report not found'],
status: JsonResponse::HTTP_NOT_FOUND
);
}
$result->delete();
return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
} catch (Throwable $e) {
return new JsonResponse(
data: ['error' => 'Failed to delete report result'],
status: JsonResponse::HTTP_INTERNAL_SERVER_ERROR
);
}
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide
app ~/jiminny/app, folder
.circleci, folder
.cursor, folder
.github
.sonarlint, folder
.vscode, folder
.windsurf, folder
app, sources root
Actions, folder
Component, folder
Acl, folder
ActionItems, folder
Activity, folder
ActivityAnalytics, folder
ActivitySearch, folder
AiActivityType, folder
AiAutomation, folder
AiCallScoring, folder
AskAnything, folder
Dtos, folder
Events, folder
AskAnythingPromptService.php, class
HistoryService.php, class
AskJiminnyAi, folder
AWS, folder
BillingManagement, folder
Cache, folder
CoachingFeedback, folder
Country, folder
CustomerApi, folder
Database, folder
Datadog, folder
DateTime, folder
DealInsights, folder
DealRisks, folder
ElasticSearch, folder
Eloquent, folder
Encoding, folder
Encryption, folder
ES, folder
Faker, folder
FeatureFlags, folder
FFMpeg, folder
FileSystem, folder
Gecko, folder
Gong, folder
GuzzleHttp, folder
KeyPoints, folder
Kiosk, folder
LanguageDetection, folder
LiveFeed, folder
Locks, folder
Math, folder
MediaPipeline, folder
MeetingBot, folder
MobileSettings, folder
Model, folder
Notification, folder
Nudge, folder
ParagraphBreaker, folder
ParticipantSpeech, folder
PartitionedCookie, folder
PlaybackPage, folder
Playlist, folder
Prophet, folder
ProphetAi, folder
ProsperWorks, folder
Queue, folder
Router, folder
Saml2, folder
SCIM, folder
Seeder, folder
Sentry, folder
Serializer, folder
Settings, folder
Sidekick, folder
Slack, folder
TeamInsights, folder
TimeMemoryMapper, folder
Transcription, folder
TranscriptionSummary, folder
Twilio, folder
Uploader, folder
UrlGenerator, folder
Utility, folder
Uuid, folder
Waveform, folder
Webhooks, folder
Workflow, folder
Configuration, folder
Console, folder
Commands, folder
Activities, folder
Analytics, folder
Calendars, folder
Crm, folder
Hubspot, folder
IntegrationApp, folder
Traits, folder
AddLayoutEntities.php, class
AutologDelayedCommand.php, class
BullhornCommandAbstract.php, abstract class
BullhornPingCommand.php, class
BullhornSearchCommand.php, class
BullhornSessionCommand.php, class
CheckActivityLoggableCommand.php, final class
CleanDuplicateFieldDataCommand.php, class
FullSyncOpportunityCommand.php, class
LogActivitiesCommand.php, final class
ManageSyncStrategyCommand.php, class
MatchCrmObjectsCommand.php, class
MatchOpportunityActivitiesCommand.php, class
MigrateProvider.php, class
ProcessHubspotObjectsSyncBatches.php, class
PurgeDeletedOpportunitiesCommand.php, class
ResetGovernorLimits.php, class
SendNotLogged.php, class
SetupActivityTypeForFollowUp.php, final class
SetupCloseCrm.php, class
SetupCopperCrm.php, class
SetupCrmCommand.php, abstract class
SetupLayouts.php, class
SyncAccount.php, class
SyncContact.php, class...
|
PhpStorm
|
faVsco.js – UserAutomatedReportsController.php
|
NULL
|
76664
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
13
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Http\Controllers\API\UserAutomatedReports;
use Illuminate\Support\Carbon;
use Illuminate\Http\JsonResponse;
use Jiminny\Exceptions\ApplicationException;
use Jiminny\Http\Controllers\Controller;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Models\User;
use Jiminny\Repositories\AutomatedReportsRepository;
use Jiminny\Services\ApiResponseService;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSort;
use Jiminny\Services\Kiosk\AutomatedReports\ReportSortDirection;
use Illuminate\Http\Request;
use Throwable;
class UserAutomatedReportsController extends Controller
{
public const int RESULTS_PER_PAGE = 25;
public const string SORT_COLUMN = 'sort_column';
public const string SORT_DIRECTION = 'sort_direction';
public function __construct(
private readonly AutomatedReportsRepository $automatedReportsRepository,
private readonly AutomatedReportsService $automatedReportsService,
private readonly ApiResponseService $apiResponseService,
private readonly Response $response
) {
parent::__construct();
}
/**
* @throws ApplicationException
*/
public function list(Request $request): JsonResponse
{
/** @var User $user */
$user = $request->user();
$teamIds = $request->has('team')
? (array) $request->get('team')
: [];
$reportTypes = $request->has('report_type')
? (array) $request->get('report_type')
: [];
$name = $request->has('name') ? trim($request->get('name', '')) : null;
try {
$fromDate = $request->has('from_date') ? Carbon::parse($request->get('from_date')) : null;
$toDate = $request->has('to_date') ? Carbon::parse($request->get('to_date')) : null;
} catch (\Exception) {
return $this->response->errorWrongArgs('Invalid date.');
}
$page = $request->has('page') ? (int) $request->get('page') : 1;
$sort = ReportSort::tryFrom(
$request->get(self::SORT_COLUMN, '')
) ?? ReportSort::GENERATED_AT;
$sortDirection = ReportSortDirection::tryFrom(
strtolower($request->get(self::SORT_DIRECTION, ''))
) ?? ReportSortDirection::DESC;
$paginatedUserReports = $this->automatedReportsRepository->getPaginatedUserReports(
user: $user,
sort: $sort,
sortDirection: $sortDirection,
resultsPerPage: self::RESULTS_PER_PAGE,
page: $page,
fromDate: $fromDate,
toDate: $toDate,
teamIds: array_map('intval', $teamIds),
reportTypes: $reportTypes,
name: $name,
);
$reportResults = $this->automatedReportsService->transformReportResults(
$paginatedUserReports->getCollection()
);
$team = $user->getTeam();
$reportTypeFilter = $this->automatedReportsService->getReportTypeFieldData(
shortVersion: true,
team: $team
);
$data = $this->apiResponseService->fromPaginatorToArray(
paginator: $paginatedUserReports,
data: $reportResults,
moreMeta: [
self::SORT_COLUMN => $sort->value,
self::SORT_DIRECTION => $sortDirection->value,
],
filters: [
$reportTypeFilter['id'] => $reportTypeFilter,
],
);
return $this->response->withArray($data);
}
public function delete(Request $request, string $uuid): JsonResponse
{
/** @var User $user */
$user = $request->user();
try {
$result = $this->automatedReportsRepository->findResultByUuidForUser($uuid, $user);
if ($result === null) {
return new JsonResponse(
data: ['error' => 'Report not found'],
status: JsonResponse::HTTP_NOT_FOUND
);
}
$result->delete();
return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT);
} catch (Throwable $e) {
return new JsonResponse(
data: ['error' => 'Failed to delete report result'],
status: JsonResponse::HTTP_INTERNAL_SERVER_ERROR
);
}
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide
app ~/jiminny/app, folder
.circleci, folder
.cursor, folder
.github
.sonarlint, folder
.vscode, folder
.windsurf, folder
app, sources root
Actions, folder
Component, folder
Acl, folder
ActionItems, folder
Activity, folder
ActivityAnalytics, folder
ActivitySearch, folder
AiActivityType, folder
AiAutomation, folder
AiCallScoring, folder
AskAnything, folder
Dtos, folder
Events, folder
AskAnythingPromptService.php, class
HistoryService.php, class
AskJiminnyAi, folder
AWS, folder
BillingManagement, folder
Cache, folder
CoachingFeedback, folder
Country, folder
CustomerApi, folder
Database, folder
Datadog, folder
DateTime, folder
DealInsights, folder
DealRisks, folder
ElasticSearch, folder
Eloquent, folder
Encoding, folder
Encryption, folder
ES, folder
Faker, folder
FeatureFlags, folder
FFMpeg, folder
FileSystem, folder
Gecko, folder
Gong, folder
GuzzleHttp, folder
KeyPoints, folder
Kiosk, folder
LanguageDetection, folder
LiveFeed, folder
Locks, folder
Math, folder
MediaPipeline, folder
MeetingBot, folder
MobileSettings, folder
Model, folder
Notification, folder
Nudge, folder
ParagraphBreaker, folder
ParticipantSpeech, folder
PartitionedCookie, folder
PlaybackPage, folder
Playlist, folder
Prophet, folder
ProphetAi, folder
ProsperWorks, folder
Queue, folder
Router, folder
Saml2, folder
SCIM, folder
Seeder, folder
Sentry, folder
Serializer, folder
Settings, folder
Sidekick, folder
Slack, folder
TeamInsights, folder
TimeMemoryMapper, folder
Transcription, folder
TranscriptionSummary, folder
Twilio, folder
Uploader, folder
UrlGenerator, folder
Utility, folder
Uuid, folder
Waveform, folder
Webhooks, folder
Workflow, folder
Configuration, folder
Console, folder
Commands, folder
Activities, folder
Analytics, folder
Calendars, folder
Crm, folder
Hubspot, folder
IntegrationApp, folder
Traits, folder
AddLayoutEntities.php, class
AutologDelayedCommand.php, class
BullhornCommandAbstract.php, abstract class
BullhornPingCommand.php, class
BullhornSearchCommand.php, class
BullhornSessionCommand.php, class
CheckActivityLoggableCommand.php, final class
CleanDuplicateFieldDataCommand.php, class
FullSyncOpportunityCommand.php, class
LogActivitiesCommand.php, final class
ManageSyncStrategyCommand.php, class
MatchCrmObjectsCommand.php, class
MatchOpportunityActivitiesCommand.php, class
MigrateProvider.php, class
ProcessHubspotObjectsSyncBatches.php, class
PurgeDeletedOpportunitiesCommand.php, class
ResetGovernorLimits.php, class
SendNotLogged.php, class
SetupActivityTypeForFollowUp.php, final class
SetupCloseCrm.php, class
SetupCopperCrm.php, class
SetupCrmCommand.php, abstract class
SetupLayouts.php, class
SyncAccount.php, class
SyncContact.php, class
SyncFieldMetadata.php, class
SyncHubspotActiveDeals.php, class
SyncHubspotObjects.php, class
SyncLead.php, class
SyncObjects.php, class
SyncOpportunitiesMissingFieldDataCommand.php, class
SyncOpportunity.php, class
SyncProfileMetadata.php, class
SyncTeamMetadata.php, class
UpdateOpportunitySpecifications.php, class...
|
PhpStorm
|
faVsco.js – UserAutomatedReportsController.php
|
NULL
|
76665
|
|
Firefox• 0FileEditViewHistory→BookmarksProfilesToo Firefox• 0FileEditViewHistory→BookmarksProfilesToolsWindowHelpmeet.google.com/agt-teir-cwt?authuser=lukas.kovalik%40jiminny.com•Daily - Platform - now100% K78 • Fri 24 Apr 9:46:13|=Pop out this videoNikolay NikolovStefka StoyanovaGalya DimitrovaLukas Kovalik9:46 AM | Daily - Platform• 0:27...
|
PhpStorm
|
faVsco.js – UserAutomatedReportsController.php
|
NULL
|
76666
|
|
Project: faVsco.js, menu
DMSActivityMorerireroxToo Project: faVsco.js, menu
DMSActivityMorerireroxToolsHelpcalMistorbookmarksJiminny …..vXStarredi• jiminny-x-integrati..8 platform-inner-teamE) Channels# ai-chapter# ai-team# alerts# backend# c-learning-peoplei confusion-clinic# curiosity_labadeal-insichts-dev# engineering# frontend# general# infra-changes# jiminny-bg8 people-with-copilo...8 people-with-zoom-# platform-team# platform-tickets# product_launches# random# releases# sofia-office# support# thank-yous# the Deople of iimi...ProtllesWindow& platform-inner-...& 10MessagesChannel OverviewMoreYesterdayjinnylaop Aor 22nd Added by GitHubNikolay Ivanov 3:24 PMнякой нещо да е настроивал по githubactions. Почна да прави къмити вместо менбез да съм му разрешевал?https:/github.com/lminnv/app/pull/1200//changes/a68f42f210859f838a4fdced451f750627besoioИли нещо аз не разбирам?0AA0e 20 replies Last reply 18...Nikolay Yankov 3:50PMreplied to a uhread: някои нешо ла е насто..лол. ами предлагам маи ла му заораним лапускам към всички ла вилятNikolav Yankov 9.38 AMЩе се забавя за дейлито. Започнете без Мен.Aneliva Angelova 9:43 AMIДобро утро, няма да успея да вляза влейлито. Пествам ньлжовете.Message & platform-inner-team+ Aa I..•) New TabAl reports promotion pages by nik• JY-9712 | Nuges to expire after on8 Jiminnyu Userpilot Logged-activityJY-20157 add not enough activ XPipelines - jiminny/app+ New Tab©github.com/jimjiminny / app 8<> Code87 Pull requests 31( Agents |© Actions•• Wiki © Security and quality 32 ~ Insights 3 Settings@ On April 24 we'll start using GitHub Copilot interaction data for Al model training unless you opt out. Review this update and manage your preferences in your GitHub account settings.JY-20157 add not enough activities notification #12011 •$1 Open LakyLak wants to merge 2 commits into master from JY-20157-AJ-report-not-send-notification@) Conversation o• Commits 2|- Checks 21E Files changed 13A © All commits +Q Filter files...apo/Console/Commands/Reports/AutomatedReportsCommand.ohp@ -61,21 +61,29 @ public function handle(): intv = Console/Commands/Renorts|Snow = Carbon: : now();E AutomatedReportsCommand...v Jobs/AutomatedReportsE RequestGenerateAskJiminnyR...SendReportNotGeneratedMail...v @ Listeners/AutomatedReports/U….E TrackAutomatedReportGener...v # Mail/ReportsS1sMondav = Snow->1SMonday)S1sr1rstDayUtMonth = Snow->day === 1;ScurrentMonth = Snow->month.// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);$this->logger->info(self::LOG_PREFIX . ' Checking conditions', [+ ReportNotGenerated.ohp |"1SMonday' => S1SMonday,~ E Services/Kiosk/AutomatedRepo…..AskJiminnyReportActivityServ…'isFirstDay0fMonth' => SisFirstDay0fMonth,'currentMonth' => ScurrentMonth.E AutomatedReportsService.php'isQuarterlyMonth' => SisQuarterlyMonth,~E resources/views/emails/reportsreport-not-generated.blade.php/I Process dailv revortsl• F tests/UnitSthis->processReports(AutomatedReportsService::FREQUENCYDAILY):~ Jobs/AutomatedReportsE ReguestGenerateAsk JiminnvR....v = listeners/AutomatedRenorts/U.₴ TrackAutomatedReportGener..v E Services/Kiosk/AutomatedRepo…..E AskJiminnyReportActivityServ....AutomatedReportsServiceActi…./ Process weekly renorts on Mondavcif (SisMondav) {64 +67 +74 +86 +@40@ Daily - Platform - nowQ Type to search100% C4 8• Fri 24 Apr 9:46:13• Checks pending Code • (Preview) -+384 -52 9000C 0 I 13 viewedSubmit review+10 -2 mane [ Viewed0 ...Snow = Carhon:.nowdSisMondav = Snow->1SMonday)"Sisweekend = $now->isWeekend():SisFirstDay0fMonth = Snow->day === 1;ScurrentMonth = Snow->month.SisManualTrigger = $this->option('report-id') !== null;// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);Sthis->loager->info(self::L0G PREFIX . ' Checkina conditions'. [I"isMonday' => SisMonday,'isweekend' => $isWeekend,'isFirstDay0fMonth' => $isFirstDay0fMonth,'currentMonth' => ScurrentMonth.l'isQuarterlyMonth' => SisQuarterlyMonth,/ Process dailv renorts on weekdavs onlv (skio Saturdav/Sundav)...// Manual triggers via --report-id bypass the weekend skip.if (I Sisweekend || SisManualTriager) {Sthis->processReports(AutomatedReportsService::FREQUENCY_DAILY):} else {ISthis->logger->info(self::L0G PREFIX . ' Skipping daily reports on weekend'):/ Process weekly renorts on Mondavslif (SisMonday) {...
|
PhpStorm
|
faVsco.js – UserAutomatedReportsController.php
|
NULL
|
76667
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
1
2
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
try {
foreach ($this->resolveUsers($automatedReport) as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide
app ~/jiminny/app, folder
.circleci, folder
.cursor, folder
.github
.sonarlint, folder
.vscode, folder
.windsurf, folder
app, sources root
Actions, folder
Component, folder
Acl, folder
ActionItems, folder
Activity, folder
ActivityAnalytics, folder
ActivitySearch, folder
AiActivityType, folder
AiAutomation, folder
AiCallScoring, folder
AskAnything, folder
Dtos, folder
Events, folder
AskAnythingPromptService.php, class
HistoryService.php, class
AskJiminnyAi, folder
AWS, folder
BillingManagement, folder
Cache, folder
CoachingFeedback, folder
Country, folder
CustomerApi, folder
Database, folder
Datadog, folder
DateTime, folder
DealInsights, folder
DealRisks, folder
ElasticSearch, folder
Eloquent, folder
Encoding, folder
Encryption, folder
ES, folder
Faker, folder
FeatureFlags, folder
FFMpeg, folder
FileSystem, folder
Gecko, folder
Gong, folder
GuzzleHttp, folder
KeyPoints, folder
Kiosk, folder
LanguageDetection, folder
LiveFeed, folder
Locks, folder
Math, folder
MediaPipeline, folder
MeetingBot, folder
MobileSettings, folder
Model, folder
Notification, folder
Nudge, folder
ParagraphBreaker, folder
ParticipantSpeech, folder
PartitionedCookie, folder
PlaybackPage, folder
Playlist, folder
Prophet, folder
ProphetAi, folder
ProsperWorks, folder
Queue, folder
Router, folder
Saml2, folder
SCIM, folder
Seeder, folder
Sentry, folder
Serializer, folder
Settings, folder
Sidekick, folder
Slack, folder
TeamInsights, folder
TimeMemoryMapper, folder
Transcription, folder
TranscriptionSummary, folder
Twilio, folder
Uploader, folder
UrlGenerator, folder
Utility, folder
Uuid, folder
Waveform, folder
Webhooks, folder
Workflow, folder
Configuration, folder
Console, folder
Commands, folder
Activities, folder
Analytics, folder
Calendars, folder
Crm, folder
Hubspot, folder
IntegrationApp, folder
Traits, folder
AddLayoutEntities.php, class
AutologDelayedCommand.php, class
BullhornCommandAbstract.php, abstract class
BullhornPingCommand.php, class
BullhornSearchCommand.php, class
BullhornSessionCommand.php, class
CheckActivityLoggableCommand.php, final class
CleanDuplicateFieldDataCommand.php, class
FullSyncOpportunityCommand.php, class
LogActivitiesCommand.php, final class
ManageSyncStrategyCommand.php, class
MatchCrmObjectsCommand.php, class
MatchOpportunityActivitiesCommand.php, class
MigrateProvider.php, class
ProcessHubspotObjectsSyncBatches.php, class
PurgeDeletedOpportunitiesCommand.php, class
ResetGovernorLimits.php, class
SendNotLogged.php, class...
|
PhpStorm
|
faVsco.js – custom.log
|
NULL
|
76668
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
1
2
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
try {
foreach ($this->resolveUsers($automatedReport) as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – custom.log
|
NULL
|
76669
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
1
2
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
try {
foreach ($this->resolveUsers($automatedReport) as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – custom.log
|
NULL
|
76670
|
|
Firefox• 0FileEditViewHistory→BookmarksProfilesToo Firefox• 0FileEditViewHistory→BookmarksProfilesToolsWindowHelpmeet.google.com/agt-teir-cwt?authuser=lukas.kovalik%40jiminny.com•Daily - Platform - now100% K78 • Fri 24 Apr 9:46:13|=Pop out this videoNikolay NikolovStefka StoyanovaGalya DimitrovaLukas Kovalik9:46 AM | Daily - Platform• 0:27...
|
PhpStorm
|
|
NULL
|
76671
|
|
DMSActivityMorerireroxToolsHelpcalMistorbookmarksJ DMSActivityMorerireroxToolsHelpcalMistorbookmarksJiminny …..vXStarredi• jiminny-x-integrati..8 platform-inner-teamE) Channels# ai-chapter# ai-team# alerts# backend# c-learning-peoplei confusion-clinic# curiosity_labadeal-insichts-dev# engineering# frontend# general# infra-changes# jiminny-bg8 people-with-copilo...8 people-with-zoom-# platform-team# platform-tickets# product_launches# random# releases# sofia-office# support# thank-yous# the Deople of iimi...ProtllesWindow& platform-inner-...& 10MessagesChannel OverviewMoreYesterdayjinnylaop Aor 22nd Added by GitHubNikolay Ivanov 3:24 PMнякой нещо да е настроивал по githubactions. Почна да прави къмити вместо менбез да съм му разрешевал?https:/github.com/lminnv/app/pull/1200//changes/a68f42f210859f838a4fdced451f750627besoioИли нещо аз не разбирам?0AA0e 20 replies Last reply 18...Nikolay Yankov 3:50PMreplied to a uhread: някои нешо ла е насто..лол. ами предлагам маи ла му заораним лапускам към всички ла вилятNikolav Yankov 9.38 AMЩе се забавя за дейлито. Започнете без Мен.Aneliva Angelova 9:43 AMIДобро утро, няма да успея да вляза влейлито. Пествам ньлжовете.Message & platform-inner-team+ Aa I..•) New TabAl reports promotion pages by nik• JY-9712 | Nuges to expire after on8 Jiminnyu Userpilot Logged-activityJY-20157 add not enough activ XPipelines - jiminny/app+ New Tab©github.com/jimjiminny / app 8<> Code87 Pull requests 31( Agents |© Actions•• Wiki © Security and quality 32 ~ Insights 3 Settings@ On April 24 we'll start using GitHub Copilot interaction data for Al model training unless you opt out. Review this update and manage your preferences in your GitHub account settings.JY-20157 add not enough activities notification #12011 •$1 Open LakyLak wants to merge 2 commits into master from JY-20157-AJ-report-not-send-notification@) Conversation o• Commits 2|- Checks 21E Files changed 13A © All commits +Q Filter files...apo/Console/Commands/Reports/AutomatedReportsCommand.ohp@ -61,21 +61,29 @ public function handle(): intv = Console/Commands/Renorts|Snow = Carbon: : now();E AutomatedReportsCommand...v Jobs/AutomatedReportsE RequestGenerateAskJiminnyR...SendReportNotGeneratedMail...v @ Listeners/AutomatedReports/U….E TrackAutomatedReportGener...v # Mail/ReportsS1sMondav = Snow->1SMonday)S1sr1rstDayUtMonth = Snow->day === 1;ScurrentMonth = Snow->month.// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);$this->logger->info(self::LOG_PREFIX . ' Checking conditions', [+ ReportNotGenerated.ohp |"1SMonday' => S1SMonday,~ E Services/Kiosk/AutomatedRepo…..AskJiminnyReportActivityServ…'isFirstDay0fMonth' => SisFirstDay0fMonth,'currentMonth' => ScurrentMonth.E AutomatedReportsService.php'isQuarterlyMonth' => SisQuarterlyMonth,~E resources/views/emails/reportsreport-not-generated.blade.php/I Process dailv revortsl• F tests/UnitSthis->processReports(AutomatedReportsService::FREQUENCYDAILY):~ Jobs/AutomatedReportsE ReguestGenerateAsk JiminnvR....v = listeners/AutomatedRenorts/U.₴ TrackAutomatedReportGener..v E Services/Kiosk/AutomatedRepo…..E AskJiminnyReportActivityServ....AutomatedReportsServiceActi…./ Process weekly renorts on Mondavcif (SisMondav) {64 +67 +74 +86 +@40@ Daily - Platform - nowQ Type to search100% C4 8• Fri 24 Apr 9:46:13• Checks pending Code • (Preview) -+384 -52 9000C 0 I 13 viewedSubmit review+10 -2 mane [ Viewed0 ...Snow = Carhon:.nowdSisMondav = Snow->1SMonday)"Sisweekend = $now->isWeekend():SisFirstDay0fMonth = Snow->day === 1;ScurrentMonth = Snow->month.SisManualTrigger = $this->option('report-id') !== null;// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);Sthis->loager->info(self::L0G PREFIX . ' Checkina conditions'. [I"isMonday' => SisMonday,'isweekend' => $isWeekend,'isFirstDay0fMonth' => $isFirstDay0fMonth,'currentMonth' => ScurrentMonth.l'isQuarterlyMonth' => SisQuarterlyMonth,/ Process dailv renorts on weekdavs onlv (skio Saturdav/Sundav)...// Manual triggers via --report-id bypass the weekend skip.if (I Sisweekend || SisManualTriager) {Sthis->processReports(AutomatedReportsService::FREQUENCY_DAILY):} else {ISthis->logger->info(self::L0G PREFIX . ' Skipping daily reports on weekend'):/ Process weekly renorts on Mondavslif (SisMonday) {...
|
PhpStorm
|
|
NULL
|
76672
|
|
Switch workspaces… (Jiminny Inc) Has new messages
Switch workspaces… (Jiminny Inc) Has new messages
Home
Home
DMs
DMs
Activity
Activity
Files
Files
Later
Later
More…
More
Unreads
Threads
Huddles
Drafts & sent
Directories
jiminny-x-integration-app
platform-inner-team
ai-chapter
ai-team
alerts
backend
c-learning-people
confusion-clinic
curiosity_lab
deal-insights-dev
engineering
frontend
general
infra-changes
jiminny-bg
people-with-copilot-licences
people-with-zoom-phone-licences
platform-team
platform-tickets
product_launches
random
releases
sofia-office
support
thank-yous
the_people_of_jiminny
Aneliya Angelova
,
Nikolay Yankov
,
Steliyan Georgiev
Stefka Stoyanova
Adelina Petrova
Vasil Vasilev
Stoyan Tomov
Galya Dimitrova
Nikolay Yankov
Petko Kashinski
Aneliya Angelova
Nikolay Nikolov
Mario Georgiev
Todor Stamatov
Gabriela Dureva
Jira Cloud
Toast
Messages
Messages
Add canvas
Add canvas
Files
Files
Add and Edit Channel Tabs
Canvas
List
Folder
Jump to first unread message (⌘J)
Mark as read (esc)
Jump to date
Aneliya Angelova
Yesterday at 10:14:00 AM
10:14 AM
оки
Yesterday at 10:15:09 AM
10:15
тайтъла на репорта и Data Source секцията ще показват правилния период нали - понеже това се разминаваше до сега
Lukas Kovalik
Yesterday at 10:47:31 AM
10:47 AM
сега забелязах че стария count който се ползва в при контрол на достъп canAccessAiReport брои само ресултати който са пратени, не тези които са генерирани
Nikolay Yankov
Yesterday at 1:31:46 PM
1:31 PM
какво значи които са пратени?
Yesterday at 1:32:17 PM
1:32
по принцип условието беше - ако за user-a има генерирано и може да го гледа на ai-reports страницата
Lukas Kovalik
Yesterday at 1:35:32 PM
1:35 PM
да, говоря за нещо още от преди, count който се гледа дали има user право да гледа ai-reports страница , брои пратени не тези който са генерирани само. Реално ако се праща всичко на ред почти няма да се вижда разлика
(edited)
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Jump to date
New
Aneliya Angelova
Today at 11:13:41 AM
11:13 AM
Лукаш вчера генерирах 3 седмични репорта на. Галя на prod us. Обаче не ги пуснах по емейл и тази сутрин ги е получила по мейл.
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Lukas Kovalik
Today at 11:15:05 AM
11:15 AM
седмични не са ли в понеделник само
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Aneliya Angelova
Today at 11:15:19 AM
11:15 AM
да
React with white_check_mark
Firefox• 0FileEditViewHistory→BookmarksProfilesToolsWindowHelpmeet.google.com/agt-teir-cwt?authuser=lukas.kovalik%40jiminny.com•Daily - Platform - now100% K78 • Fri 24 Apr 9:46:13|=Pop out this videoNikolay NikolovStefka StoyanovaGalya DimitrovaLukas Kovalik9:46 AM | Daily - Platform• 0:27...
|
Slack
|
! Aneliya Angelova, Nikolay Yankov, Steliyan Georg ! Aneliya Angelova, Nikolay Yankov, Steliyan Georgiev (DM) - Jiminny Inc - 1 new item - Slack...
|
NULL
|
76673
|
|
Switch workspaces… (Jiminny Inc) Has new messages
Switch workspaces… (Jiminny Inc) Has new messages
Home
Home
DMs
DMs
Activity
Activity
Files
Files
Later
Later
More…
More
Unreads
Threads
Huddles
Drafts & sent
Directories
jiminny-x-integration-app
platform-inner-team
ai-chapter
ai-team
alerts
backend
c-learning-people
confusion-clinic
curiosity_lab
deal-insights-dev
engineering
frontend
general
infra-changes
jiminny-bg
people-with-copilot-licences
people-with-zoom-phone-licences
platform-team
platform-tickets
product_launches
random
releases
sofia-office
support
thank-yous
the_people_of_jiminny
Aneliya Angelova
,
Nikolay Yankov
,
Steliyan Georgiev
Stefka Stoyanova
Adelina Petrova
Vasil Vasilev
Stoyan Tomov
Galya Dimitrova
Nikolay Yankov
Petko Kashinski
Aneliya Angelova
Nikolay Nikolov
Mario Georgiev
Todor Stamatov
Gabriela Dureva
DMSActivityMorerireroxToolsHelpcalMistorbookmarksJiminny …..vXStarredi• jiminny-x-integrati..8 platform-inner-teamE) Channels# ai-chapter# ai-team# alerts# backend# c-learning-peoplei confusion-clinic# curiosity_labadeal-insichts-dev# engineering# frontend# general# infra-changes# jiminny-bg8 people-with-copilo...8 people-with-zoom-# platform-team# platform-tickets# product_launches# random# releases# sofia-office# support# thank-yous# the Deople of iimi...ProtllesWindow& platform-inner-...& 10MessagesChannel OverviewMoreYesterdayjinnylaop Aor 22nd Added by GitHubNikolay Ivanov 3:24 PMнякой нещо да е настроивал по githubactions. Почна да прави къмити вместо менбез да съм му разрешевал?https:/github.com/lminnv/app/pull/1200//changes/a68f42f210859f838a4fdced451f750627besoioИли нещо аз не разбирам?0AA0e 20 replies Last reply 18...Nikolay Yankov 3:50PMreplied to a uhread: някои нешо ла е насто..лол. ами предлагам маи ла му заораним лапускам към всички ла вилятNikolav Yankov 9.38 AMЩе се забавя за дейлито. Започнете без Мен.Aneliva Angelova 9:43 AMIДобро утро, няма да успея да вляза влейлито. Пествам ньлжовете.Message & platform-inner-team+ Aa I..•) New TabAl reports promotion pages by nik• JY-9712 | Nuges to expire after on8 Jiminnyu Userpilot Logged-activityJY-20157 add not enough activ XPipelines - jiminny/app+ New Tab©github.com/jimjiminny / app 8<> Code87 Pull requests 31( Agents |© Actions•• Wiki © Security and quality 32 ~ Insights 3 Settings@ On April 24 we'll start using GitHub Copilot interaction data for Al model training unless you opt out. Review this update and manage your preferences in your GitHub account settings.JY-20157 add not enough activities notification #12011 •$1 Open LakyLak wants to merge 2 commits into master from JY-20157-AJ-report-not-send-notification@) Conversation o• Commits 2|- Checks 21E Files changed 13A © All commits +Q Filter files...apo/Console/Commands/Reports/AutomatedReportsCommand.ohp@ -61,21 +61,29 @ public function handle(): intv = Console/Commands/Renorts|Snow = Carbon: : now();E AutomatedReportsCommand...v Jobs/AutomatedReportsE RequestGenerateAskJiminnyR...SendReportNotGeneratedMail...v @ Listeners/AutomatedReports/U….E TrackAutomatedReportGener...v # Mail/ReportsS1sMondav = Snow->1SMonday)S1sr1rstDayUtMonth = Snow->day === 1;ScurrentMonth = Snow->month.// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);$this->logger->info(self::LOG_PREFIX . ' Checking conditions', [+ ReportNotGenerated.ohp |"1SMonday' => S1SMonday,~ E Services/Kiosk/AutomatedRepo…..AskJiminnyReportActivityServ…'isFirstDay0fMonth' => SisFirstDay0fMonth,'currentMonth' => ScurrentMonth.E AutomatedReportsService.php'isQuarterlyMonth' => SisQuarterlyMonth,~E resources/views/emails/reportsreport-not-generated.blade.php/I Process dailv revortsl• F tests/UnitSthis->processReports(AutomatedReportsService::FREQUENCYDAILY):~ Jobs/AutomatedReportsE ReguestGenerateAsk JiminnvR....v = listeners/AutomatedRenorts/U.₴ TrackAutomatedReportGener..v E Services/Kiosk/AutomatedRepo…..E AskJiminnyReportActivityServ....AutomatedReportsServiceActi…./ Process weekly renorts on Mondavcif (SisMondav) {64 +67 +74 +86 +@40@ Daily - Platform - nowQ Type to search100% C4 8• Fri 24 Apr 9:46:13• Checks pending Code • (Preview) -+384 -52 9000C 0 I 13 viewedSubmit review+10 -2 mane [ Viewed0 ...Snow = Carhon:.nowdSisMondav = Snow->1SMonday)"Sisweekend = $now->isWeekend():SisFirstDay0fMonth = Snow->day === 1;ScurrentMonth = Snow->month.SisManualTrigger = $this->option('report-id') !== null;// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);Sthis->loager->info(self::L0G PREFIX . ' Checkina conditions'. [I"isMonday' => SisMonday,'isweekend' => $isWeekend,'isFirstDay0fMonth' => $isFirstDay0fMonth,'currentMonth' => ScurrentMonth.l'isQuarterlyMonth' => SisQuarterlyMonth,/ Process dailv renorts on weekdavs onlv (skio Saturdav/Sundav)...// Manual triggers via --report-id bypass the weekend skip.if (I Sisweekend || SisManualTriager) {Sthis->processReports(AutomatedReportsService::FREQUENCY_DAILY):} else {ISthis->logger->info(self::L0G PREFIX . ' Skipping daily reports on weekend'):/ Process weekly renorts on Mondavslif (SisMonday) {...
|
Slack
|
! Aneliya Angelova, Nikolay Yankov, Steliyan Georg ! Aneliya Angelova, Nikolay Yankov, Steliyan Georgiev (DM) - Jiminny Inc - 1 new item - Slack...
|
NULL
|
76674
|
|
Switch workspaces… (Jiminny Inc) Has new messages
Switch workspaces… (Jiminny Inc) Has new messages
Home
Home
DMs
DMs
Activity
Activity
Files
Files
Later
Later
More…
More
Unreads
Threads
Huddles
Drafts & sent
Directories
jiminny-x-integration-app
platform-inner-team
ai-chapter
ai-team
alerts
backend
c-learning-people
confusion-clinic
curiosity_lab
deal-insights-dev
engineering
frontend
general
infra-changes
jiminny-bg
people-with-copilot-licences
people-with-zoom-phone-licences
platform-team
platform-tickets
product_launches
random
releases
sofia-office
support
thank-yous
the_people_of_jiminny
Aneliya Angelova
,
Nikolay Yankov
,
Steliyan Georgiev
Stefka Stoyanova
Adelina Petrova
Vasil Vasilev
Stoyan Tomov
Galya Dimitrova
Nikolay Yankov
Petko Kashinski
Aneliya Angelova
Nikolay Nikolov
Mario Georgiev
Todor Stamatov
Gabriela Dureva
Jira Cloud
Toast
Messages
Messages
Add canvas
Add canvas
Files
Files
Add and Edit Channel Tabs
Canvas
List
Folder
Jump to date
Aneliya Angelova
Yesterday at 10:14:00 AM
10:14 AM
оки
Yesterday at 10:15:09 AM
10:15
тайтъла на репорта и Data Source секцията ще показват правилния период нали - понеже това се разминаваше до сега
Lukas Kovalik
DMSActivityMorerireroxToolsHelpcalMistorbookmarksJiminny …..vXStarredi• jiminny-x-integrati..8 platform-inner-teamE) Channels# ai-chapter# ai-team# alerts# backend# c-learning-peoplei confusion-clinic# curiosity_labadeal-insichts-dev# engineering# frontend# general# infra-changes# jiminny-bg8 people-with-copilo...8 people-with-zoom-# platform-team# platform-tickets# product_launches# random# releases# sofia-office# support# thank-yous# the Deople of iimi...ProtllesWindow& platform-inner-...& 10MessagesChannel OverviewMoreYesterdayjinnylaop Aor 22nd Added by GitHubNikolay Ivanov 3:24 PMнякой нещо да е настроивал по githubactions. Почна да прави къмити вместо менбез да съм му разрешевал?https:/github.com/lminnv/app/pull/1200//changes/a68f42f210859f838a4fdced451f750627besoioИли нещо аз не разбирам?0AA0e 20 replies Last reply 18...Nikolay Yankov 3:50PMreplied to a uhread: някои нешо ла е насто..лол. ами предлагам маи ла му заораним лапускам към всички ла вилятNikolav Yankov 9.38 AMЩе се забавя за дейлито. Започнете без Мен.Aneliva Angelova 9:43 AMIДобро утро, няма да успея да вляза влейлито. Пествам ньлжовете.Message & platform-inner-team+ Aa I..•) New TabAl reports promotion pages by nik• JY-9712 | Nuges to expire after on8 Jiminnyu Userpilot Logged-activityJY-20157 add not enough activ XPipelines - jiminny/app+ New Tab©github.com/jimjiminny / app 8<> Code87 Pull requests 31( Agents |© Actions•• Wiki © Security and quality 32 ~ Insights 3 Settings@ On April 24 we'll start using GitHub Copilot interaction data for Al model training unless you opt out. Review this update and manage your preferences in your GitHub account settings.JY-20157 add not enough activities notification #12011 •$1 Open LakyLak wants to merge 2 commits into master from JY-20157-AJ-report-not-send-notification@) Conversation o• Commits 2|- Checks 21E Files changed 13A © All commits +Q Filter files...apo/Console/Commands/Reports/AutomatedReportsCommand.ohp@ -61,21 +61,29 @ public function handle(): intv = Console/Commands/Renorts|Snow = Carbon: : now();E AutomatedReportsCommand...v Jobs/AutomatedReportsE RequestGenerateAskJiminnyR...SendReportNotGeneratedMail...v @ Listeners/AutomatedReports/U….E TrackAutomatedReportGener...v # Mail/ReportsS1sMondav = Snow->1SMonday)S1sr1rstDayUtMonth = Snow->day === 1;ScurrentMonth = Snow->month.// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);$this->logger->info(self::LOG_PREFIX . ' Checking conditions', [+ ReportNotGenerated.ohp |"1SMonday' => S1SMonday,~ E Services/Kiosk/AutomatedRepo…..AskJiminnyReportActivityServ…'isFirstDay0fMonth' => SisFirstDay0fMonth,'currentMonth' => ScurrentMonth.E AutomatedReportsService.php'isQuarterlyMonth' => SisQuarterlyMonth,~E resources/views/emails/reportsreport-not-generated.blade.php/I Process dailv revortsl• F tests/UnitSthis->processReports(AutomatedReportsService::FREQUENCYDAILY):~ Jobs/AutomatedReportsE ReguestGenerateAsk JiminnvR....v = listeners/AutomatedRenorts/U.₴ TrackAutomatedReportGener..v E Services/Kiosk/AutomatedRepo…..E AskJiminnyReportActivityServ....AutomatedReportsServiceActi…./ Process weekly renorts on Mondavcif (SisMondav) {64 +67 +74 +86 +@40@ Daily - Platform - nowQ Type to search100% C4 8• Fri 24 Apr 9:46:13• Checks pending Code • (Preview) -+384 -52 9000C 0 I 13 viewedSubmit review+10 -2 mane [ Viewed0 ...Snow = Carhon:.nowdSisMondav = Snow->1SMonday)"Sisweekend = $now->isWeekend():SisFirstDay0fMonth = Snow->day === 1;ScurrentMonth = Snow->month.SisManualTrigger = $this->option('report-id') !== null;// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);Sthis->loager->info(self::L0G PREFIX . ' Checkina conditions'. [I"isMonday' => SisMonday,'isweekend' => $isWeekend,'isFirstDay0fMonth' => $isFirstDay0fMonth,'currentMonth' => ScurrentMonth.l'isQuarterlyMonth' => SisQuarterlyMonth,/ Process dailv renorts on weekdavs onlv (skio Saturdav/Sundav)...// Manual triggers via --report-id bypass the weekend skip.if (I Sisweekend || SisManualTriager) {Sthis->processReports(AutomatedReportsService::FREQUENCY_DAILY):} else {ISthis->logger->info(self::L0G PREFIX . ' Skipping daily reports on weekend'):/ Process weekly renorts on Mondavslif (SisMonday) {...
|
Slack
|
Aneliya Angelova, Nikolay Yankov, Steliyan Georgie Aneliya Angelova, Nikolay Yankov, Steliyan Georgiev (DM) - Jiminny Inc - 1 new item - Slack...
|
NULL
|
76675
|
|
Switch workspaces… (Jiminny Inc) Has new messages
Switch workspaces… (Jiminny Inc) Has new messages
Home
Home
DMs
DMs
Activity
Activity
Files
Files
Later
Later
More…
More
Unreads
Threads
Huddles
Drafts & sent
Directories
jiminny-x-integration-app
platform-inner-team
ai-chapter
ai-team
alerts
backend
c-learning-people
confusion-clinic
curiosity_lab
deal-insights-dev
engineering
frontend
general
infra-changes
jiminny-bg
people-with-copilot-licences
people-with-zoom-phone-licences
platform-team
platform-tickets
product_launches
random
releases
sofia-office
support
thank-yous
the_people_of_jiminny
Aneliya Angelova
,
Nikolay Yankov
,
Steliyan Georgiev
Stefka Stoyanova
Adelina Petrova
Vasil Vasilev
Stoyan Tomov
Galya Dimitrova
Nikolay Yankov
Petko Kashinski
Aneliya Angelova
Nikolay Nikolov
Mario Georgiev
Todor Stamatov
Gabriela Dureva
Jira Cloud
Toast
Messages
Messages
Add canvas
Add canvas
Files
Files
Add and Edit Channel Tabs
Canvas
List
Folder
Jump to date
Aneliya Angelova
Yesterday at 10:14:00 AM
10:14 AM
оки
Yesterday at 10:15:09 AM
10:15
тайтъла на репорта и Data Source секцията ще показват правилния период нали - понеже това се разминаваше до сега
Lukas Kovalik
Yesterday at 10:47:31 AM
10:47 AM
сега забелязах че стария count който се ползва в при контрол на достъп canAccessAiReport брои само ресултати който са пратени, не тези които са генерирани
Nikolay Yankov
Yesterday at 1:31:46 PM
1:31 PM
какво значи които са пратени?
Yesterday at 1:32:17 PM
1:32
по принцип условието беше - ако за user-a има генерирано и може да го гледа на ai-reports страницата
Lukas Kovalik
Yesterday at 1:35:32 PM
1:35 PM
да, говоря за нещо още от преди, count който се гледа дали има user право да гледа ai-reports страница , брои пратени не тези който са генерирани само. Реално ако се праща всичко на ред почти няма да се вижда разлика
(edited)
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Jump to date
Aneliya Angelova
Today at 11:13:41 AM
11:13 AM
Лукаш вчера генерирах 3 седмични репорта на. Галя на prod us. Обаче не ги пуснах по емейл и тази сутрин ги е получила по мейл.
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Lukas Kovalik
Today at 11:15:05 AM
11:15 AM
седмични не са ли в понеделник само
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Firefox• 0FileEditViewHistory→BookmarksProfilesToolsWindowHelpmeet.google.com/agt-teir-cwt?authuser=lukas.kovalik%40jiminny.com•Daily - Platform - now100% K78 • Fri 24 Apr 9:46:13|=Pop out this videoNikolay NikolovStefka StoyanovaGalya DimitrovaLukas Kovalik9:46 AM | Daily - Platform• 0:27...
|
Slack
|
Aneliya Angelova, Nikolay Yankov, Steliyan Georgie Aneliya Angelova, Nikolay Yankov, Steliyan Georgiev (DM) - Jiminny Inc - 1 new item - Slack...
|
NULL
|
76676
|
|
Switch workspaces… (Jiminny Inc) Has new messages
Switch workspaces… (Jiminny Inc) Has new messages
Home
Home
DMs
DMs
Activity
Activity
Files
Files
Later
Later
More…
More
Unreads
Threads
Huddles
Drafts & sent
Directories
jiminny-x-integration-app
platform-inner-team
ai-chapter
ai-team
alerts
backend
c-learning-people
confusion-clinic
curiosity_lab
deal-insights-dev
engineering
frontend
general
infra-changes
jiminny-bg
people-with-copilot-licences
people-with-zoom-phone-licences
platform-team
platform-tickets
product_launches
random
releases
sofia-office
support
thank-yous
the_people_of_jiminny
Aneliya Angelova
,
Nikolay Yankov
,
Steliyan Georgiev
Stefka Stoyanova
Adelina Petrova
Vasil Vasilev
Stoyan Tomov
Galya Dimitrova
Nikolay Yankov
Petko Kashinski
Aneliya Angelova
Nikolay Nikolov
Mario Georgiev
Todor Stamatov
Gabriela Dureva
Jira Cloud
Toast
Messages
Messages
Add canvas
Add canvas
Files
Files
Add and Edit Channel Tabs
Canvas
List
Folder
Jump to date
Yesterday at 10:15:09 AM
10:15
тайтъла на репорта и Data Source секцията ще показват правилния период нали - понеже това се разминаваше до сега
Lukas Kovalik
Yesterday at 10:47:31 AM
10:47 AM
сега забелязах че стария count който се ползва в при контрол на достъп canAccessAiReport брои само ресултати който са пратени, не тези които са генерирани
Nikolay Yankov
Yesterday at 1:31:46 PM
1:31 PM
какво значи които са пратени?
Yesterday at 1:32:17 PM
1:32
по принцип условието беше - ако за user-a има генерирано и може да го гледа на ai-reports страницата
Lukas Kovalik
Yesterday at 1:35:32 PM
1:35 PM
да, говоря за нещо още от преди, count който се гледа дали има user право да гледа ai-reports страница , брои пратени не тези който са генерирани само. Реално ако се праща всичко на ред почти няма да се вижда разлика
(edited)
Jump to date
Aneliya Angelova
Today at 11:13:41 AM
11:13 AM
Лукаш вчера генерирах 3 седмични репорта на. Галя на prod us. Обаче не ги пуснах по емейл и тази сутрин ги е получила по мейл.
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Lukas Kovalik
Today at 11:15:05 AM
11:15 AM
седмични не са ли в понеделник само
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Aneliya Angelova
Today at 11:15:19 AM
11:15 AM
да
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
New
Today at 11:15:56 AM
11:15
аз ги генерирах вчера. Да не би сутрин да минава джоба за разпращане на мейли и да събира всички генерирани репорти със статус 3 и да ги разпраща
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Today at 11:16:08 AM
11:16
без да се съобразява далки е дневен или седмичен или месечен
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Lukas Kovalik
Today at 11:16:37 AM
11:16 AM
а да, за пращане мисля че няма проверки
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Today at 11:16:56 AM
11:16
чакай да видя, мисля че само при генериране
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Adelina Petrova, Direct Message, 1 of 7 suggestions
Aneliya Angelova is typing
Firefox• 0FileEditViewHistory→BookmarksProfilesToolsWindowHelpmeet.google.com/agt-teir-cwt?authuser=lukas.kovalik%40jiminny.com•Daily - Platform - now100% K78 • Fri 24 Apr 9:46:13|=Pop out this videoNikolay NikolovStefka StoyanovaGalya DimitrovaLukas Kovalik9:46 AM | Daily - Platform• 0:27...
|
Slack
|
Aneliya Angelova, Nikolay Yankov, Steliyan Georgie Aneliya Angelova, Nikolay Yankov, Steliyan Georgiev (DM) - Jiminny Inc - 1 new item - Slack...
|
NULL
|
76677
|
|
Switch workspaces… (Jiminny Inc) Has new messages
Switch workspaces… (Jiminny Inc) Has new messages
Home
Home
DMs
DMs
Activity
Activity
Files
Files
Later
Later
More…
More
Unreads
Threads
Huddles
Drafts & sent
Directories
jiminny-x-integration-app
platform-inner-team
ai-chapter
ai-team
alerts
backend
c-learning-people
confusion-clinic
curiosity_lab
deal-insights-dev
engineering
frontend
general
infra-changes
jiminny-bg
people-with-copilot-licences
people-with-zoom-phone-licences
platform-team
platform-tickets
product_launches
random
releases
sofia-office
support
thank-yous
the_people_of_jiminny
Aneliya Angelova
,
Nikolay Yankov
,
Steliyan Georgiev
Stefka Stoyanova
Adelina Petrova
Vasil Vasilev
Stoyan Tomov
Galya Dimitrova
Nikolay Yankov
Petko Kashinski
Aneliya Angelova
Nikolay Nikolov
Mario Georgiev
Todor Stamatov
Gabriela Dureva
Jira Cloud
Toast
Messages
Messages
Add canvas
Add canvas
Files
Files
Add and Edit Channel Tabs
Canvas
List
Folder
Jump to date
Yesterday at 10:15:09 AM
10:15
тайтъла на репорта и Data Source секцията ще показват правилния период нали - понеже това се разминаваше до сега
Lukas Kovalik
Yesterday at 10:47:31 AM
10:47 AM
сега забелязах че стария count който се ползва в при контрол на достъп canAccessAiReport брои само ресултати който са пратени, не тези които са генерирани
Nikolay Yankov
Yesterday at 1:31:46 PM
1:31 PM
какво значи които са пратени?
Yesterday at 1:32:17 PM
1:32
по принцип условието беше - ако за user-a има генерирано и може да го гледа на ai-reports страницата
Lukas Kovalik
Yesterday at 1:35:32 PM
1:35 PM
да, говоря за нещо още от преди, count който се гледа дали има user право да гледа ai-reports страница , брои пратени не тези който са генерирани само. Реално ако се праща всичко на ред почти няма да се вижда разлика
(edited)
Jump to date
Aneliya Angelova
Today at 11:13:41 AM
11:13 AM
Лукаш вчера генерирах 3 седмични репорта на. Галя на prod us. Обаче не ги пуснах по емейл и тази сутрин ги е получила по мейл.
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Lukas Kovalik
Today at 11:15:05 AM
11:15 AM
седмични не са ли в понеделник само
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Aneliya Angelova
Today at 11:15:19 AM
11:15 AM
да
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
New
Today at 11:15:56 AM
11:15
аз ги генерирах вчера. Да не би сутрин да минава джоба за разпращане на мейли и да събира всички генерирани репорти със статус 3 и да ги разпраща
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Today at 11:16:08 AM
11:16
без да се съобразява далки е дневен или седмичен или месечен
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Lukas Kovalik
Today at 11:16:37 AM
11:16 AM
а да, за пращане мисля че няма проверки
React with white_check_mark
React with eyes
React with raised_hands
Add reaction…
Reply in thread
Forward message…
Save for later
More actions
Adelina Petrova, Direct Message, 1 of 7 suggestions
Aneliya Angelova is typing
DMSActivityMorerireroxToolsHelpcalMistorbookmarksJiminny …..vXStarredi• jiminny-x-integrati..8 platform-inner-teamE) Channels# ai-chapter# ai-team# alerts# backend# c-learning-peoplei confusion-clinic# curiosity_labadeal-insichts-dev# engineering# frontend# general# infra-changes# jiminny-bg8 people-with-copilo...8 people-with-zoom-# platform-team# platform-tickets# product_launches# random# releases# sofia-office# support# thank-yous# the Deople of iimi...ProtllesWindow& platform-inner-...& 10MessagesChannel OverviewMoreYesterdayjinnylaop Aor 22nd Added by GitHubNikolay Ivanov 3:24 PMнякой нещо да е настроивал по githubactions. Почна да прави къмити вместо менбез да съм му разрешевал?https:/github.com/lminnv/app/pull/1200//changes/a68f42f210859f838a4fdced451f750627besoioИли нещо аз не разбирам?0AA0e 20 replies Last reply 18...Nikolay Yankov 3:50PMreplied to a uhread: някои нешо ла е насто..лол. ами предлагам маи ла му заораним лапускам към всички ла вилятNikolav Yankov 9.38 AMЩе се забавя за дейлито. Започнете без Мен.Aneliva Angelova 9:43 AMIДобро утро, няма да успея да вляза влейлито. Пествам ньлжовете.Message & platform-inner-team+ Aa I..•) New TabAl reports promotion pages by nik• JY-9712 | Nuges to expire after on8 Jiminnyu Userpilot Logged-activityJY-20157 add not enough activ XPipelines - jiminny/app+ New Tab©github.com/jimjiminny / app 8<> Code87 Pull requests 31( Agents |© Actions•• Wiki © Security and quality 32 ~ Insights 3 Settings@ On April 24 we'll start using GitHub Copilot interaction data for Al model training unless you opt out. Review this update and manage your preferences in your GitHub account settings.JY-20157 add not enough activities notification #12011 •$1 Open LakyLak wants to merge 2 commits into master from JY-20157-AJ-report-not-send-notification@) Conversation o• Commits 2|- Checks 21E Files changed 13A © All commits +Q Filter files...apo/Console/Commands/Reports/AutomatedReportsCommand.ohp@ -61,21 +61,29 @ public function handle(): intv = Console/Commands/Renorts|Snow = Carbon: : now();E AutomatedReportsCommand...v Jobs/AutomatedReportsE RequestGenerateAskJiminnyR...SendReportNotGeneratedMail...v @ Listeners/AutomatedReports/U….E TrackAutomatedReportGener...v # Mail/ReportsS1sMondav = Snow->1SMonday)S1sr1rstDayUtMonth = Snow->day === 1;ScurrentMonth = Snow->month.// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);$this->logger->info(self::LOG_PREFIX . ' Checking conditions', [+ ReportNotGenerated.ohp |"1SMonday' => S1SMonday,~ E Services/Kiosk/AutomatedRepo…..AskJiminnyReportActivityServ…'isFirstDay0fMonth' => SisFirstDay0fMonth,'currentMonth' => ScurrentMonth.E AutomatedReportsService.php'isQuarterlyMonth' => SisQuarterlyMonth,~E resources/views/emails/reportsreport-not-generated.blade.php/I Process dailv revortsl• F tests/UnitSthis->processReports(AutomatedReportsService::FREQUENCYDAILY):~ Jobs/AutomatedReportsE ReguestGenerateAsk JiminnvR....v = listeners/AutomatedRenorts/U.₴ TrackAutomatedReportGener..v E Services/Kiosk/AutomatedRepo…..E AskJiminnyReportActivityServ....AutomatedReportsServiceActi…./ Process weekly renorts on Mondavcif (SisMondav) {64 +67 +74 +86 +@40@ Daily - Platform - nowQ Type to search100% C4 8• Fri 24 Apr 9:46:13• Checks pending Code • (Preview) -+384 -52 9000C 0 I 13 viewedSubmit review+10 -2 mane [ Viewed0 ...Snow = Carhon:.nowdSisMondav = Snow->1SMonday)"Sisweekend = $now->isWeekend():SisFirstDay0fMonth = Snow->day === 1;ScurrentMonth = Snow->month.SisManualTrigger = $this->option('report-id') !== null;// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);Sthis->loager->info(self::L0G PREFIX . ' Checkina conditions'. [I"isMonday' => SisMonday,'isweekend' => $isWeekend,'isFirstDay0fMonth' => $isFirstDay0fMonth,'currentMonth' => ScurrentMonth.l'isQuarterlyMonth' => SisQuarterlyMonth,/ Process dailv renorts on weekdavs onlv (skio Saturdav/Sundav)...// Manual triggers via --report-id bypass the weekend skip.if (I Sisweekend || SisManualTriager) {Sthis->processReports(AutomatedReportsService::FREQUENCY_DAILY):} else {ISthis->logger->info(self::L0G PREFIX . ' Skipping daily reports on weekend'):/ Process weekly renorts on Mondavslif (SisMonday) {...
|
Slack
|
Aneliya Angelova, Nikolay Yankov, Steliyan Georgie Aneliya Angelova, Nikolay Yankov, Steliyan Georgiev (DM) - Jiminny Inc - 1 new item - Slack...
|
NULL
|
76678
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
1
1
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – TrackAutomatedReportGeneratedEvent.php
|
NULL
|
76679
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
1
1
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – TrackAutomatedReportGeneratedEvent.php
|
NULL
|
76680
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
1
1
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – TrackAutomatedReportGeneratedEvent.php
|
NULL
|
76681
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
1
1
1
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – TrackAutomatedReportGeneratedEvent.php
|
NULL
|
76682
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
6
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Jobs\Crm;
use ChaseConey\LaravelDatadogHelper\Datadog;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Contracts\Services\Crm\FetchRelatedActivityInterface;
use Jiminny\Contracts\Services\Crm\Provider\SalesforceInterface;
use Jiminny\Contracts\Services\Crm\RemoteEntityManipulationInterface;
use Jiminny\Component\Datadog\Constants as DatadogConstants;
use Jiminny\Events\Activities\Crm\ActivityLinkedToCrm;
use Jiminny\Events\Activities\Crm\ActivityLogged;
use Jiminny\Events\Activities\Crm\FollowupScheduled;
use Jiminny\Integrations\PlaybookResolver;
use Jiminny\Jobs\Job;
use Jiminny\Jobs\JobDispatcher;
use Jiminny\Models\Activity;
use Jiminny\Models\Crm\Profile;
use Jiminny\Repositories\ActivityRepository;
use Jiminny\Services\Activity\ActivityProviderRegistry;
use Jiminny\Exceptions\CrmException;
use Jiminny\Notifications\Crm\ActivityLogFailed;
use Jiminny\Services\ResolveTeamCrmConnection;
use Psr\Log\LoggerInterface;
class SaveActivity extends Job implements ShouldQueue
{
use InteractsWithQueue;
use SerializesModels;
private string $activityId;
/**
* Follow-up Task or Event data
*
* @var array
*/
private array $followupData;
/**
* SaveActivity constructor.
*/
public function __construct(Activity $activity, array $followupData = [])
{
$this->activityId = $activity->getUuid();
$this->followupData = $followupData;
$this->onQueue(Constants::QUEUE_CRM_UPDATE);
}
/**
* If they wish to create a follow-up task as well, persist that to CRM.
*/
public function handle(
ResolveTeamCrmConnection $resolveTeamCrmConnection,
LoggerInterface $logger,
ActivityProviderRegistry $activityProvider,
ActivityRepository $activityRepository,
Dispatcher $eventDispatcher,
JobDispatcher $jobDispatcher
): void {
$activity = $activityRepository->findByUuid($this->activityId);
if (! $activity instanceof Activity) {
$logger->error(__METHOD__ . ': activity not found', [
'activityId' => $this->activityId,
]);
return;
}
$user = $activity->getUser();
$crmService = $resolveTeamCrmConnection->resolveForUser($user);
if (! $crmService instanceof RemoteEntityManipulationInterface) {
$logger->info('[SaveActivity] Unsupported CRM provider', [
'provider' => $user->getTeam()->getCrmConfiguration()->getProviderName(),
]);
return;
}
$profile = $user->getProfile();
if ($profile === null) {
$logger->error(
sprintf('[%s] Failed to save activity because user has no profile', $crmService->getDisplayName()),
[
'userId' => $user->getUuid(),
'activityId' => $this->activityId,
],
);
return;
}
$jobDelay = $crmService instanceof SalesforceInterface ? $crmService->getJobDelay() : 0;
$alreadyAssociated = $activity->hasCrmProviderId();
try {
$associatedExistingActivity = null;
if (
! $alreadyAssociated &&
$crmService instanceof FetchRelatedActivityInterface &&
$crmService->getConfiguration()->hasAutoSyncEnabled()
) {
$associatedExistingActivity = $crmService->fetchAndAssociateRelatedActivity($activity);
if ($associatedExistingActivity === null) {
Datadog::increment(DatadogConstants::CRM_AUTO_SYNC, 1, [
'company' => $user->getTeam()->getSlug(),
'provider' => $crmService->getConfiguration()->getProviderName(),
'status' => 'fail',
]);
}
}
if ($associatedExistingActivity !== null) {
Datadog::increment(DatadogConstants::CRM_AUTO_SYNC, 1, [
'company' => $user->getTeam()->getSlug(),
'provider' => $crmService->getConfiguration()->getProviderName(),
'status' => 'success',
]);
$eventDispatcher->dispatch(new ActivityLinkedToCrm($associatedExistingActivity));
}
// Let the service store the activity as it wishes.
$logger->info('CRM LOG saveActivity: ', [
'activityId' => $this->activityId,
]);
$activity = $crmService->saveActivity($activity);
$eventDispatcher->dispatch(new ActivityLogged($activity));
// Create notes and follow-up activity in order to have fresh data for converted lead and log them
// in the correct place.
// Log Notes if necessary.
if (
// link to task won't trigger sidekick notes in this case
! $alreadyAssociated &&
$crmService instanceof SalesforceInterface
&& $profile->getAttribute('log_notes') !== Profile::LOG_NOTE_NONE
) {
$job = (new CreateNotes($activity))
->delay(now()->addMinutes($jobDelay));
$jobDispatcher->dispatch($job);
}
// Create Follow-up Task or Event.
if ($this->followupData) {
$logger->info('Saving followup data: ', [
'activityId' => $this->activityId,
'followupData' => $this->followupData,
]);
// For some reason, the new task can be more sensitive to locking so increase by 1.
$job = (new CreateFollowupActivity($activity, $this->followupData))
->delay(now()->addMinutes($jobDelay));
$jobDispatcher->dispatch($job);
$eventDispatcher->dispatch(new FollowupScheduled($activity));
}
// See if we should log calls to these additional providers.
/** @var Activity\Provider[] $providers */
$providers = $activity->getUser()->getTeam()->getProvidersForAdditionalLogging();
$service = null;
try {
foreach ($providers as $provider) {
/** @var string $providerIdentifier */
$providerIdentifier = $provider->provider;
$service = $activityProvider->get($providerIdentifier);
if (in_array($activity->getType(), $service->supportedActivityChannels(), true)) {
$service->setTeam($user->getTeam());
$service->setSocialAccount(null);
$service->createCall($activity);
}
}
} catch (\Exception $exception) {
$logger->error(
sprintf(
'[%s] Failed to save activity to %s',
$crmService->getDisplayName(),
$service?->getDisplayName() ?? '',
),
[
'userId' => $user->getUuid(),
'activityId' => $activity->getUuid(),
'reason' => $exception->getMessage(),
],
);
}
} catch (CrmException $e) {
$playbookResolver = app(PlaybookResolver::class);
$playbook = $playbookResolver->resolvePlaybookByUser($user);
if ($playbook) {
// If this fails, raise a notification to user.
$user->notify(
new ActivityLogFailed($playbook->getActivityType(), $activity, $e->getMessage())
);
}
$logger->error(sprintf('[%s] Failed to save activity', $crmService->getDisplayName()), [
'activityId' => $this->activityId,
'reason' => $e->getMessage(),
]);
Datadog::increment('jiminny.crm.activity.log-failure', 1, ['company' => $user->getTeam()->slug]);
}
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – SaveActivity.php
|
NULL
|
76683
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
6
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Jobs\Crm;
use ChaseConey\LaravelDatadogHelper\Datadog;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Contracts\Services\Crm\FetchRelatedActivityInterface;
use Jiminny\Contracts\Services\Crm\Provider\SalesforceInterface;
use Jiminny\Contracts\Services\Crm\RemoteEntityManipulationInterface;
use Jiminny\Component\Datadog\Constants as DatadogConstants;
use Jiminny\Events\Activities\Crm\ActivityLinkedToCrm;
use Jiminny\Events\Activities\Crm\ActivityLogged;
use Jiminny\Events\Activities\Crm\FollowupScheduled;
use Jiminny\Integrations\PlaybookResolver;
use Jiminny\Jobs\Job;
use Jiminny\Jobs\JobDispatcher;
use Jiminny\Models\Activity;
use Jiminny\Models\Crm\Profile;
use Jiminny\Repositories\ActivityRepository;
use Jiminny\Services\Activity\ActivityProviderRegistry;
use Jiminny\Exceptions\CrmException;
use Jiminny\Notifications\Crm\ActivityLogFailed;
use Jiminny\Services\ResolveTeamCrmConnection;
use Psr\Log\LoggerInterface;
class SaveActivity extends Job implements ShouldQueue
{
use InteractsWithQueue;
use SerializesModels;
private string $activityId;
/**
* Follow-up Task or Event data
*
* @var array
*/
private array $followupData;
/**
* SaveActivity constructor.
*/
public function __construct(Activity $activity, array $followupData = [])
{
$this->activityId = $activity->getUuid();
$this->followupData = $followupData;
$this->onQueue(Constants::QUEUE_CRM_UPDATE);
}
/**
* If they wish to create a follow-up task as well, persist that to CRM.
*/
public function handle(
ResolveTeamCrmConnection $resolveTeamCrmConnection,
LoggerInterface $logger,
ActivityProviderRegistry $activityProvider,
ActivityRepository $activityRepository,
Dispatcher $eventDispatcher,
JobDispatcher $jobDispatcher
): void {
$activity = $activityRepository->findByUuid($this->activityId);
if (! $activity instanceof Activity) {
$logger->error(__METHOD__ . ': activity not found', [
'activityId' => $this->activityId,
]);
return;
}
$user = $activity->getUser();
$crmService = $resolveTeamCrmConnection->resolveForUser($user);
if (! $crmService instanceof RemoteEntityManipulationInterface) {
$logger->info('[SaveActivity] Unsupported CRM provider', [
'provider' => $user->getTeam()->getCrmConfiguration()->getProviderName(),
]);
return;
}
$profile = $user->getProfile();
if ($profile === null) {
$logger->error(
sprintf('[%s] Failed to save activity because user has no profile', $crmService->getDisplayName()),
[
'userId' => $user->getUuid(),
'activityId' => $this->activityId,
],
);
return;
}
$jobDelay = $crmService instanceof SalesforceInterface ? $crmService->getJobDelay() : 0;
$alreadyAssociated = $activity->hasCrmProviderId();
try {
$associatedExistingActivity = null;
if (
! $alreadyAssociated &&
$crmService instanceof FetchRelatedActivityInterface &&
$crmService->getConfiguration()->hasAutoSyncEnabled()
) {
$associatedExistingActivity = $crmService->fetchAndAssociateRelatedActivity($activity);
if ($associatedExistingActivity === null) {
Datadog::increment(DatadogConstants::CRM_AUTO_SYNC, 1, [
'company' => $user->getTeam()->getSlug(),
'provider' => $crmService->getConfiguration()->getProviderName(),
'status' => 'fail',
]);
}
}
if ($associatedExistingActivity !== null) {
Datadog::increment(DatadogConstants::CRM_AUTO_SYNC, 1, [
'company' => $user->getTeam()->getSlug(),
'provider' => $crmService->getConfiguration()->getProviderName(),
'status' => 'success',
]);
$eventDispatcher->dispatch(new ActivityLinkedToCrm($associatedExistingActivity));
}
// Let the service store the activity as it wishes.
$logger->info('CRM LOG saveActivity: ', [
'activityId' => $this->activityId,
]);
$activity = $crmService->saveActivity($activity);
$eventDispatcher->dispatch(new ActivityLogged($activity));
// Create notes and follow-up activity in order to have fresh data for converted lead and log them
// in the correct place.
// Log Notes if necessary.
if (
// link to task won't trigger sidekick notes in this case
! $alreadyAssociated &&
$crmService instanceof SalesforceInterface
&& $profile->getAttribute('log_notes') !== Profile::LOG_NOTE_NONE
) {
$job = (new CreateNotes($activity))
->delay(now()->addMinutes($jobDelay));
$jobDispatcher->dispatch($job);
}
// Create Follow-up Task or Event.
if ($this->followupData) {
$logger->info('Saving followup data: ', [
'activityId' => $this->activityId,
'followupData' => $this->followupData,
]);
// For some reason, the new task can be more sensitive to locking so increase by 1.
$job = (new CreateFollowupActivity($activity, $this->followupData))
->delay(now()->addMinutes($jobDelay));
$jobDispatcher->dispatch($job);
$eventDispatcher->dispatch(new FollowupScheduled($activity));
}
// See if we should log calls to these additional providers.
/** @var Activity\Provider[] $providers */
$providers = $activity->getUser()->getTeam()->getProvidersForAdditionalLogging();
$service = null;
try {
foreach ($providers as $provider) {
/** @var string $providerIdentifier */
$providerIdentifier = $provider->provider;
$service = $activityProvider->get($providerIdentifier);
if (in_array($activity->getType(), $service->supportedActivityChannels(), true)) {
$service->setTeam($user->getTeam());
$service->setSocialAccount(null);
$service->createCall($activity);
}
}
} catch (\Exception $exception) {
$logger->error(
sprintf(
'[%s] Failed to save activity to %s',
$crmService->getDisplayName(),
$service?->getDisplayName() ?? '',
),
[
'userId' => $user->getUuid(),
'activityId' => $activity->getUuid(),
'reason' => $exception->getMessage(),
],
);
}
} catch (CrmException $e) {
$playbookResolver = app(PlaybookResolver::class);
$playbook = $playbookResolver->resolvePlaybookByUser($user);
if ($playbook) {
// If this fails, raise a notification to user.
$user->notify(
new ActivityLogFailed($playbook->getActivityType(), $activity, $e->getMessage())
);
}
$logger->error(sprintf('[%s] Failed to save activity', $crmService->getDisplayName()), [
'activityId' => $this->activityId,
'reason' => $e->getMessage(),
]);
Datadog::increment('jiminny.crm.activity.log-failure', 1, ['company' => $user->getTeam()->slug]);
}
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – SaveActivity.php
|
NULL
|
76684
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
2
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Http\Controllers\API;
use Carbon\Carbon;
use ChaseConey\LaravelDatadogHelper\Datadog;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Jiminny\Component\ActivityAnalytics;
use Jiminny\Component\ActivitySearch;
use Jiminny\Component\ActivitySearch\FilterDefinitionCollection;
use Jiminny\Component\PlaybackPage\Comments\Services\ActivityCommentService;
use Jiminny\Component\Queue\Constants;
use Jiminny\Contracts\Nudge\NudgeFactoryInterface;
use Jiminny\Contracts\Playlist\PlaylistTrackFactoryInterface;
use Jiminny\Contracts\Repositories\PlaylistActivityRepository;
use Jiminny\Contracts\Services\Crm\ServiceInterface;
use Jiminny\Enums\TeamSetting;
use Jiminny\Events\Activities\AiAutomation\ActivityProspectAdded;
use Jiminny\Events\Activities\Coaching\Coached;
use Jiminny\Contracts\Services\Crm\SupportsObjectTypeParseInterface;
use Jiminny\Exceptions\LogicException;
use Jiminny\Exceptions\SocialAccountTokenInvalidException;
use Jiminny\Http\Controllers\API\BaseController as Controller;
use Jiminny\Http\Controllers\CommentContextInterface;
use Jiminny\Http\Responses\Api\AbstractResponse;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Http\Serializers\JsonSerializer;
use Jiminny\Http\Transformers\ActivityCommentTransformer;
use Jiminny\Http\Transformers\ActivityTopicTriggerTransformer;
use Jiminny\Http\Transformers\ActivityTransformer;
use Jiminny\Http\Transformers\AvailabilityNotificationTransformer;
use Jiminny\Http\Transformers\CoachingFeedbackTransformer;
use Jiminny\Http\Transformers\CoachingSectionsTransformer;
use Jiminny\Http\Transformers\SearchTransformer;
use Jiminny\Http\Transformers\StatsTransformer;
use Jiminny\Jobs\Crm\SaveActivity;
use Jiminny\Jobs\Crm\UpdateStage;
use Jiminny\Jobs\Telephony\StartRecording;
use Jiminny\Jobs\Telephony\StopRecording;
use Jiminny\Jobs\Telephony\ToggleRecording;
use Jiminny\Models\Account;
use Jiminny\Models\Activity;
use Jiminny\Models\Activity\CoachRequest;
use Jiminny\Models\Activity\Comment;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\Activity\SearchFilter;
use Jiminny\Models\Activity\Share;
use Jiminny\Models\CoachingFeedback;
use Jiminny\Models\CoachingSection;
use Jiminny\Models\CoachingSectionCriterion;
use Jiminny\Models\CoachingSectionFeedback;
use Jiminny\Models\Contact;
use Jiminny\Models\Crm\Field;
use Jiminny\Models\Crm\FieldData;
use Jiminny\Models\Crm\Layout;
use Jiminny\Models\Crm\LayoutEntity;
use Jiminny\Models\Feature\FeatureEnum;
use Jiminny\Models\LanguageDialect;
use Jiminny\Models\Lead;
use Jiminny\Models\Nudge;
use Jiminny\Models\PlaybookCategory;
use Jiminny\Models\Playlist;
use Jiminny\Models\Stage;
use Jiminny\Models\Team;
use Jiminny\Models\Track;
use Jiminny\Models\User;
use Jiminny\Repositories\AjReportsRepository;
use Jiminny\Repositories\CoachingFeedbackRepository;
use Jiminny\Repositories\ElasticActivityRepository;
use Jiminny\Repositories\TeamRepository;
use Jiminny\Rules\CrmReference;
use Jiminny\Rules\MultidimensionalArrayMaxCharRule;
use Jiminny\Services\ActivityService;
use Jiminny\Services\Crm\ProviderRegistry;
use Jiminny\Services\PlaybackService;
use Jiminny\Services\UserService;
use Jiminny\VO\Repository\OnDemandActivitySearch\Criteria;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Sentry;
use Symfony\Component\HttpFoundation;
final class ActivityController extends Controller implements CommentContextInterface
{
// Number of minutes to look back on activities. i.e. a timeout on activity duration.
private const LOOK_BACK = 180;
public function __construct(
private ProviderRegistry $providerRegistry,
private ActivityService $activityService,
Response $response,
private UserService $userService,
private ActivitySearch\Service\ActivitySearch $activitySearch,
private NudgeFactoryInterface $nudgeFactory,
private ActivityCommentService $activityCommentService,
private LoggerInterface $logger,
private readonly CoachingFeedbackRepository $coachingFeedbackRepository,
private readonly TeamRepository $teamRepository,
private readonly AjReportsRepository $ajReportsRepository,
) {
parent::__construct($response);
}
public static function getCommentImplementation(): string
{
return Comment::class;
}
public function delete()
{
$this->request->validate([
'*' => 'uuid:activities',
]);
$deletedIds = [];
foreach ($this->request->all() as $activityId) {
$activity = Activity::idOrUuId($activityId);
try {
if ($this->authorize('delete', $activity)) {
$activity->delete();
$deletedIds[] = $activityId;
\Log::info('Soft deleted activity ' . $activity->id_string . ' by user ' . $this->getUser()->id);
}
} catch (AuthorizationException $authorizationException) {
// They didn't have permission.
}
}
return $this->response->withArray($deletedIds);
}
public function update(Request $request, Activity $activity)
{
$this->authorize('updateMetadata', $activity);
$request->validate([
'title' => 'string|max:250',
'category_id' => 'uuid:playbook_categories',
'language' => [
new In(
LanguageDialect::query()
->with('language')
->cursor()
->map(static function (LanguageDialect $languageDialect): string {
return $languageDialect->getLanguageLocale();
})
->all()
),
],
]);
if ($request->has('title')) {
$activity->title = $request->input('title');
}
if ($request->has('category_id')) {
$category = PlaybookCategory::uuid($request->input('category_id'));
if ($category->playbook->team_id !== $request->user()->team_id) {
return $this->response->errorNotFound('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
if ($request->has('language')) {
if (! $activity->isInProgress()) {
return $this->response->withError(
'Activity language can only be set while the meeting is in progress.',
400
);
}
$activity->setLanguageCode($request->input('language'));
}
$activity->save();
return $this->response->withOk();
}
// XXX: This should be merged with the update method.
/**
* @param Activity $activity
*
* @throws AuthorizationException
* @throws SocialAccountTokenInvalidException
*
* @return mixed
*/
public function summarize(Activity $activity): mixed
{
$this->logger->info('[Log Activity] Summarizing activity ', [
'activityId' => $activity->getUuid(),
'payload' => $this->request->all(),
]);
$this->authorize('update', $activity);
$this->logger->info('[Log Activity] Validating summary');
// Validate the payload.
$this->validateSummary($activity);
// All objects must belong to this team.
/** @var User $user */
$user = $this->request->user();
$team = $user->getTeam();
$crmService = $this->providerRegistry->get($team->crm->provider);
try {
$crmUser = $user;
if ($user->isCrmRequired() === false) {
$crmUser = $team->owner;
}
$crmService->setUser($crmUser);
} catch (SocialAccountTokenInvalidException $accountTokenInvalidException) {
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($accountTokenInvalidException->getMessage());
}
$rawEntities = $this->request->input('entities');
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid(
$this->request->input('layout_id')
);
// Delay execution of CRM jobs to avoid locking issues.
$jobDelay = 0;
// If we have arrived from a notification, mark it as read.
$notificationId = $this->request->input('nId');
if ($notificationId) {
$notification = $user->unreadNotifications->where('id', $notificationId)->first();
if ($notification) {
$notification->markAsRead();
}
}
$title = $this->request->input('title');
$prospects = $this->request->input('prospects');
$opportunityId = $this->request->input('opportunity_id');
$stageId = $this->request->input('stage_id');
$categoryId = $this->request->input('category_id');
$summary = $this->request->input('summary');
$crmProviderId = $this->request->input('crm_id');
$isInternal = $this->request->input('is_internal') ?? false;
$lead = null;
$category = null;
$account = null;
$contact = null;
$opportunity = null;
$stage = null;
$callStage = null;
foreach ($prospects as $prospectData) {
$objectId = $prospectData['id'];
if ($objectId === null) {
continue;
}
$objectType = $prospectData['type'];
$this->logger->info('debug', ['prospect_data' => $prospectData]);
try {
if ($objectType === null) {
$this->logger->info('no object type');
if ($crmService instanceof SupportsObjectTypeParseInterface) {
$objectType = $crmService->parseObjectType($objectId);
}
}
switch ($objectType) {
case 'lead':
$this->logger->info('Processing lead');
/** @var Lead|null $lead */
$lead = $team->crm->leads()->where('crm_provider_id', $objectId)->first();
// Lead does not exist locally, import it.
if ($lead === null) {
$this->logger->info('Lead does not exist locally');
/** @var Lead $lead */
$lead = $crmService->syncLead($objectId);
}
$this->logger->info('Lead found', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
if ($stageId === null) {
$this->logger->info('Stage ID is null');
// If it was not provided, just assume it is the current stage.
$callStage = $lead->stage;
break;
}
$this->logger->info('Looking for stage');
// Determine if they have changed the stage.
/** @var Stage $stage */
$stage = $team->crm->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_LEAD)
->firstOrFail();
$this->logger->info('Stage found', ['stageId' => $stage->id, 'lead_stage' => $lead->stage_id]);
if ($lead->stage_id && $lead->stage_id !== $stage->id) {
$this->logger->info('Stage has changed');
// Storage current stage on activity.
$callStage = $lead->stage;
// The stage has changed, update in remote CRM.
dispatch(new UpdateStage($activity, $lead, $callStage, $stage));
$this->logger->info(
sprintf(
'[%s] User changing lead stage from %s to %s',
$crmService->getDisplayName(),
$callStage->getName(),
$stage->getName()
),
[
'user' => $user->getUuid(),
'lead' => $lead->getUuid(),
]
);
} else {
$this->logger->info('Stage has not changed');
// Stage remains as current.
$callStage = $stage;
}
break;
case 'account':
$this->logger->info('Processing account');
// If the object is not a lead, it should be an account.
$account = $team->crm->accounts()->where('crm_provider_id', $objectId)->first();
// Account does not exist locally, import it.
if ($account === null) {
$this->logger->info('Account does not exist locally');
$account = $crmService->syncAccount($objectId);
}
$this->logger->info('Account found', ['accountId' => $account->id]);
break;
case 'contact':
$this->logger->info('processing contact');
$contact = $team->crm->contacts()->where('crm_provider_id', $objectId)->first();
// Contact does not exist locally, import it.
if (! $contact instanceof Contact) {
$this->logger->info('contact does not exist locally');
$contact = $crmService->syncContact($objectId);
}
$this->logger->info('resolving account');
$account = $this->resolveAccount($team, $contact, $crmService, $prospects);
break;
}
// If they have specified an opportunity, retrieve this with stage.
if ($opportunityId) {
$this->logger->info('opportunity id is set');
$opportunity = $team->crm->opportunities()->where('crm_provider_id', $opportunityId)->first();
// Opportunity does not exist locally, import it.
if ($opportunity === null) {
$this->logger->info('opportunity does not exist locally');
$opportunity = $crmService->syncOpportunity($opportunityId);
}
if ($stageId === null) {
$this->logger->info('stage id is null');
// If it was not provided, just assume it is the current stage.
$callStage = $opportunity->stage ?? null;
} else {
$this->logger->info('looking for stage');
/** @var ?Stage $opportunityStage */
$opportunityStage = $team->crm
->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_OPPORTUNITY)
->first();
// There is a chance we still cannot import this opportunity.
if ($opportunityStage !== null && $opportunity !== null && $opportunity->stage_id !== $opportunityStage->id) {
$this->logger->info('opportunity stage has changed');
// Storage current stage on activity.
$callStage = $opportunity->stage;
dispatch(new UpdateStage($activity, $opportunity, $callStage, $opportunityStage));
$this->logger->info(
sprintf(
'[%s] User changing opportunity stage from %s to %s',
$crmService->getDisplayName(),
$callStage->name,
$opportunityStage->name
),
[
'userId' => $user->id_string,
'opportunityId' => $opportunity->id_string,
]
);
} else {
$this->logger->info('opportunity stage has not changed');
// Stage remains as current.
$callStage = $opportunityStage;
}
}
}
if ($crmProviderId) {
// Cast $crmProviderId to string otherwise it won't use database index for some records
$linkedActivity = Activity::where('crm_provider_id', (string) $crmProviderId)->first();
// Check if this activity has already been assigned to a different activity.
if ($linkedActivity && $linkedActivity->id !== $activity->id) {
throw new InvalidArgumentException(
'Sorry, the linked task has already been logged under a different call. '
. 'Please choose another linked task.'
);
}
}
} catch (InvalidArgumentException $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($exception->getMessage());
} catch (Exception $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorInternalError(
'Sorry, an error occurred. Please try again or reach out to support if the problem continues.'
);
}
}
if ($categoryId) {
$category = PlaybookCategory::uuid($categoryId);
if ($category->playbook->team_id !== $team->id) {
throw new InvalidArgumentException('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
$this->logger->info('Prospect data', [
'lead_id' => $lead?->getId(),
'account_id' => $account?->getId(),
'contact_id' => $contact?->getId(),
'opportunity_id' => $opportunity?->getId(),
'stage_id' => $stage?->getId(),
]);
if ($title) {
$activity->title = $title;
}
if ($summary) {
$activity->summary = $summary;
}
if ($crmProviderId) {
$activity->crm_provider_id = $crmProviderId;
}
if ($callStage) {
$this->logger->info('Setting stage id', ['stageId' => $callStage->id]);
$activity->stage_id = $callStage->id;
}
if ($lead) {
$this->logger->info('Setting lead id', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
// If we are changed from an account > lead, unset the account data.
$this->logger->info('Unsetting account id, opportunity id, contact id, value');
$activity->account_id = null;
$activity->opportunity_id = null;
$activity->contact_id = null;
$activity->value = null;
}
if ($account) {
$this->logger->info('Setting account id', ['accountId' => $account->id]);
$activity->account_id = $account->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('unsetting lead id');
$activity->lead_id = null;
// Unset the contact if switching different accounts. Will be set up below if still applicable.
if (! $team->hasFeature(FeatureEnum::LINK_ACTIVITY_TO_MULTIPLE_PROSPECTS) || empty($contact)) {
$this->logger->info('Unsetting contact id');
$activity->contact_id = null;
}
}
if ($opportunity) {
$this->logger->info('setting opportunity id', ['opportunityId' => $opportunity->id]);
$this->logger->info('unsetting lead id');
$activity->opportunity_id = $opportunity->id;
$activity->value = $opportunity->value;
// If we are changed from an lead > account, unset the lead data.
$activity->lead_id = null;
}
if ($contact) {
$this->logger->info('setting contact id', ['contactId' => $contact->id]);
$activity->contact_id = $contact->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('Unsetting lead id');
$activity->lead_id = null;
}
$activity->is_internal = $isInternal;
$activity->save();
$activity->refresh();
$this->logger->notice('Activity saved', [
'activity_id' => $activity->getId(),
'lead_id' => $activity->lead_id,
'account_id' => $activity->account_id,
'contact_id' => $activity->contact_id,
'opportunity_id' => $activity->opportunity_id,
'stage_id' => $activity->stage_id,
'crm_provider_id' => $activity->getCrmProviderId(),
]);
// Store entities as field data on the activity.
$updatedData = $this->storeEntities($crmService, $activity, $layout, $rawEntities);
if ($activity->isLoggable()) {
// Follow-up Task or Event data.
$followupData = $this->fetchFollowupEntities($crmService, $layout, $rawEntities);
$this->logger->info('CRM LOG manual log triggered', [
'activityId' => $activity->getUuid(),
'followupData' => $followupData,
'userId' => $user->getUuid(),
]);
// Store data in the CRM.
// ++add check for crm_required
$job = new SaveActivity($activity, $followupData);
if ($updatedData) {
$job->delay(Carbon::now()->addMinutes($jobDelay));
}
dispatch($job);
// Manually dispatch log for Opportunity or Prospect added
if ($activity->hasOpportunity() || $activity->hasProspect()) {
event(new ActivityProspectAdded(
activity: $activity,
eventSource: 'manually-log-crm-data'
));
}
}
return $this->response->withOk();
}
/**
* Extract any activity data to be upserted in the Lead/Opportunity/Task etc in the CRM.
*
* @param ServiceInterface $service
* @param Activity $activity
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function storeEntities(ServiceInterface $service, Activity $activity, Layout $layout, array $entities): array
{
$updatedData = [];
$existingData = $activity->data()->get();
// We need to delete any existing data to overwrite with latest values.
$activity->data()->delete();
$layoutEntities = $layout->entities()
->with('field', 'parent')
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->get();
/** @var LayoutEntity $entity */
foreach ($layoutEntities as $entity) {
// If the user has provided a value for this entity
if (array_key_exists($entity->id_string, $entities)) {
$value = $entities[$entity->id_string];
// Convert raw data into values that the CRM can consume.
if ($value) {
$value = $service->normalizeValue($entity->field->type, $value);
}
// Check the field is part of the activity-summary section.
if ($entity->parent && $entity->parent->label === 'activity-summary' && $value) {
// This is the internal database ID, not the external CRM ID.
$objectId = null;
switch ($entity->field->object_type) {
case Field::OBJECT_ACCOUNT:
$objectId = $activity->account_id;
break;
case Field::OBJECT_CONTACT:
$objectId = $activity->contact_id;
break;
case Field::OBJECT_OPPORTUNITY:
$objectId = $activity->opportunity_id;
break;
case Field::OBJECT_LEAD:
$objectId = $activity->lead_id;
break;
case Field::OBJECT_TASK:
case Field::OBJECT_EVENT:
$objectId = $activity->id;
break;
}
if ($objectId) {
/** @var FieldData $data */
$data = $activity->data()->create([
'crm_layout_entity_id' => $entity->id,
'crm_field_id' => $entity->crm_field_id,
'object_type' => $entity->field->object_type,
'object_id' => $objectId,
'value' => $value,
]);
// Never send read-only field data to the CRM.
if ($entity->read_only === false && $entity->is_visible) {
$existingValue = $existingData
->where('crm_layout_entity_id', $entity->id)
->where('crm_field_id', $entity->crm_field_id)
->where('object_type', $entity->field->object_type)
->where('object_id', $objectId)
->first();
// If the field was actually changed, we need to reflect this in the CRM too.
if ($existingValue === null || $existingValue->value !== $value) {
$updatedData[] = $data->id;
}
}
}
}
}
}
return $updatedData;
}
/**
* Extract any followup data to be dispatched in a job to create a new Task/Event in the CRM.
*
* @param ServiceInterface $crmService
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function fetchFollowupEntities(ServiceInterface $crmService, Layout $layout, array $entities): array
{
$fieldData = [];
foreach ($entities as $entityId => $value) {
// Only bother with fields that have a value.
if ($value) {
// Extract the entity from the UUID. Check the field is valid and part of the follow-up section.
$entity = $layout->entities()
->uuid($entityId, false)
->whereHas('parent', function ($query) {
$query->where('label', 'follow-up');
})
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->first();
if ($entity) {
// Convert raw data into values that the CRM can consume.
$value = $crmService->normalizeValue($entity->field->type, $value);
// Add the field and value to the payload.
$fieldData += [
$entity->field->crm_provider_id => $value,
];
}
}
}
return $fieldData;
}
/**
* @param Activity $activity
*/
private function validateSummary(Activity $activity): void
{
$team = $activity->user->team;
$crmProvider = $team->crm->provider;
$attributes = [];
$rules = [
'layout_id' => 'required|uuid:crm_layouts,crm_configuration_id,' . $team->crm_id,
'title' => 'string|max:250',
'prospects' => 'required|array',
'opportunity_id' => new CrmReference($crmProvider),
'category_id' => 'uuid:playbook_categories|required_unless:is_internal,true',
'stage_id' => 'uuid:stages,team_id,' . $team->id, // Todo: move to proper validator
'summary' => 'max:50000',
'nId' => 'exists:notifications,id',
'crm_id' => new CrmReference($crmProvider),
'entities' => 'array',
'is_internal' => 'boolean',
];
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid($this->request->input('layout_id'));
// Only validate fields, not headers etc. If not loggable, we don't care about follow-up section.
$entities = $layout->entities()
->where('read_only', 0)
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->whereHas('parent', function ($query) use ($activity) {
if ($activity->isLoggable() === false) {
$query->where('label', '<>', 'follow-up');
}
});
$isInternal = $this->request->input('is_internal', false);
foreach ($entities->get() as $entity) {
$rules += $this->buildFieldValidator($entity, $isInternal);
$attributes += $this->buildFieldMessage($entity);
}
$this->request->validate($rules, [], $attributes);
}
private function buildFieldValidator(LayoutEntity $entity, bool $isInternal): array
{
return [
'entities.' . $entity->id_string => $entity->getValidator($isInternal),
];
}
/**
* @param LayoutEntity $entity
*
* @return array
*/
private function buildFieldMessage(LayoutEntity $entity): array
{
$label = $entity->label;
if ($label === null) {
$label = $entity->field->label;
}
return [
'entities.' . $entity->id_string => $label,
];
}
public function search(Request $request, ElasticActivityRepository $repository): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->debugLog(
$user,
'User extracted from request',
['user' => $user->getId(), 'tz' => $user->getTimezone()]
);
$searchCriteria = Criteria::createFromRequest($request->all(), $user->getTimezone());
$this->debugLog(
$user,
'ActivitySearch criteria built',
['searchCriteria' => $searchCriteria]
);
$filterSet = $this->activitySearch->getHomepageFilterSet($searchCriteria, $user);
$this->debugLog($user, 'FilterSet built', ['filterSet' => $filterSet]);
$this->validateSearch($request, $filterSet);
$this->debugLog($user, 'Request validated');
$searchResponse = $repository->onDemandSearch($user, $searchCriteria, $filterSet);
/** @var Collection<Activity> $activities */
$activities = $searchResponse['results'];
$this->debugLog($user, 'Activities ES response extracted');
$hideInternalMeetingsSetting = $this->teamRepository->getTeamSettingByTeamId(
$user->getTeamId(),
TeamSetting::HIDE_INTERNAL_SCHEDULED_MEETINGS->name(),
);
if ($hideInternalMeetingsSetting?->getValue() === '1') {
$activities = $activities->filter(function (Activity $activity) {
if ($activity->is_internal && empty($activity->actual_start_time)) {
return false;
}
return true;
});
}
$this->debugLog($user, 'Internal meetings (?!) filtered');
$this->response->getManager()
->parseIncludes([
'category',
'organizer.group',
'prospect',
'stage',
'opportunity',
'stats',
'scorecards',
'masterTrack',
'activeParticipants',
'notification',
])
->setSerializer(new JsonSerializer());
$transformerExcludes = $this->request->input('exclude');
if ($transformerExcludes) {
$this->response->getManager()->parseExcludes($transformerExcludes);
}
$this->debugLog($user, 'Response Manager (?!) applied');
$transformer = new ActivityTransformer();
$transformer->setConsumer($user);
$this->debugLog($user, 'Activity Transformer added');
$resource = new \League\Fractal\Resource\Collection($activities, $transformer);
$page = $searchCriteria->getPageNumber();
$this->debugLog($user, 'Search criteria page number called', ['page' => $page]);
$histogram = array_pluck(array_get($searchResponse, 'histogram.buckets', []), 'doc_count', 'key_as_string');
$this->debugLog($user, 'Histogram generated. Response is ready.', ['histogram' => $histogram]);
return $this->response->withArray([
'pagination' => [
'total' => $searchResponse['totalHits'],
'current' => $page,
'prev' => max($page - 1, 1),
'next' => $page + 1,
],
'results' => $this->response->getManager()->createData($resource)->toArray(),
'histogram' => $histogram,
]);
}
private function debugLog(User $user, string $logMessage, ?array $context = []): void
{
// Debug for Learning People Only
if ($user->getTeamId() !== 260) {
return;
}
Log::notice(
sprintf('[activity-search-controller] %s', $logMessage),
$context
);
}
/** @throws ValidationException */
private function validateSearch(Request $request, FilterDefinitionCollection $filterSet, ?string $prefix = null): void
{
$rules = [
'exclude' => 'array',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
];
if ($prefix !== null && mb_strpos($prefix, '.') !== false) {
$rules[rtrim($prefix, '.')] = sprintf(
'required|array|max:%d',
$filterSet->count()
);
}
$validationRules = $filterSet->getValidationRules($prefix)
->merge($rules)
->all();
$request->validate($validationRules);
}
public function createActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$search = $this->updateOrCreateActivitySearch($request);
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function updateActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('update', $search);
$this->updateOrCreateActivitySearch($request, $search);
return $this->response->withOk();
}
private function storeNamedSearchFilters(
Collection $request,
Search $search,
FilterDefinitionCollection $filterSet,
?string $prefix = null,
): self {
$arrayTypeProperties = $filterSet
->getPropertyTypes([
FilterDefinitionCollection::PROPERTY_TYPE_ARRAY,
])
->all();
$supportedRequestProperties = $filterSet->getSupportedRequestProperties($prefix);
foreach ($supportedRequestProperties as $requestPropertyName) {
if (! array_has($request, $requestPropertyName)) {
continue;
}
/** @var string|string[] $propertyValue */
$propertyValue = array_get($request, $requestPropertyName);
$propertyName = $prefix === null
? $requestPropertyName
: mb_substr($requestPropertyName, mb_strlen($prefix));
$isArrayType = array_has($arrayTypeProperties, $propertyName);
if (! $isArrayType) {
/** @var string $requestPropertyValue */
$search->filters()->updateOrCreate(
[
'filter' => $propertyName,
],
[
'value' => $propertyValue,
]
);
continue;
}
/** @var string[] $requestPropertyValue */
/** @var SearchFilter[]|Collection $existingFilterValues */
$existingFilterValuesKeyed = $search->filters()
->where('filter', $propertyName)
->get()
->keyBy('id');
// Iterate over values provided as request parameters
foreach ($propertyValue as $value) {
/** @var SearchFilter|null $valueFilter */
$valueFilter = $search->filters()
->where(
[
'filter' => $propertyName,
'value' => $value,
]
)
->first();
if ($valueFilter !== null) {
// Remove filter value pair from list to be deleted
$existingFilterValuesKeyed->forget($valueFilter->id);
} else {
// Add new filter/value pair
$search->filters()->updateOrCreate([
'filter' => $propertyName,
'value' => $value,
]);
}
}
// Delete filter value pairs for this filter that no longer exist in request parameters
foreach ($existingFilterValuesKeyed as $existingFilter) {
$existingFilter->delete();
}
}
/** @var Collection<int, SearchFilter> $filtersKeyed */
$filtersKeyed = $search->filters()->get()->keyBy('filter');
// wipe removed filters from this search
foreach ($filtersKeyed as $filterName => $filter) {
if (array_has($request, $prefix . $filterName)) {
continue;
}
// Remove all filter values for this filter
$search->filters()->where('filter', $filterName)->delete();
}
return $this;
}
/**
* @throws AuthorizationException
*/
public function fetchActivitySearch(
Search $search,
Request $request,
SearchTransformer $searchTransformer,
): JsonResponse {
$this->authorize('view', $search);
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function listActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withCollection(
$user->searches()->get(),
$searchTransformer
->withConsumer($user)
);
}
/**
* Deletes a saved search
*
* @param Request $request
* @param Search $search
*
* @throws Exception
*
* @return JsonResponse
*/
public function deleteActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('delete', $search);
$ajReportCount = $this->ajReportsRepository->countActiveReportsBySavedSearch($search->getId());
if ($ajReportCount > 0) {
return $this->response->errorWrongArgs(
"This saved search is used by {$ajReportCount} active AJ report(s). "
. 'Please remove or update those reports before deleting this saved search.'
);
}
$search->filters()->delete();
$search->delete();
return $this->response->withOk();
}
public function live(Request $request, ElasticActivityRepository $repository): JsonResponse
{
$user = $this->getUserFromRequest($request);
$this->request->validate([
'sort_direction' => 'in:asc,desc',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
]);
$activities = $repository->getLiveCoachingEligibleActivities(
user: $user,
lookBackMinutes: self::LOOK_BACK,
limit: (int) $this->request->input('limit', 25),
page: (int) $this->request->input('page', 1),
sortBy: ['actual_start_time', 'scheduled_start_time'],
sortDirection: (string) $this->request->input('sort_direction', 'asc'),
);
$this->response
->getManager()
->parseIncludes(['organizer.group', 'prospect'])
->setSerializer(new JsonSerializer());
return $this->response->withCollection($activities, new ActivityTransformer());
}
/**
* @param Activity $activity
*
* @throws AuthorizationException
*
* @return mixed
*/
public function show(Activity $activity, ActivityService $activityService): JsonResponse
{
$this->authorize('show', $activity);
$user = $activity->getUser();
$team = $user->getTeam();
// Sync the opportunity with the latest data if possible.
if ($activity->opportunity_id) {
try {
$crmService = $this->providerRegistry->get($team->crm->provider);
if (! $user->isCrmRequired()) {
$crmService->setUser($team->getOwner());
} else {
$crmService->setUser($user);
}
$crmService->syncOpportunity($activity->opportunity->crm_provider_id);
} catch (Exception $exception) {
// Move on.
}
}
$activityData = $activityService->getActivityData($this->request->user(), $activity);
return response()->json($activityData);
}
public function createRecording(Activity $activity)
{
$this->authorize('record', $activity);
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Tell Twilio to start recording this activity.
if ($activity->recording_state === Activity::RECORDING_OFF) {
$job = (new StartRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withCreated();
}
return $this->response->errorGone('Activity is already recording.');
}
public function updateRecording(Request $request, Activity $activity)
{
$this->authorize('record', $activity);
$request->validate([
'preference' => 'boolean',
'state' => [
'string',
Rule::in([
Activity::RECORDING_IN_PROGRESS,
Activity::RECORDING_PAUSED,
]),
],
]);
if ($request->has('state')) {
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Toggle the recording state between paused and resumed.
if (! $activity->isRecordingState(Activity::RECORDING_OFF)) {
$job = (new ToggleRecording($activity, $request->input('state')))
->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Recording is not toggleable.');
}
if ($request->has('preference')) {
$activity->update([
'recording_preference' => $request->input('preference') ? 1 : 0,
]);
return $this->response->withOk();
}
return $this->response->errorWrongArgs('Something went wrong');
}
public function stopRecording(Activity $activity)
{
$this->authorize('stopRecord', $activity);
// Tell Twilio to stop recording this activity.
if ($activity->isRecordingState(Activity::RECORDING_IN_PROGRESS)) {
$job = (new StopRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Activity is not recording.');
}
/**
* Add activity to this user's favorites playlist
*
* @throws AuthorizationException
*/
public function favorite(Activity $activity, PlaylistActivityRepository $playlistActivityRepository): JsonResponse
{
$this->authorize('favorite', $activity);
$user = $this->getUserFromRequest($this->request);
$favorite = $activity->wasFavoritedBy($user);
$name = $activity->activity_title ?? '';
// It needs to check at least one record.
if (! $favorite) {
$favoritePlaylist = $user->favoritePlaylist();
$playlistActivity = $playlistActivityRepository->findByBaseActivityUserAndPlaylist(
$activity,
$user,
$favoritePlaylist
);
if ($playlistActivity !== null) {
$playlistActivity->update(
// Just update, don't sort.
['start_time' => 0, 'name' => mb_strimwidth($name, 0, 100)],
);
} else {
$playlistActivity = $activity->playlistActivities()->create([
'playlist_id' => $favoritePlaylist->getId(),
'user_id' => $user->getId(),
'start_time' => 0,
'name' => mb_strimwidth($name, 0, 100),
]);
// Sort it on top.
$playlistActivity->update(
[
'sort' => $playlistActivityRepository->calculateNewSortOrder(
null,
$playlistActivity,
),
],
);
}
$playlistActivityRepository->calculateNewSortOrder(null, $playlistActivity);
return new JsonResponse([], JsonResponse::HTTP_CREATED);
}
return new JsonResponse(
[
'error' => [
'code' => AbstractResponse::CODE_CONFLICT,
'http_code' => JsonResponse::HTTP_CONFLICT,
'message' => 'Resource Already Exists',
],
],
JsonResponse::HTTP_CONFLICT,
);
}
/**
* Remove activity from this us...
|
PhpStorm
|
faVsco.js – ActivityController.php
|
NULL
|
76685
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
2
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Http\Controllers\API;
use Carbon\Carbon;
use ChaseConey\LaravelDatadogHelper\Datadog;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Jiminny\Component\ActivityAnalytics;
use Jiminny\Component\ActivitySearch;
use Jiminny\Component\ActivitySearch\FilterDefinitionCollection;
use Jiminny\Component\PlaybackPage\Comments\Services\ActivityCommentService;
use Jiminny\Component\Queue\Constants;
use Jiminny\Contracts\Nudge\NudgeFactoryInterface;
use Jiminny\Contracts\Playlist\PlaylistTrackFactoryInterface;
use Jiminny\Contracts\Repositories\PlaylistActivityRepository;
use Jiminny\Contracts\Services\Crm\ServiceInterface;
use Jiminny\Enums\TeamSetting;
use Jiminny\Events\Activities\AiAutomation\ActivityProspectAdded;
use Jiminny\Events\Activities\Coaching\Coached;
use Jiminny\Contracts\Services\Crm\SupportsObjectTypeParseInterface;
use Jiminny\Exceptions\LogicException;
use Jiminny\Exceptions\SocialAccountTokenInvalidException;
use Jiminny\Http\Controllers\API\BaseController as Controller;
use Jiminny\Http\Controllers\CommentContextInterface;
use Jiminny\Http\Responses\Api\AbstractResponse;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Http\Serializers\JsonSerializer;
use Jiminny\Http\Transformers\ActivityCommentTransformer;
use Jiminny\Http\Transformers\ActivityTopicTriggerTransformer;
use Jiminny\Http\Transformers\ActivityTransformer;
use Jiminny\Http\Transformers\AvailabilityNotificationTransformer;
use Jiminny\Http\Transformers\CoachingFeedbackTransformer;
use Jiminny\Http\Transformers\CoachingSectionsTransformer;
use Jiminny\Http\Transformers\SearchTransformer;
use Jiminny\Http\Transformers\StatsTransformer;
use Jiminny\Jobs\Crm\SaveActivity;
use Jiminny\Jobs\Crm\UpdateStage;
use Jiminny\Jobs\Telephony\StartRecording;
use Jiminny\Jobs\Telephony\StopRecording;
use Jiminny\Jobs\Telephony\ToggleRecording;
use Jiminny\Models\Account;
use Jiminny\Models\Activity;
use Jiminny\Models\Activity\CoachRequest;
use Jiminny\Models\Activity\Comment;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\Activity\SearchFilter;
use Jiminny\Models\Activity\Share;
use Jiminny\Models\CoachingFeedback;
use Jiminny\Models\CoachingSection;
use Jiminny\Models\CoachingSectionCriterion;
use Jiminny\Models\CoachingSectionFeedback;
use Jiminny\Models\Contact;
use Jiminny\Models\Crm\Field;
use Jiminny\Models\Crm\FieldData;
use Jiminny\Models\Crm\Layout;
use Jiminny\Models\Crm\LayoutEntity;
use Jiminny\Models\Feature\FeatureEnum;
use Jiminny\Models\LanguageDialect;
use Jiminny\Models\Lead;
use Jiminny\Models\Nudge;
use Jiminny\Models\PlaybookCategory;
use Jiminny\Models\Playlist;
use Jiminny\Models\Stage;
use Jiminny\Models\Team;
use Jiminny\Models\Track;
use Jiminny\Models\User;
use Jiminny\Repositories\AjReportsRepository;
use Jiminny\Repositories\CoachingFeedbackRepository;
use Jiminny\Repositories\ElasticActivityRepository;
use Jiminny\Repositories\TeamRepository;
use Jiminny\Rules\CrmReference;
use Jiminny\Rules\MultidimensionalArrayMaxCharRule;
use Jiminny\Services\ActivityService;
use Jiminny\Services\Crm\ProviderRegistry;
use Jiminny\Services\PlaybackService;
use Jiminny\Services\UserService;
use Jiminny\VO\Repository\OnDemandActivitySearch\Criteria;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Sentry;
use Symfony\Component\HttpFoundation;
final class ActivityController extends Controller implements CommentContextInterface
{
// Number of minutes to look back on activities. i.e. a timeout on activity duration.
private const LOOK_BACK = 180;
public function __construct(
private ProviderRegistry $providerRegistry,
private ActivityService $activityService,
Response $response,
private UserService $userService,
private ActivitySearch\Service\ActivitySearch $activitySearch,
private NudgeFactoryInterface $nudgeFactory,
private ActivityCommentService $activityCommentService,
private LoggerInterface $logger,
private readonly CoachingFeedbackRepository $coachingFeedbackRepository,
private readonly TeamRepository $teamRepository,
private readonly AjReportsRepository $ajReportsRepository,
) {
parent::__construct($response);
}
public static function getCommentImplementation(): string
{
return Comment::class;
}
public function delete()
{
$this->request->validate([
'*' => 'uuid:activities',
]);
$deletedIds = [];
foreach ($this->request->all() as $activityId) {
$activity = Activity::idOrUuId($activityId);
try {
if ($this->authorize('delete', $activity)) {
$activity->delete();
$deletedIds[] = $activityId;
\Log::info('Soft deleted activity ' . $activity->id_string . ' by user ' . $this->getUser()->id);
}
} catch (AuthorizationException $authorizationException) {
// They didn't have permission.
}
}
return $this->response->withArray($deletedIds);
}
public function update(Request $request, Activity $activity)
{
$this->authorize('updateMetadata', $activity);
$request->validate([
'title' => 'string|max:250',
'category_id' => 'uuid:playbook_categories',
'language' => [
new In(
LanguageDialect::query()
->with('language')
->cursor()
->map(static function (LanguageDialect $languageDialect): string {
return $languageDialect->getLanguageLocale();
})
->all()
),
],
]);
if ($request->has('title')) {
$activity->title = $request->input('title');
}
if ($request->has('category_id')) {
$category = PlaybookCategory::uuid($request->input('category_id'));
if ($category->playbook->team_id !== $request->user()->team_id) {
return $this->response->errorNotFound('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
if ($request->has('language')) {
if (! $activity->isInProgress()) {
return $this->response->withError(
'Activity language can only be set while the meeting is in progress.',
400
);
}
$activity->setLanguageCode($request->input('language'));
}
$activity->save();
return $this->response->withOk();
}
// XXX: This should be merged with the update method.
/**
* @param Activity $activity
*
* @throws AuthorizationException
* @throws SocialAccountTokenInvalidException
*
* @return mixed
*/
public function summarize(Activity $activity): mixed
{
$this->logger->info('[Log Activity] Summarizing activity ', [
'activityId' => $activity->getUuid(),
'payload' => $this->request->all(),
]);
$this->authorize('update', $activity);
$this->logger->info('[Log Activity] Validating summary');
// Validate the payload.
$this->validateSummary($activity);
// All objects must belong to this team.
/** @var User $user */
$user = $this->request->user();
$team = $user->getTeam();
$crmService = $this->providerRegistry->get($team->crm->provider);
try {
$crmUser = $user;
if ($user->isCrmRequired() === false) {
$crmUser = $team->owner;
}
$crmService->setUser($crmUser);
} catch (SocialAccountTokenInvalidException $accountTokenInvalidException) {
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($accountTokenInvalidException->getMessage());
}
$rawEntities = $this->request->input('entities');
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid(
$this->request->input('layout_id')
);
// Delay execution of CRM jobs to avoid locking issues.
$jobDelay = 0;
// If we have arrived from a notification, mark it as read.
$notificationId = $this->request->input('nId');
if ($notificationId) {
$notification = $user->unreadNotifications->where('id', $notificationId)->first();
if ($notification) {
$notification->markAsRead();
}
}
$title = $this->request->input('title');
$prospects = $this->request->input('prospects');
$opportunityId = $this->request->input('opportunity_id');
$stageId = $this->request->input('stage_id');
$categoryId = $this->request->input('category_id');
$summary = $this->request->input('summary');
$crmProviderId = $this->request->input('crm_id');
$isInternal = $this->request->input('is_internal') ?? false;
$lead = null;
$category = null;
$account = null;
$contact = null;
$opportunity = null;
$stage = null;
$callStage = null;
foreach ($prospects as $prospectData) {
$objectId = $prospectData['id'];
if ($objectId === null) {
continue;
}
$objectType = $prospectData['type'];
$this->logger->info('debug', ['prospect_data' => $prospectData]);
try {
if ($objectType === null) {
$this->logger->info('no object type');
if ($crmService instanceof SupportsObjectTypeParseInterface) {
$objectType = $crmService->parseObjectType($objectId);
}
}
switch ($objectType) {
case 'lead':
$this->logger->info('Processing lead');
/** @var Lead|null $lead */
$lead = $team->crm->leads()->where('crm_provider_id', $objectId)->first();
// Lead does not exist locally, import it.
if ($lead === null) {
$this->logger->info('Lead does not exist locally');
/** @var Lead $lead */
$lead = $crmService->syncLead($objectId);
}
$this->logger->info('Lead found', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
if ($stageId === null) {
$this->logger->info('Stage ID is null');
// If it was not provided, just assume it is the current stage.
$callStage = $lead->stage;
break;
}
$this->logger->info('Looking for stage');
// Determine if they have changed the stage.
/** @var Stage $stage */
$stage = $team->crm->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_LEAD)
->firstOrFail();
$this->logger->info('Stage found', ['stageId' => $stage->id, 'lead_stage' => $lead->stage_id]);
if ($lead->stage_id && $lead->stage_id !== $stage->id) {
$this->logger->info('Stage has changed');
// Storage current stage on activity.
$callStage = $lead->stage;
// The stage has changed, update in remote CRM.
dispatch(new UpdateStage($activity, $lead, $callStage, $stage));
$this->logger->info(
sprintf(
'[%s] User changing lead stage from %s to %s',
$crmService->getDisplayName(),
$callStage->getName(),
$stage->getName()
),
[
'user' => $user->getUuid(),
'lead' => $lead->getUuid(),
]
);
} else {
$this->logger->info('Stage has not changed');
// Stage remains as current.
$callStage = $stage;
}
break;
case 'account':
$this->logger->info('Processing account');
// If the object is not a lead, it should be an account.
$account = $team->crm->accounts()->where('crm_provider_id', $objectId)->first();
// Account does not exist locally, import it.
if ($account === null) {
$this->logger->info('Account does not exist locally');
$account = $crmService->syncAccount($objectId);
}
$this->logger->info('Account found', ['accountId' => $account->id]);
break;
case 'contact':
$this->logger->info('processing contact');
$contact = $team->crm->contacts()->where('crm_provider_id', $objectId)->first();
// Contact does not exist locally, import it.
if (! $contact instanceof Contact) {
$this->logger->info('contact does not exist locally');
$contact = $crmService->syncContact($objectId);
}
$this->logger->info('resolving account');
$account = $this->resolveAccount($team, $contact, $crmService, $prospects);
break;
}
// If they have specified an opportunity, retrieve this with stage.
if ($opportunityId) {
$this->logger->info('opportunity id is set');
$opportunity = $team->crm->opportunities()->where('crm_provider_id', $opportunityId)->first();
// Opportunity does not exist locally, import it.
if ($opportunity === null) {
$this->logger->info('opportunity does not exist locally');
$opportunity = $crmService->syncOpportunity($opportunityId);
}
if ($stageId === null) {
$this->logger->info('stage id is null');
// If it was not provided, just assume it is the current stage.
$callStage = $opportunity->stage ?? null;
} else {
$this->logger->info('looking for stage');
/** @var ?Stage $opportunityStage */
$opportunityStage = $team->crm
->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_OPPORTUNITY)
->first();
// There is a chance we still cannot import this opportunity.
if ($opportunityStage !== null && $opportunity !== null && $opportunity->stage_id !== $opportunityStage->id) {
$this->logger->info('opportunity stage has changed');
// Storage current stage on activity.
$callStage = $opportunity->stage;
dispatch(new UpdateStage($activity, $opportunity, $callStage, $opportunityStage));
$this->logger->info(
sprintf(
'[%s] User changing opportunity stage from %s to %s',
$crmService->getDisplayName(),
$callStage->name,
$opportunityStage->name
),
[
'userId' => $user->id_string,
'opportunityId' => $opportunity->id_string,
]
);
} else {
$this->logger->info('opportunity stage has not changed');
// Stage remains as current.
$callStage = $opportunityStage;
}
}
}
if ($crmProviderId) {
// Cast $crmProviderId to string otherwise it won't use database index for some records
$linkedActivity = Activity::where('crm_provider_id', (string) $crmProviderId)->first();
// Check if this activity has already been assigned to a different activity.
if ($linkedActivity && $linkedActivity->id !== $activity->id) {
throw new InvalidArgumentException(
'Sorry, the linked task has already been logged under a different call. '
. 'Please choose another linked task.'
);
}
}
} catch (InvalidArgumentException $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($exception->getMessage());
} catch (Exception $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorInternalError(
'Sorry, an error occurred. Please try again or reach out to support if the problem continues.'
);
}
}
if ($categoryId) {
$category = PlaybookCategory::uuid($categoryId);
if ($category->playbook->team_id !== $team->id) {
throw new InvalidArgumentException('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
$this->logger->info('Prospect data', [
'lead_id' => $lead?->getId(),
'account_id' => $account?->getId(),
'contact_id' => $contact?->getId(),
'opportunity_id' => $opportunity?->getId(),
'stage_id' => $stage?->getId(),
]);
if ($title) {
$activity->title = $title;
}
if ($summary) {
$activity->summary = $summary;
}
if ($crmProviderId) {
$activity->crm_provider_id = $crmProviderId;
}
if ($callStage) {
$this->logger->info('Setting stage id', ['stageId' => $callStage->id]);
$activity->stage_id = $callStage->id;
}
if ($lead) {
$this->logger->info('Setting lead id', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
// If we are changed from an account > lead, unset the account data.
$this->logger->info('Unsetting account id, opportunity id, contact id, value');
$activity->account_id = null;
$activity->opportunity_id = null;
$activity->contact_id = null;
$activity->value = null;
}
if ($account) {
$this->logger->info('Setting account id', ['accountId' => $account->id]);
$activity->account_id = $account->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('unsetting lead id');
$activity->lead_id = null;
// Unset the contact if switching different accounts. Will be set up below if still applicable.
if (! $team->hasFeature(FeatureEnum::LINK_ACTIVITY_TO_MULTIPLE_PROSPECTS) || empty($contact)) {
$this->logger->info('Unsetting contact id');
$activity->contact_id = null;
}
}
if ($opportunity) {
$this->logger->info('setting opportunity id', ['opportunityId' => $opportunity->id]);
$this->logger->info('unsetting lead id');
$activity->opportunity_id = $opportunity->id;
$activity->value = $opportunity->value;
// If we are changed from an lead > account, unset the lead data.
$activity->lead_id = null;
}
if ($contact) {
$this->logger->info('setting contact id', ['contactId' => $contact->id]);
$activity->contact_id = $contact->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('Unsetting lead id');
$activity->lead_id = null;
}
$activity->is_internal = $isInternal;
$activity->save();
$activity->refresh();
$this->logger->notice('Activity saved', [
'activity_id' => $activity->getId(),
'lead_id' => $activity->lead_id,
'account_id' => $activity->account_id,
'contact_id' => $activity->contact_id,
'opportunity_id' => $activity->opportunity_id,
'stage_id' => $activity->stage_id,
'crm_provider_id' => $activity->getCrmProviderId(),
]);
// Store entities as field data on the activity.
$updatedData = $this->storeEntities($crmService, $activity, $layout, $rawEntities);
if ($activity->isLoggable()) {
// Follow-up Task or Event data.
$followupData = $this->fetchFollowupEntities($crmService, $layout, $rawEntities);
$this->logger->info('CRM LOG manual log triggered', [
'activityId' => $activity->getUuid(),
'followupData' => $followupData,
'userId' => $user->getUuid(),
]);
// Store data in the CRM.
// ++add check for crm_required
$job = new SaveActivity($activity, $followupData);
if ($updatedData) {
$job->delay(Carbon::now()->addMinutes($jobDelay));
}
dispatch($job);
// Manually dispatch log for Opportunity or Prospect added
if ($activity->hasOpportunity() || $activity->hasProspect()) {
event(new ActivityProspectAdded(
activity: $activity,
eventSource: 'manually-log-crm-data'
));
}
}
return $this->response->withOk();
}
/**
* Extract any activity data to be upserted in the Lead/Opportunity/Task etc in the CRM.
*
* @param ServiceInterface $service
* @param Activity $activity
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function storeEntities(ServiceInterface $service, Activity $activity, Layout $layout, array $entities): array
{
$updatedData = [];
$existingData = $activity->data()->get();
// We need to delete any existing data to overwrite with latest values.
$activity->data()->delete();
$layoutEntities = $layout->entities()
->with('field', 'parent')
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->get();
/** @var LayoutEntity $entity */
foreach ($layoutEntities as $entity) {
// If the user has provided a value for this entity
if (array_key_exists($entity->id_string, $entities)) {
$value = $entities[$entity->id_string];
// Convert raw data into values that the CRM can consume.
if ($value) {
$value = $service->normalizeValue($entity->field->type, $value);
}
// Check the field is part of the activity-summary section.
if ($entity->parent && $entity->parent->label === 'activity-summary' && $value) {
// This is the internal database ID, not the external CRM ID.
$objectId = null;
switch ($entity->field->object_type) {
case Field::OBJECT_ACCOUNT:
$objectId = $activity->account_id;
break;
case Field::OBJECT_CONTACT:
$objectId = $activity->contact_id;
break;
case Field::OBJECT_OPPORTUNITY:
$objectId = $activity->opportunity_id;
break;
case Field::OBJECT_LEAD:
$objectId = $activity->lead_id;
break;
case Field::OBJECT_TASK:
case Field::OBJECT_EVENT:
$objectId = $activity->id;
break;
}
if ($objectId) {
/** @var FieldData $data */
$data = $activity->data()->create([
'crm_layout_entity_id' => $entity->id,
'crm_field_id' => $entity->crm_field_id,
'object_type' => $entity->field->object_type,
'object_id' => $objectId,
'value' => $value,
]);
// Never send read-only field data to the CRM.
if ($entity->read_only === false && $entity->is_visible) {
$existingValue = $existingData
->where('crm_layout_entity_id', $entity->id)
->where('crm_field_id', $entity->crm_field_id)
->where('object_type', $entity->field->object_type)
->where('object_id', $objectId)
->first();
// If the field was actually changed, we need to reflect this in the CRM too.
if ($existingValue === null || $existingValue->value !== $value) {
$updatedData[] = $data->id;
}
}
}
}
}
}
return $updatedData;
}
/**
* Extract any followup data to be dispatched in a job to create a new Task/Event in the CRM.
*
* @param ServiceInterface $crmService
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function fetchFollowupEntities(ServiceInterface $crmService, Layout $layout, array $entities): array
{
$fieldData = [];
foreach ($entities as $entityId => $value) {
// Only bother with fields that have a value.
if ($value) {
// Extract the entity from the UUID. Check the field is valid and part of the follow-up section.
$entity = $layout->entities()
->uuid($entityId, false)
->whereHas('parent', function ($query) {
$query->where('label', 'follow-up');
})
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->first();
if ($entity) {
// Convert raw data into values that the CRM can consume.
$value = $crmService->normalizeValue($entity->field->type, $value);
// Add the field and value to the payload.
$fieldData += [
$entity->field->crm_provider_id => $value,
];
}
}
}
return $fieldData;
}
/**
* @param Activity $activity
*/
private function validateSummary(Activity $activity): void
{
$team = $activity->user->team;
$crmProvider = $team->crm->provider;
$attributes = [];
$rules = [
'layout_id' => 'required|uuid:crm_layouts,crm_configuration_id,' . $team->crm_id,
'title' => 'string|max:250',
'prospects' => 'required|array',
'opportunity_id' => new CrmReference($crmProvider),
'category_id' => 'uuid:playbook_categories|required_unless:is_internal,true',
'stage_id' => 'uuid:stages,team_id,' . $team->id, // Todo: move to proper validator
'summary' => 'max:50000',
'nId' => 'exists:notifications,id',
'crm_id' => new CrmReference($crmProvider),
'entities' => 'array',
'is_internal' => 'boolean',
];
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid($this->request->input('layout_id'));
// Only validate fields, not headers etc. If not loggable, we don't care about follow-up section.
$entities = $layout->entities()
->where('read_only', 0)
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->whereHas('parent', function ($query) use ($activity) {
if ($activity->isLoggable() === false) {
$query->where('label', '<>', 'follow-up');
}
});
$isInternal = $this->request->input('is_internal', false);
foreach ($entities->get() as $entity) {
$rules += $this->buildFieldValidator($entity, $isInternal);
$attributes += $this->buildFieldMessage($entity);
}
$this->request->validate($rules, [], $attributes);
}
private function buildFieldValidator(LayoutEntity $entity, bool $isInternal): array
{
return [
'entities.' . $entity->id_string => $entity->getValidator($isInternal),
];
}
/**
* @param LayoutEntity $entity
*
* @return array
*/
private function buildFieldMessage(LayoutEntity $entity): array
{
$label = $entity->label;
if ($label === null) {
$label = $entity->field->label;
}
return [
'entities.' . $entity->id_string => $label,
];
}
public function search(Request $request, ElasticActivityRepository $repository): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->debugLog(
$user,
'User extracted from request',
['user' => $user->getId(), 'tz' => $user->getTimezone()]
);
$searchCriteria = Criteria::createFromRequest($request->all(), $user->getTimezone());
$this->debugLog(
$user,
'ActivitySearch criteria built',
['searchCriteria' => $searchCriteria]
);
$filterSet = $this->activitySearch->getHomepageFilterSet($searchCriteria, $user);
$this->debugLog($user, 'FilterSet built', ['filterSet' => $filterSet]);
$this->validateSearch($request, $filterSet);
$this->debugLog($user, 'Request validated');
$searchResponse = $repository->onDemandSearch($user, $searchCriteria, $filterSet);
/** @var Collection<Activity> $activities */
$activities = $searchResponse['results'];
$this->debugLog($user, 'Activities ES response extracted');
$hideInternalMeetingsSetting = $this->teamRepository->getTeamSettingByTeamId(
$user->getTeamId(),
TeamSetting::HIDE_INTERNAL_SCHEDULED_MEETINGS->name(),
);
if ($hideInternalMeetingsSetting?->getValue() === '1') {
$activities = $activities->filter(function (Activity $activity) {
if ($activity->is_internal && empty($activity->actual_start_time)) {
return false;
}
return true;
});
}
$this->debugLog($user, 'Internal meetings (?!) filtered');
$this->response->getManager()
->parseIncludes([
'category',
'organizer.group',
'prospect',
'stage',
'opportunity',
'stats',
'scorecards',
'masterTrack',
'activeParticipants',
'notification',
])
->setSerializer(new JsonSerializer());
$transformerExcludes = $this->request->input('exclude');
if ($transformerExcludes) {
$this->response->getManager()->parseExcludes($transformerExcludes);
}
$this->debugLog($user, 'Response Manager (?!) applied');
$transformer = new ActivityTransformer();
$transformer->setConsumer($user);
$this->debugLog($user, 'Activity Transformer added');
$resource = new \League\Fractal\Resource\Collection($activities, $transformer);
$page = $searchCriteria->getPageNumber();
$this->debugLog($user, 'Search criteria page number called', ['page' => $page]);
$histogram = array_pluck(array_get($searchResponse, 'histogram.buckets', []), 'doc_count', 'key_as_string');
$this->debugLog($user, 'Histogram generated. Response is ready.', ['histogram' => $histogram]);
return $this->response->withArray([
'pagination' => [
'total' => $searchResponse['totalHits'],
'current' => $page,
'prev' => max($page - 1, 1),
'next' => $page + 1,
],
'results' => $this->response->getManager()->createData($resource)->toArray(),
'histogram' => $histogram,
]);
}
private function debugLog(User $user, string $logMessage, ?array $context = []): void
{
// Debug for Learning People Only
if ($user->getTeamId() !== 260) {
return;
}
Log::notice(
sprintf('[activity-search-controller] %s', $logMessage),
$context
);
}
/** @throws ValidationException */
private function validateSearch(Request $request, FilterDefinitionCollection $filterSet, ?string $prefix = null): void
{
$rules = [
'exclude' => 'array',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
];
if ($prefix !== null && mb_strpos($prefix, '.') !== false) {
$rules[rtrim($prefix, '.')] = sprintf(
'required|array|max:%d',
$filterSet->count()
);
}
$validationRules = $filterSet->getValidationRules($prefix)
->merge($rules)
->all();
$request->validate($validationRules);
}
public function createActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$search = $this->updateOrCreateActivitySearch($request);
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function updateActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('update', $search);
$this->updateOrCreateActivitySearch($request, $search);
return $this->response->withOk();
}
private function storeNamedSearchFilters(
Collection $request,
Search $search,
FilterDefinitionCollection $filterSet,
?string $prefix = null,
): self {
$arrayTypeProperties = $filterSet
->getPropertyTypes([
FilterDefinitionCollection::PROPERTY_TYPE_ARRAY,
])
->all();
$supportedRequestProperties = $filterSet->getSupportedRequestProperties($prefix);
foreach ($supportedRequestProperties as $requestPropertyName) {
if (! array_has($request, $requestPropertyName)) {
continue;
}
/** @var string|string[] $propertyValue */
$propertyValue = array_get($request, $requestPropertyName);
$propertyName = $prefix === null
? $requestPropertyName
: mb_substr($requestPropertyName, mb_strlen($prefix));
$isArrayType = array_has($arrayTypeProperties, $propertyName);
if (! $isArrayType) {
/** @var string $requestPropertyValue */
$search->filters()->updateOrCreate(
[
'filter' => $propertyName,
],
[
'value' => $propertyValue,
]
);
continue;
}
/** @var string[] $requestPropertyValue */
/** @var SearchFilter[]|Collection $existingFilterValues */
$existingFilterValuesKeyed = $search->filters()
->where('filter', $propertyName)
->get()
->keyBy('id');
// Iterate over values provided as request parameters
foreach ($propertyValue as $value) {
/** @var SearchFilter|null $valueFilter */
$valueFilter = $search->filters()
->where(
[
'filter' => $propertyName,
'value' => $value,
]
)
->first();
if ($valueFilter !== null) {
// Remove filter value pair from list to be deleted
$existingFilterValuesKeyed->forget($valueFilter->id);
} else {
// Add new filter/value pair
$search->filters()->updateOrCreate([
'filter' => $propertyName,
'value' => $value,
]);
}
}
// Delete filter value pairs for this filter that no longer exist in request parameters
foreach ($existingFilterValuesKeyed as $existingFilter) {
$existingFilter->delete();
}
}
/** @var Collection<int, SearchFilter> $filtersKeyed */
$filtersKeyed = $search->filters()->get()->keyBy('filter');
// wipe removed filters from this search
foreach ($filtersKeyed as $filterName => $filter) {
if (array_has($request, $prefix . $filterName)) {
continue;
}
// Remove all filter values for this filter
$search->filters()->where('filter', $filterName)->delete();
}
return $this;
}
/**
* @throws AuthorizationException
*/
public function fetchActivitySearch(
Search $search,
Request $request,
SearchTransformer $searchTransformer,
): JsonResponse {
$this->authorize('view', $search);
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function listActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withCollection(
$user->searches()->get(),
$searchTransformer
->withConsumer($user)
);
}
/**
* Deletes a saved search
*
* @param Request $request
* @param Search $search
*
* @throws Exception
*
* @return JsonResponse
*/
public function deleteActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('delete', $search);
$ajReportCount = $this->ajReportsRepository->countActiveReportsBySavedSearch($search->getId());
if ($ajReportCount > 0) {
return $this->response->errorWrongArgs(
"This saved search is used by {$ajReportCount} active AJ report(s). "
. 'Please remove or update those reports before deleting this saved search.'
);
}
$search->filters()->delete();
$search->delete();
return $this->response->withOk();
}
public function live(Request $request, ElasticActivityRepository $repository): JsonResponse
{
$user = $this->getUserFromRequest($request);
$this->request->validate([
'sort_direction' => 'in:asc,desc',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
]);
$activities = $repository->getLiveCoachingEligibleActivities(
user: $user,
lookBackMinutes: self::LOOK_BACK,
limit: (int) $this->request->input('limit', 25),
page: (int) $this->request->input('page', 1),
sortBy: ['actual_start_time', 'scheduled_start_time'],
sortDirection: (string) $this->request->input('sort_direction', 'asc'),
);
$this->response
->getManager()
->parseIncludes(['organizer.group', 'prospect'])
->setSerializer(new JsonSerializer());
return $this->response->withCollection($activities, new ActivityTransformer());
}
/**
* @param Activity $activity
*
* @throws AuthorizationException
*
* @return mixed
*/
public function show(Activity $activity, ActivityService $activityService): JsonResponse
{
$this->authorize('show', $activity);
$user = $activity->getUser();
$team = $user->getTeam();
// Sync the opportunity with the latest data if possible.
if ($activity->opportunity_id) {
try {
$crmService = $this->providerRegistry->get($team->crm->provider);
if (! $user->isCrmRequired()) {
$crmService->setUser($team->getOwner());
} else {
$crmService->setUser($user);
}
$crmService->syncOpportunity($activity->opportunity->crm_provider_id);
} catch (Exception $exception) {
// Move on.
}
}
$activityData = $activityService->getActivityData($this->request->user(), $activity);
return response()->json($activityData);
}
public function createRecording(Activity $activity)
{
$this->authorize('record', $activity);
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Tell Twilio to start recording this activity.
if ($activity->recording_state === Activity::RECORDING_OFF) {
$job = (new StartRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withCreated();
}
return $this->response->errorGone('Activity is already recording.');
}
public function updateRecording(Request $request, Activity $activity)
{
$this->authorize('record', $activity);
$request->validate([
'preference' => 'boolean',
'state' => [
'string',
Rule::in([
Activity::RECORDING_IN_PROGRESS,
Activity::RECORDING_PAUSED,
]),
],
]);
if ($request->has('state')) {
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Toggle the recording state between paused and resumed.
if (! $activity->isRecordingState(Activity::RECORDING_OFF)) {
$job = (new ToggleRecording($activity, $request->input('state')))
->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Recording is not toggleable.');
}
if ($request->has('preference')) {
$activity->update([
'recording_preference' => $request->input('preference') ? 1 : 0,
]);
return $this->response->withOk();
}
return $this->response->errorWrongArgs('Something went wrong');
}
public function stopRecording(Activity $activity)
{
$this->authorize('stopRecord', $activity);
// Tell Twilio to stop recording this activity.
if ($activity->isRecordingState(Activity::RECORDING_IN_PROGRESS)) {
$job = (new StopRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Activity is not recording.');
}
/**
* Add activity to this user's favorites playlist
*
* @throws AuthorizationException
*/
public function favorite(Activity $activity, PlaylistActivityRepository $playlistActivityRepository): JsonResponse
{
$this->authorize('favorite', $activity);
$user = $this->getUserFromRequest($this->request);
$favorite = $activity->wasFavoritedBy($user);
$name = $activity->activity_title ?? '';
// It needs to check at least one record.
if (! $favorite) {
$favoritePlaylist = $user->favoritePlaylist();
$playlistActivity = $playlistActivityRepository->findByBaseActivityUserAndPlaylist(
$activity,
$user,
$favoritePlaylist
);
if ($playlistActivity !== null) {
$playlistActivity->update(
// Just update, don't sort.
['start_time' => 0, 'name' => mb_strimwidth($name, 0, 100)],
);
} else {
$playlistActivity = $activity->playlistActivities()->create([
'playlist_id' => $favoritePlaylist->getId(),
'user_id' => $user->getId(),
'start_time' => 0,
'name' => mb_strimwidth($name, 0, 100),
]);
// Sort it on top.
$playlistActivity->update(
[
'sort' => $playlistActivityRepository->calculateNewSortOrder(
null,
$playlistActivity,
),
],
);
}
$playlistActivityRepository->calculateNewSortOrder(null, $playlistActivity);
return new JsonResponse([], JsonResponse::HTTP_CREATED);
}
return new JsonResponse(
[
'error' => [
'code' => AbstractResponse::CODE_CONFLICT,
'http_code' => JsonResponse::HTTP_CONFLICT,
'message' => 'Resource Already Exists',
],
],
JsonResponse::HTTP_CONFLICT,
);
}
/**
* Remove activity from this us...
|
PhpStorm
|
faVsco.js – ActivityController.php
|
NULL
|
76686
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
27
3
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Http\Controllers\API;
use Carbon\Carbon;
use ChaseConey\LaravelDatadogHelper\Datadog;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Jiminny\Component\ActivityAnalytics;
use Jiminny\Component\ActivitySearch;
use Jiminny\Component\ActivitySearch\FilterDefinitionCollection;
use Jiminny\Component\PlaybackPage\Comments\Services\ActivityCommentService;
use Jiminny\Component\Queue\Constants;
use Jiminny\Contracts\Nudge\NudgeFactoryInterface;
use Jiminny\Contracts\Playlist\PlaylistTrackFactoryInterface;
use Jiminny\Contracts\Repositories\PlaylistActivityRepository;
use Jiminny\Contracts\Services\Crm\ServiceInterface;
use Jiminny\Enums\TeamSetting;
use Jiminny\Events\Activities\AiAutomation\ActivityProspectAdded;
use Jiminny\Events\Activities\Coaching\Coached;
use Jiminny\Contracts\Services\Crm\SupportsObjectTypeParseInterface;
use Jiminny\Exceptions\LogicException;
use Jiminny\Exceptions\SocialAccountTokenInvalidException;
use Jiminny\Http\Controllers\API\BaseController as Controller;
use Jiminny\Http\Controllers\CommentContextInterface;
use Jiminny\Http\Responses\Api\AbstractResponse;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Http\Serializers\JsonSerializer;
use Jiminny\Http\Transformers\ActivityCommentTransformer;
use Jiminny\Http\Transformers\ActivityTopicTriggerTransformer;
use Jiminny\Http\Transformers\ActivityTransformer;
use Jiminny\Http\Transformers\AvailabilityNotificationTransformer;
use Jiminny\Http\Transformers\CoachingFeedbackTransformer;
use Jiminny\Http\Transformers\CoachingSectionsTransformer;
use Jiminny\Http\Transformers\SearchTransformer;
use Jiminny\Http\Transformers\StatsTransformer;
use Jiminny\Jobs\Crm\SaveActivity;
use Jiminny\Jobs\Crm\UpdateStage;
use Jiminny\Jobs\Telephony\StartRecording;
use Jiminny\Jobs\Telephony\StopRecording;
use Jiminny\Jobs\Telephony\ToggleRecording;
use Jiminny\Models\Account;
use Jiminny\Models\Activity;
use Jiminny\Models\Activity\CoachRequest;
use Jiminny\Models\Activity\Comment;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\Activity\SearchFilter;
use Jiminny\Models\Activity\Share;
use Jiminny\Models\CoachingFeedback;
use Jiminny\Models\CoachingSection;
use Jiminny\Models\CoachingSectionCriterion;
use Jiminny\Models\CoachingSectionFeedback;
use Jiminny\Models\Contact;
use Jiminny\Models\Crm\Field;
use Jiminny\Models\Crm\FieldData;
use Jiminny\Models\Crm\Layout;
use Jiminny\Models\Crm\LayoutEntity;
use Jiminny\Models\Feature\FeatureEnum;
use Jiminny\Models\LanguageDialect;
use Jiminny\Models\Lead;
use Jiminny\Models\Nudge;
use Jiminny\Models\PlaybookCategory;
use Jiminny\Models\Playlist;
use Jiminny\Models\Stage;
use Jiminny\Models\Team;
use Jiminny\Models\Track;
use Jiminny\Models\User;
use Jiminny\Repositories\AjReportsRepository;
use Jiminny\Repositories\CoachingFeedbackRepository;
use Jiminny\Repositories\ElasticActivityRepository;
use Jiminny\Repositories\TeamRepository;
use Jiminny\Rules\CrmReference;
use Jiminny\Rules\MultidimensionalArrayMaxCharRule;
use Jiminny\Services\ActivityService;
use Jiminny\Services\Crm\ProviderRegistry;
use Jiminny\Services\PlaybackService;
use Jiminny\Services\UserService;
use Jiminny\VO\Repository\OnDemandActivitySearch\Criteria;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Sentry;
use Symfony\Component\HttpFoundation;
final class ActivityController extends Controller implements CommentContextInterface
{
// Number of minutes to look back on activities. i.e. a timeout on activity duration.
private const LOOK_BACK = 180;
public function __construct(
private ProviderRegistry $providerRegistry,
private ActivityService $activityService,
Response $response,
private UserService $userService,
private ActivitySearch\Service\ActivitySearch $activitySearch,
private NudgeFactoryInterface $nudgeFactory,
private ActivityCommentService $activityCommentService,
private LoggerInterface $logger,
private readonly CoachingFeedbackRepository $coachingFeedbackRepository,
private readonly TeamRepository $teamRepository,
private readonly AjReportsRepository $ajReportsRepository,
) {
parent::__construct($response);
}
public static function getCommentImplementation(): string
{
return Comment::class;
}
public function delete()
{
$this->request->validate([
'*' => 'uuid:activities',
]);
$deletedIds = [];
foreach ($this->request->all() as $activityId) {
$activity = Activity::idOrUuId($activityId);
try {
if ($this->authorize('delete', $activity)) {
$activity->delete();
$deletedIds[] = $activityId;
\Log::info('Soft deleted activity ' . $activity->id_string . ' by user ' . $this->getUser()->id);
}
} catch (AuthorizationException $authorizationException) {
// They didn't have permission.
}
}
return $this->response->withArray($deletedIds);
}
public function update(Request $request, Activity $activity)
{
$this->authorize('updateMetadata', $activity);
$request->validate([
'title' => 'string|max:250',
'category_id' => 'uuid:playbook_categories',
'language' => [
new In(
LanguageDialect::query()
->with('language')
->cursor()
->map(static function (LanguageDialect $languageDialect): string {
return $languageDialect->getLanguageLocale();
})
->all()
),
],
]);
if ($request->has('title')) {
$activity->title = $request->input('title');
}
if ($request->has('category_id')) {
$category = PlaybookCategory::uuid($request->input('category_id'));
if ($category->playbook->team_id !== $request->user()->team_id) {
return $this->response->errorNotFound('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
if ($request->has('language')) {
if (! $activity->isInProgress()) {
return $this->response->withError(
'Activity language can only be set while the meeting is in progress.',
400
);
}
$activity->setLanguageCode($request->input('language'));
}
$activity->save();
return $this->response->withOk();
}
// XXX: This should be merged with the update method.
/**
* @param Activity $activity
*
* @throws AuthorizationException
* @throws SocialAccountTokenInvalidException
*
* @return mixed
*/
public function summarize(Activity $activity): mixed
{
$this->logger->info('[Log Activity] Summarizing activity ', [
'activityId' => $activity->getUuid(),
'payload' => $this->request->all(),
]);
$this->authorize('update', $activity);
$this->logger->info('[Log Activity] Validating summary');
// Validate the payload.
$this->validateSummary($activity);
// All objects must belong to this team.
/** @var User $user */
$user = $this->request->user();
$team = $user->getTeam();
$crmService = $this->providerRegistry->get($team->crm->provider);
try {
$crmUser = $user;
if ($user->isCrmRequired() === false) {
$crmUser = $team->owner;
}
$crmService->setUser($crmUser);
} catch (SocialAccountTokenInvalidException $accountTokenInvalidException) {
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($accountTokenInvalidException->getMessage());
}
$rawEntities = $this->request->input('entities');
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid(
$this->request->input('layout_id')
);
// Delay execution of CRM jobs to avoid locking issues.
$jobDelay = 0;
// If we have arrived from a notification, mark it as read.
$notificationId = $this->request->input('nId');
if ($notificationId) {
$notification = $user->unreadNotifications->where('id', $notificationId)->first();
if ($notification) {
$notification->markAsRead();
}
}
$title = $this->request->input('title');
$prospects = $this->request->input('prospects');
$opportunityId = $this->request->input('opportunity_id');
$stageId = $this->request->input('stage_id');
$categoryId = $this->request->input('category_id');
$summary = $this->request->input('summary');
$crmProviderId = $this->request->input('crm_id');
$isInternal = $this->request->input('is_internal') ?? false;
$lead = null;
$category = null;
$account = null;
$contact = null;
$opportunity = null;
$stage = null;
$callStage = null;
foreach ($prospects as $prospectData) {
$objectId = $prospectData['id'];
if ($objectId === null) {
continue;
}
$objectType = $prospectData['type'];
$this->logger->info('debug', ['prospect_data' => $prospectData]);
try {
if ($objectType === null) {
$this->logger->info('no object type');
if ($crmService instanceof SupportsObjectTypeParseInterface) {
$objectType = $crmService->parseObjectType($objectId);
}
}
switch ($objectType) {
case 'lead':
$this->logger->info('Processing lead');
/** @var Lead|null $lead */
$lead = $team->crm->leads()->where('crm_provider_id', $objectId)->first();
// Lead does not exist locally, import it.
if ($lead === null) {
$this->logger->info('Lead does not exist locally');
/** @var Lead $lead */
$lead = $crmService->syncLead($objectId);
}
$this->logger->info('Lead found', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
if ($stageId === null) {
$this->logger->info('Stage ID is null');
// If it was not provided, just assume it is the current stage.
$callStage = $lead->stage;
break;
}
$this->logger->info('Looking for stage');
// Determine if they have changed the stage.
/** @var Stage $stage */
$stage = $team->crm->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_LEAD)
->firstOrFail();
$this->logger->info('Stage found', ['stageId' => $stage->id, 'lead_stage' => $lead->stage_id]);
if ($lead->stage_id && $lead->stage_id !== $stage->id) {
$this->logger->info('Stage has changed');
// Storage current stage on activity.
$callStage = $lead->stage;
// The stage has changed, update in remote CRM.
dispatch(new UpdateStage($activity, $lead, $callStage, $stage));
$this->logger->info(
sprintf(
'[%s] User changing lead stage from %s to %s',
$crmService->getDisplayName(),
$callStage->getName(),
$stage->getName()
),
[
'user' => $user->getUuid(),
'lead' => $lead->getUuid(),
]
);
} else {
$this->logger->info('Stage has not changed');
// Stage remains as current.
$callStage = $stage;
}
break;
case 'account':
$this->logger->info('Processing account');
// If the object is not a lead, it should be an account.
$account = $team->crm->accounts()->where('crm_provider_id', $objectId)->first();
// Account does not exist locally, import it.
if ($account === null) {
$this->logger->info('Account does not exist locally');
$account = $crmService->syncAccount($objectId);
}
$this->logger->info('Account found', ['accountId' => $account->id]);
break;
case 'contact':
$this->logger->info('processing contact');
$contact = $team->crm->contacts()->where('crm_provider_id', $objectId)->first();
// Contact does not exist locally, import it.
if (! $contact instanceof Contact) {
$this->logger->info('contact does not exist locally');
$contact = $crmService->syncContact($objectId);
}
$this->logger->info('resolving account');
$account = $this->resolveAccount($team, $contact, $crmService, $prospects);
break;
}
// If they have specified an opportunity, retrieve this with stage.
if ($opportunityId) {
$this->logger->info('opportunity id is set');
$opportunity = $team->crm->opportunities()->where('crm_provider_id', $opportunityId)->first();
// Opportunity does not exist locally, import it.
if ($opportunity === null) {
$this->logger->info('opportunity does not exist locally');
$opportunity = $crmService->syncOpportunity($opportunityId);
}
if ($stageId === null) {
$this->logger->info('stage id is null');
// If it was not provided, just assume it is the current stage.
$callStage = $opportunity->stage ?? null;
} else {
$this->logger->info('looking for stage');
/** @var ?Stage $opportunityStage */
$opportunityStage = $team->crm
->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_OPPORTUNITY)
->first();
// There is a chance we still cannot import this opportunity.
if ($opportunityStage !== null && $opportunity !== null && $opportunity->stage_id !== $opportunityStage->id) {
$this->logger->info('opportunity stage has changed');
// Storage current stage on activity.
$callStage = $opportunity->stage;
dispatch(new UpdateStage($activity, $opportunity, $callStage, $opportunityStage));
$this->logger->info(
sprintf(
'[%s] User changing opportunity stage from %s to %s',
$crmService->getDisplayName(),
$callStage->name,
$opportunityStage->name
),
[
'userId' => $user->id_string,
'opportunityId' => $opportunity->id_string,
]
);
} else {
$this->logger->info('opportunity stage has not changed');
// Stage remains as current.
$callStage = $opportunityStage;
}
}
}
if ($crmProviderId) {
// Cast $crmProviderId to string otherwise it won't use database index for some records
$linkedActivity = Activity::where('crm_provider_id', (string) $crmProviderId)->first();
// Check if this activity has already been assigned to a different activity.
if ($linkedActivity && $linkedActivity->id !== $activity->id) {
throw new InvalidArgumentException(
'Sorry, the linked task has already been logged under a different call. '
. 'Please choose another linked task.'
);
}
}
} catch (InvalidArgumentException $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($exception->getMessage());
} catch (Exception $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorInternalError(
'Sorry, an error occurred. Please try again or reach out to support if the problem continues.'
);
}
}
if ($categoryId) {
$category = PlaybookCategory::uuid($categoryId);
if ($category->playbook->team_id !== $team->id) {
throw new InvalidArgumentException('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
$this->logger->info('Prospect data', [
'lead_id' => $lead?->getId(),
'account_id' => $account?->getId(),
'contact_id' => $contact?->getId(),
'opportunity_id' => $opportunity?->getId(),
'stage_id' => $stage?->getId(),
]);
if ($title) {
$activity->title = $title;
}
if ($summary) {
$activity->summary = $summary;
}
if ($crmProviderId) {
$activity->crm_provider_id = $crmProviderId;
}
if ($callStage) {
$this->logger->info('Setting stage id', ['stageId' => $callStage->id]);
$activity->stage_id = $callStage->id;
}
if ($lead) {
$this->logger->info('Setting lead id', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
// If we are changed from an account > lead, unset the account data.
$this->logger->info('Unsetting account id, opportunity id, contact id, value');
$activity->account_id = null;
$activity->opportunity_id = null;
$activity->contact_id = null;
$activity->value = null;
}
if ($account) {
$this->logger->info('Setting account id', ['accountId' => $account->id]);
$activity->account_id = $account->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('unsetting lead id');
$activity->lead_id = null;
// Unset the contact if switching different accounts. Will be set up below if still applicable.
if (! $team->hasFeature(FeatureEnum::LINK_ACTIVITY_TO_MULTIPLE_PROSPECTS) || empty($contact)) {
$this->logger->info('Unsetting contact id');
$activity->contact_id = null;
}
}
if ($opportunity) {
$this->logger->info('setting opportunity id', ['opportunityId' => $opportunity->id]);
$this->logger->info('unsetting lead id');
$activity->opportunity_id = $opportunity->id;
$activity->value = $opportunity->value;
// If we are changed from an lead > account, unset the lead data.
$activity->lead_id = null;
}
if ($contact) {
$this->logger->info('setting contact id', ['contactId' => $contact->id]);
$activity->contact_id = $contact->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('Unsetting lead id');
$activity->lead_id = null;
}
$activity->is_internal = $isInternal;
$activity->save();
$activity->refresh();
$this->logger->notice('Activity saved', [
'activity_id' => $activity->getId(),
'lead_id' => $activity->lead_id,
'account_id' => $activity->account_id,
'contact_id' => $activity->contact_id,
'opportunity_id' => $activity->opportunity_id,
'stage_id' => $activity->stage_id,
'crm_provider_id' => $activity->getCrmProviderId(),
]);
// Store entities as field data on the activity.
$updatedData = $this->storeEntities($crmService, $activity, $layout, $rawEntities);
if ($activity->isLoggable()) {
// Follow-up Task or Event data.
$followupData = $this->fetchFollowupEntities($crmService, $layout, $rawEntities);
$this->logger->info('CRM LOG manual log triggered', [
'activityId' => $activity->getUuid(),
'followupData' => $followupData,
'userId' => $user->getUuid(),
]);
// Store data in the CRM.
// ++add check for crm_required
$job = new SaveActivity($activity, $followupData);
if ($updatedData) {
$job->delay(Carbon::now()->addMinutes($jobDelay));
}
dispatch($job);
// Manually dispatch log for Opportunity or Prospect added
if ($activity->hasOpportunity() || $activity->hasProspect()) {
event(new ActivityProspectAdded(
activity: $activity,
eventSource: 'manually-log-crm-data'
));
}
}
return $this->response->withOk();
}
/**
* Extract any activity data to be upserted in the Lead/Opportunity/Task etc in the CRM.
*
* @param ServiceInterface $service
* @param Activity $activity
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function storeEntities(ServiceInterface $service, Activity $activity, Layout $layout, array $entities): array
{
$updatedData = [];
$existingData = $activity->data()->get();
// We need to delete any existing data to overwrite with latest values.
$activity->data()->delete();
$layoutEntities = $layout->entities()
->with('field', 'parent')
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->get();
/** @var LayoutEntity $entity */
foreach ($layoutEntities as $entity) {
// If the user has provided a value for this entity
if (array_key_exists($entity->id_string, $entities)) {
$value = $entities[$entity->id_string];
// Convert raw data into values that the CRM can consume.
if ($value) {
$value = $service->normalizeValue($entity->field->type, $value);
}
// Check the field is part of the activity-summary section.
if ($entity->parent && $entity->parent->label === 'activity-summary' && $value) {
// This is the internal database ID, not the external CRM ID.
$objectId = null;
switch ($entity->field->object_type) {
case Field::OBJECT_ACCOUNT:
$objectId = $activity->account_id;
break;
case Field::OBJECT_CONTACT:
$objectId = $activity->contact_id;
break;
case Field::OBJECT_OPPORTUNITY:
$objectId = $activity->opportunity_id;
break;
case Field::OBJECT_LEAD:
$objectId = $activity->lead_id;
break;
case Field::OBJECT_TASK:
case Field::OBJECT_EVENT:
$objectId = $activity->id;
break;
}
if ($objectId) {
/** @var FieldData $data */
$data = $activity->data()->create([
'crm_layout_entity_id' => $entity->id,
'crm_field_id' => $entity->crm_field_id,
'object_type' => $entity->field->object_type,
'object_id' => $objectId,
'value' => $value,
]);
// Never send read-only field data to the CRM.
if ($entity->read_only === false && $entity->is_visible) {
$existingValue = $existingData
->where('crm_layout_entity_id', $entity->id)
->where('crm_field_id', $entity->crm_field_id)
->where('object_type', $entity->field->object_type)
->where('object_id', $objectId)
->first();
// If the field was actually changed, we need to reflect this in the CRM too.
if ($existingValue === null || $existingValue->value !== $value) {
$updatedData[] = $data->id;
}
}
}
}
}
}
return $updatedData;
}
/**
* Extract any followup data to be dispatched in a job to create a new Task/Event in the CRM.
*
* @param ServiceInterface $crmService
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function fetchFollowupEntities(ServiceInterface $crmService, Layout $layout, array $entities): array
{
$fieldData = [];
foreach ($entities as $entityId => $value) {
// Only bother with fields that have a value.
if ($value) {
// Extract the entity from the UUID. Check the field is valid and part of the follow-up section.
$entity = $layout->entities()
->uuid($entityId, false)
->whereHas('parent', function ($query) {
$query->where('label', 'follow-up');
})
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->first();
if ($entity) {
// Convert raw data into values that the CRM can consume.
$value = $crmService->normalizeValue($entity->field->type, $value);
// Add the field and value to the payload.
$fieldData += [
$entity->field->crm_provider_id => $value,
];
}
}
}
return $fieldData;
}
/**
* @param Activity $activity
*/
private function validateSummary(Activity $activity): void
{
$team = $activity->user->team;
$crmProvider = $team->crm->provider;
$attributes = [];
$rules = [
'layout_id' => 'required|uuid:crm_layouts,crm_configuration_id,' . $team->crm_id,
'title' => 'string|max:250',
'prospects' => 'required|array',
'opportunity_id' => new CrmReference($crmProvider),
'category_id' => 'uuid:playbook_categories|required_unless:is_internal,true',
'stage_id' => 'uuid:stages,team_id,' . $team->id, // Todo: move to proper validator
'summary' => 'max:50000',
'nId' => 'exists:notifications,id',
'crm_id' => new CrmReference($crmProvider),
'entities' => 'array',
'is_internal' => 'boolean',
];
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid($this->request->input('layout_id'));
// Only validate fields, not headers etc. If not loggable, we don't care about follow-up section.
$entities = $layout->entities()
->where('read_only', 0)
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->whereHas('parent', function ($query) use ($activity) {
if ($activity->isLoggable() === false) {
$query->where('label', '<>', 'follow-up');
}
});
$isInternal = $this->request->input('is_internal', false);
foreach ($entities->get() as $entity) {
$rules += $this->buildFieldValidator($entity, $isInternal);
$attributes += $this->buildFieldMessage($entity);
}
$this->request->validate($rules, [], $attributes);
}
private function buildFieldValidator(LayoutEntity $entity, bool $isInternal): array
{
return [
'entities.' . $entity->id_string => $entity->getValidator($isInternal),
];
}
/**
* @param LayoutEntity $entity
*
* @return array
*/
private function buildFieldMessage(LayoutEntity $entity): array
{
$label = $entity->label;
if ($label === null) {
$label = $entity->field->label;
}
return [
'entities.' . $entity->id_string => $label,
];
}
public function search(Request $request, ElasticActivityRepository $repository): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->debugLog(
$user,
'User extracted from request',
['user' => $user->getId(), 'tz' => $user->getTimezone()]
);
$searchCriteria = Criteria::createFromRequest($request->all(), $user->getTimezone());
$this->debugLog(
$user,
'ActivitySearch criteria built',
['searchCriteria' => $searchCriteria]
);
$filterSet = $this->activitySearch->getHomepageFilterSet($searchCriteria, $user);
$this->debugLog($user, 'FilterSet built', ['filterSet' => $filterSet]);
$this->validateSearch($request, $filterSet);
$this->debugLog($user, 'Request validated');
$searchResponse = $repository->onDemandSearch($user, $searchCriteria, $filterSet);
/** @var Collection<Activity> $activities */
$activities = $searchResponse['results'];
$this->debugLog($user, 'Activities ES response extracted');
$hideInternalMeetingsSetting = $this->teamRepository->getTeamSettingByTeamId(
$user->getTeamId(),
TeamSetting::HIDE_INTERNAL_SCHEDULED_MEETINGS->name(),
);
if ($hideInternalMeetingsSetting?->getValue() === '1') {
$activities = $activities->filter(function (Activity $activity) {
if ($activity->is_internal && empty($activity->actual_start_time)) {
return false;
}
return true;
});
}
$this->debugLog($user, 'Internal meetings (?!) filtered');
$this->response->getManager()
->parseIncludes([
'category',
'organizer.group',
'prospect',
'stage',
'opportunity',
'stats',
'scorecards',
'masterTrack',
'activeParticipants',
'notification',
])
->setSerializer(new JsonSerializer());
$transformerExcludes = $this->request->input('exclude');
if ($transformerExcludes) {
$this->response->getManager()->parseExcludes($transformerExcludes);
}
$this->debugLog($user, 'Response Manager (?!) applied');
$transformer = new ActivityTransformer();
$transformer->setConsumer($user);
$this->debugLog($user, 'Activity Transformer added');
$resource = new \League\Fractal\Resource\Collection($activities, $transformer);
$page = $searchCriteria->getPageNumber();
$this->debugLog($user, 'Search criteria page number called', ['page' => $page]);
$histogram = array_pluck(array_get($searchResponse, 'histogram.buckets', []), 'doc_count', 'key_as_string');
$this->debugLog($user, 'Histogram generated. Response is ready.', ['histogram' => $histogram]);
return $this->response->withArray([
'pagination' => [
'total' => $searchResponse['totalHits'],
'current' => $page,
'prev' => max($page - 1, 1),
'next' => $page + 1,
],
'results' => $this->response->getManager()->createData($resource)->toArray(),
'histogram' => $histogram,
]);
}
private function debugLog(User $user, string $logMessage, ?array $context = []): void
{
// Debug for Learning People Only
if ($user->getTeamId() !== 260) {
return;
}
Log::notice(
sprintf('[activity-search-controller] %s', $logMessage),
$context
);
}
/** @throws ValidationException */
private function validateSearch(Request $request, FilterDefinitionCollection $filterSet, ?string $prefix = null): void
{
$rules = [
'exclude' => 'array',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
];
if ($prefix !== null && mb_strpos($prefix, '.') !== false) {
$rules[rtrim($prefix, '.')] = sprintf(
'required|array|max:%d',
$filterSet->count()
);
}
$validationRules = $filterSet->getValidationRules($prefix)
->merge($rules)
->all();
$request->validate($validationRules);
}
public function createActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$search = $this->updateOrCreateActivitySearch($request);
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function updateActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('update', $search);
$this->updateOrCreateActivitySearch($request, $search);
return $this->response->withOk();
}
private function storeNamedSearchFilters(
Collection $request,
Search $search,
FilterDefinitionCollection $filterSet,
?string $prefix = null,
): self {
$arrayTypeProperties = $filterSet
->getPropertyTypes([
FilterDefinitionCollection::PROPERTY_TYPE_ARRAY,
])
->all();
$supportedRequestProperties = $filterSet->getSupportedRequestProperties($prefix);
foreach ($supportedRequestProperties as $requestPropertyName) {
if (! array_has($request, $requestPropertyName)) {
continue;
}
/** @var string|string[] $propertyValue */
$propertyValue = array_get($request, $requestPropertyName);
$propertyName = $prefix === null
? $requestPropertyName
: mb_substr($requestPropertyName, mb_strlen($prefix));
$isArrayType = array_has($arrayTypeProperties, $propertyName);
if (! $isArrayType) {
/** @var string $requestPropertyValue */
$search->filters()->updateOrCreate(
[
'filter' => $propertyName,
],
[
'value' => $propertyValue,
]
);
continue;
}
/** @var string[] $requestPropertyValue */
/** @var SearchFilter[]|Collection $existingFilterValues */
$existingFilterValuesKeyed = $search->filters()
->where('filter', $propertyName)
->get()
->keyBy('id');
// Iterate over values provided as request parameters
foreach ($propertyValue as $value) {
/** @var SearchFilter|null $valueFilter */
$valueFilter = $search->filters()
->where(
[
'filter' => $propertyName,
'value' => $value,
]
)
->first();
if ($valueFilter !== null) {
// Remove filter value pair from list to be deleted
$existingFilterValuesKeyed->forget($valueFilter->id);
} else {
// Add new filter/value pair
$search->filters()->updateOrCreate([
'filter' => $propertyName,
'value' => $value,
]);
}
}
// Delete filter value pairs for this filter that no longer exist in request parameters
foreach ($existingFilterValuesKeyed as $existingFilter) {
$existingFilter->delete();
}
}
/** @var Collection<int, SearchFilter> $filtersKeyed */
$filtersKeyed = $search->filters()->get()->keyBy('filter');
// wipe removed filters from this search
foreach ($filtersKeyed as $filterName => $filter) {
if (array_has($request, $prefix . $filterName)) {
continue;
}
// Remove all filter values for this filter
$search->filters()->where('filter', $filterName)->delete();
}
return $this;
}
/**
* @throws AuthorizationException
*/
public function fetchActivitySearch(
Search $search,
Request $request,
SearchTransformer $searchTransformer,
): JsonResponse {
$this->authorize('view', $search);
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function listActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withCollection(
$user->searches()->get(),
$searchTransformer
->withConsumer($user)
);
}
/**
* Deletes a saved search
*
* @param Request $request
* @param Search $search
*
* @throws Exception
*
* @return JsonResponse
*/
public function deleteActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('delete', $search);
$ajReportCount = $this->ajReportsRepository->countActiveReportsBySavedSearch($search->getId());
if ($ajReportCount > 0) {
return $this->response->errorWrongArgs(
"This saved search is used by {$ajReportCount} active AJ report(s). "
. 'Please remove or update those reports before deleting this saved search.'
);
}
$search->filters()->delete();
$search->delete();
return $this->response->withOk();
}
public function live(Request $request, ElasticActivityRepository $repository): JsonResponse
{
$user = $this->getUserFromRequest($request);
$this->request->validate([
'sort_direction' => 'in:asc,desc',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
]);
$activities = $repository->getLiveCoachingEligibleActivities(
user: $user,
lookBackMinutes: self::LOOK_BACK,
limit: (int) $this->request->input('limit', 25),
page: (int) $this->request->input('page', 1),
sortBy: ['actual_start_time', 'scheduled_start_time'],
sortDirection: (string) $this->request->input('sort_direction', 'asc'),
);
$this->response
->getManager()
->parseIncludes(['organizer.group', 'prospect'])
->setSerializer(new JsonSerializer());
return $this->response->withCollection($activities, new ActivityTransformer());
}
/**
* @param Activity $activity
*
* @throws AuthorizationException
*
* @return mixed
*/
public function show(Activity $activity, ActivityService $activityService): JsonResponse
{
$this->authorize('show', $activity);
$user = $activity->getUser();
$team = $user->getTeam();
// Sync the opportunity with the latest data if possible.
if ($activity->opportunity_id) {
try {
$crmService = $this->providerRegistry->get($team->crm->provider);
if (! $user->isCrmRequired()) {
$crmService->setUser($team->getOwner());
} else {
$crmService->setUser($user);
}
$crmService->syncOpportunity($activity->opportunity->crm_provider_id);
} catch (Exception $exception) {
// Move on.
}
}
$activityData = $activityService->getActivityData($this->request->user(), $activity);
return response()->json($activityData);
}
public function createRecording(Activity $activity)
{
$this->authorize('record', $activity);
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Tell Twilio to start recording this activity.
if ($activity->recording_state === Activity::RECORDING_OFF) {
$job = (new StartRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withCreated();
}
return $this->response->errorGone('Activity is already recording.');
}
public function updateRecording(Request $request, Activity $activity)
{
$this->authorize('record', $activity);
$request->validate([
'preference' => 'boolean',
'state' => [
'string',
Rule::in([
Activity::RECORDING_IN_PROGRESS,
Activity::RECORDING_PAUSED,
]),
],
]);
if ($request->has('state')) {
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Toggle the recording state between paused and resumed.
if (! $activity->isRecordingState(Activity::RECORDING_OFF)) {
$job = (new ToggleRecording($activity, $request->input('state')))
->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Recording is not toggleable.');
}
if ($request->has('preference')) {
$activity->update([
'recording_preference' => $request->input('preference') ? 1 : 0,
]);
return $this->response->withOk();
}
return $this->response->errorWrongArgs('Something went wrong');
}
public function stopRecording(Activity $activity)
{
$this->authorize('stopRecord', $activity);
// Tell Twilio to stop recording this activity.
if ($activity->isRecordingState(Activity::RECORDING_IN_PROGRESS)) {
$job = (new StopRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Activity is not recording.');
}
/**
* Add activity to this user's favorites playlist
*
* @throws AuthorizationException
*/
public function favorite(Activity $activity, PlaylistActivityRepository $playlistActivityRepository): JsonResponse
{
$this->authorize('favorite', $activity);
$user = $this->getUserFromRequest($this->request);
$favorite = $activity->wasFavoritedBy($user);
$name = $activity->activity_title ?? '';
// It needs to check at least one record.
if (! $favorite) {
$favoritePlaylist = $user->favoritePlaylist();
$playlistActivity = $playlistActivityRepository->findByBaseActivityUserAndPlaylist(
$activity,
$user,
$favoritePlaylist
);
if ($playlistActivity !== null) {
$playlistActivity->update(
// Just update, don't sort.
['start_time' => 0, 'name' => mb_strimwidth($name, 0, 100)],
);
} else {
$playlistActivity = $activity->playlistActivities()->create([
'playlist_id' => $favoritePlaylist->getId(),
'user_id' => $user->getId(),
'start_time' => 0,
'name' => mb_strimwidth($name, 0, 100),
]);
// Sort it on top.
$playlistActivity->update(
[
'sort' => $playlistActivityRepository->calculateNewSortOrder(
null,
$playlistActivity,
),
],
);
}
$playlistActivityRepository->calculateNewSortOrder(null, $playlistActivity);
return new JsonResponse([], JsonResponse::HTTP_CREATED);
}
return new JsonResponse(
[
'error' => [
'code' => AbstractResponse::CODE_CONFLICT,
'http_code' => JsonResponse::HTTP_CONFLICT,
'message' => 'Resource Already Exists',
],
],
JsonResponse::HTTP_CONFLICT,
);
}
/**
* Remove activity from this...
|
PhpStorm
|
faVsco.js – ActivityController.php
|
NULL
|
76687
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
27
3
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Http\Controllers\API;
use Carbon\Carbon;
use ChaseConey\LaravelDatadogHelper\Datadog;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Jiminny\Component\ActivityAnalytics;
use Jiminny\Component\ActivitySearch;
use Jiminny\Component\ActivitySearch\FilterDefinitionCollection;
use Jiminny\Component\PlaybackPage\Comments\Services\ActivityCommentService;
use Jiminny\Component\Queue\Constants;
use Jiminny\Contracts\Nudge\NudgeFactoryInterface;
use Jiminny\Contracts\Playlist\PlaylistTrackFactoryInterface;
use Jiminny\Contracts\Repositories\PlaylistActivityRepository;
use Jiminny\Contracts\Services\Crm\ServiceInterface;
use Jiminny\Enums\TeamSetting;
use Jiminny\Events\Activities\AiAutomation\ActivityProspectAdded;
use Jiminny\Events\Activities\Coaching\Coached;
use Jiminny\Contracts\Services\Crm\SupportsObjectTypeParseInterface;
use Jiminny\Exceptions\LogicException;
use Jiminny\Exceptions\SocialAccountTokenInvalidException;
use Jiminny\Http\Controllers\API\BaseController as Controller;
use Jiminny\Http\Controllers\CommentContextInterface;
use Jiminny\Http\Responses\Api\AbstractResponse;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Http\Serializers\JsonSerializer;
use Jiminny\Http\Transformers\ActivityCommentTransformer;
use Jiminny\Http\Transformers\ActivityTopicTriggerTransformer;
use Jiminny\Http\Transformers\ActivityTransformer;
use Jiminny\Http\Transformers\AvailabilityNotificationTransformer;
use Jiminny\Http\Transformers\CoachingFeedbackTransformer;
use Jiminny\Http\Transformers\CoachingSectionsTransformer;
use Jiminny\Http\Transformers\SearchTransformer;
use Jiminny\Http\Transformers\StatsTransformer;
use Jiminny\Jobs\Crm\SaveActivity;
use Jiminny\Jobs\Crm\UpdateStage;
use Jiminny\Jobs\Telephony\StartRecording;
use Jiminny\Jobs\Telephony\StopRecording;
use Jiminny\Jobs\Telephony\ToggleRecording;
use Jiminny\Models\Account;
use Jiminny\Models\Activity;
use Jiminny\Models\Activity\CoachRequest;
use Jiminny\Models\Activity\Comment;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\Activity\SearchFilter;
use Jiminny\Models\Activity\Share;
use Jiminny\Models\CoachingFeedback;
use Jiminny\Models\CoachingSection;
use Jiminny\Models\CoachingSectionCriterion;
use Jiminny\Models\CoachingSectionFeedback;
use Jiminny\Models\Contact;
use Jiminny\Models\Crm\Field;
use Jiminny\Models\Crm\FieldData;
use Jiminny\Models\Crm\Layout;
use Jiminny\Models\Crm\LayoutEntity;
use Jiminny\Models\Feature\FeatureEnum;
use Jiminny\Models\LanguageDialect;
use Jiminny\Models\Lead;
use Jiminny\Models\Nudge;
use Jiminny\Models\PlaybookCategory;
use Jiminny\Models\Playlist;
use Jiminny\Models\Stage;
use Jiminny\Models\Team;
use Jiminny\Models\Track;
use Jiminny\Models\User;
use Jiminny\Repositories\AjReportsRepository;
use Jiminny\Repositories\CoachingFeedbackRepository;
use Jiminny\Repositories\ElasticActivityRepository;
use Jiminny\Repositories\TeamRepository;
use Jiminny\Rules\CrmReference;
use Jiminny\Rules\MultidimensionalArrayMaxCharRule;
use Jiminny\Services\ActivityService;
use Jiminny\Services\Crm\ProviderRegistry;
use Jiminny\Services\PlaybackService;
use Jiminny\Services\UserService;
use Jiminny\VO\Repository\OnDemandActivitySearch\Criteria;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Sentry;
use Symfony\Component\HttpFoundation;
final class ActivityController extends Controller implements CommentContextInterface
{
// Number of minutes to look back on activities. i.e. a timeout on activity duration.
private const LOOK_BACK = 180;
public function __construct(
private ProviderRegistry $providerRegistry,
private ActivityService $activityService,
Response $response,
private UserService $userService,
private ActivitySearch\Service\ActivitySearch $activitySearch,
private NudgeFactoryInterface $nudgeFactory,
private ActivityCommentService $activityCommentService,
private LoggerInterface $logger,
private readonly CoachingFeedbackRepository $coachingFeedbackRepository,
private readonly TeamRepository $teamRepository,
private readonly AjReportsRepository $ajReportsRepository,
) {
parent::__construct($response);
}
public static function getCommentImplementation(): string
{
return Comment::class;
}
public function delete()
{
$this->request->validate([
'*' => 'uuid:activities',
]);
$deletedIds = [];
foreach ($this->request->all() as $activityId) {
$activity = Activity::idOrUuId($activityId);
try {
if ($this->authorize('delete', $activity)) {
$activity->delete();
$deletedIds[] = $activityId;
\Log::info('Soft deleted activity ' . $activity->id_string . ' by user ' . $this->getUser()->id);
}
} catch (AuthorizationException $authorizationException) {
// They didn't have permission.
}
}
return $this->response->withArray($deletedIds);
}
public function update(Request $request, Activity $activity)
{
$this->authorize('updateMetadata', $activity);
$request->validate([
'title' => 'string|max:250',
'category_id' => 'uuid:playbook_categories',
'language' => [
new In(
LanguageDialect::query()
->with('language')
->cursor()
->map(static function (LanguageDialect $languageDialect): string {
return $languageDialect->getLanguageLocale();
})
->all()
),
],
]);
if ($request->has('title')) {
$activity->title = $request->input('title');
}
if ($request->has('category_id')) {
$category = PlaybookCategory::uuid($request->input('category_id'));
if ($category->playbook->team_id !== $request->user()->team_id) {
return $this->response->errorNotFound('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
if ($request->has('language')) {
if (! $activity->isInProgress()) {
return $this->response->withError(
'Activity language can only be set while the meeting is in progress.',
400
);
}
$activity->setLanguageCode($request->input('language'));
}
$activity->save();
return $this->response->withOk();
}
// XXX: This should be merged with the update method.
/**
* @param Activity $activity
*
* @throws AuthorizationException
* @throws SocialAccountTokenInvalidException
*
* @return mixed
*/
public function summarize(Activity $activity): mixed
{
$this->logger->info('[Log Activity] Summarizing activity ', [
'activityId' => $activity->getUuid(),
'payload' => $this->request->all(),
]);
$this->authorize('update', $activity);
$this->logger->info('[Log Activity] Validating summary');
// Validate the payload.
$this->validateSummary($activity);
// All objects must belong to this team.
/** @var User $user */
$user = $this->request->user();
$team = $user->getTeam();
$crmService = $this->providerRegistry->get($team->crm->provider);
try {
$crmUser = $user;
if ($user->isCrmRequired() === false) {
$crmUser = $team->owner;
}
$crmService->setUser($crmUser);
} catch (SocialAccountTokenInvalidException $accountTokenInvalidException) {
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($accountTokenInvalidException->getMessage());
}
$rawEntities = $this->request->input('entities');
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid(
$this->request->input('layout_id')
);
// Delay execution of CRM jobs to avoid locking issues.
$jobDelay = 0;
// If we have arrived from a notification, mark it as read.
$notificationId = $this->request->input('nId');
if ($notificationId) {
$notification = $user->unreadNotifications->where('id', $notificationId)->first();
if ($notification) {
$notification->markAsRead();
}
}
$title = $this->request->input('title');
$prospects = $this->request->input('prospects');
$opportunityId = $this->request->input('opportunity_id');
$stageId = $this->request->input('stage_id');
$categoryId = $this->request->input('category_id');
$summary = $this->request->input('summary');
$crmProviderId = $this->request->input('crm_id');
$isInternal = $this->request->input('is_internal') ?? false;
$lead = null;
$category = null;
$account = null;
$contact = null;
$opportunity = null;
$stage = null;
$callStage = null;
foreach ($prospects as $prospectData) {
$objectId = $prospectData['id'];
if ($objectId === null) {
continue;
}
$objectType = $prospectData['type'];
$this->logger->info('debug', ['prospect_data' => $prospectData]);
try {
if ($objectType === null) {
$this->logger->info('no object type');
if ($crmService instanceof SupportsObjectTypeParseInterface) {
$objectType = $crmService->parseObjectType($objectId);
}
}
switch ($objectType) {
case 'lead':
$this->logger->info('Processing lead');
/** @var Lead|null $lead */
$lead = $team->crm->leads()->where('crm_provider_id', $objectId)->first();
// Lead does not exist locally, import it.
if ($lead === null) {
$this->logger->info('Lead does not exist locally');
/** @var Lead $lead */
$lead = $crmService->syncLead($objectId);
}
$this->logger->info('Lead found', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
if ($stageId === null) {
$this->logger->info('Stage ID is null');
// If it was not provided, just assume it is the current stage.
$callStage = $lead->stage;
break;
}
$this->logger->info('Looking for stage');
// Determine if they have changed the stage.
/** @var Stage $stage */
$stage = $team->crm->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_LEAD)
->firstOrFail();
$this->logger->info('Stage found', ['stageId' => $stage->id, 'lead_stage' => $lead->stage_id]);
if ($lead->stage_id && $lead->stage_id !== $stage->id) {
$this->logger->info('Stage has changed');
// Storage current stage on activity.
$callStage = $lead->stage;
// The stage has changed, update in remote CRM.
dispatch(new UpdateStage($activity, $lead, $callStage, $stage));
$this->logger->info(
sprintf(
'[%s] User changing lead stage from %s to %s',
$crmService->getDisplayName(),
$callStage->getName(),
$stage->getName()
),
[
'user' => $user->getUuid(),
'lead' => $lead->getUuid(),
]
);
} else {
$this->logger->info('Stage has not changed');
// Stage remains as current.
$callStage = $stage;
}
break;
case 'account':
$this->logger->info('Processing account');
// If the object is not a lead, it should be an account.
$account = $team->crm->accounts()->where('crm_provider_id', $objectId)->first();
// Account does not exist locally, import it.
if ($account === null) {
$this->logger->info('Account does not exist locally');
$account = $crmService->syncAccount($objectId);
}
$this->logger->info('Account found', ['accountId' => $account->id]);
break;
case 'contact':
$this->logger->info('processing contact');
$contact = $team->crm->contacts()->where('crm_provider_id', $objectId)->first();
// Contact does not exist locally, import it.
if (! $contact instanceof Contact) {
$this->logger->info('contact does not exist locally');
$contact = $crmService->syncContact($objectId);
}
$this->logger->info('resolving account');
$account = $this->resolveAccount($team, $contact, $crmService, $prospects);
break;
}
// If they have specified an opportunity, retrieve this with stage.
if ($opportunityId) {
$this->logger->info('opportunity id is set');
$opportunity = $team->crm->opportunities()->where('crm_provider_id', $opportunityId)->first();
// Opportunity does not exist locally, import it.
if ($opportunity === null) {
$this->logger->info('opportunity does not exist locally');
$opportunity = $crmService->syncOpportunity($opportunityId);
}
if ($stageId === null) {
$this->logger->info('stage id is null');
// If it was not provided, just assume it is the current stage.
$callStage = $opportunity->stage ?? null;
} else {
$this->logger->info('looking for stage');
/** @var ?Stage $opportunityStage */
$opportunityStage = $team->crm
->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_OPPORTUNITY)
->first();
// There is a chance we still cannot import this opportunity.
if ($opportunityStage !== null && $opportunity !== null && $opportunity->stage_id !== $opportunityStage->id) {
$this->logger->info('opportunity stage has changed');
// Storage current stage on activity.
$callStage = $opportunity->stage;
dispatch(new UpdateStage($activity, $opportunity, $callStage, $opportunityStage));
$this->logger->info(
sprintf(
'[%s] User changing opportunity stage from %s to %s',
$crmService->getDisplayName(),
$callStage->name,
$opportunityStage->name
),
[
'userId' => $user->id_string,
'opportunityId' => $opportunity->id_string,
]
);
} else {
$this->logger->info('opportunity stage has not changed');
// Stage remains as current.
$callStage = $opportunityStage;
}
}
}
if ($crmProviderId) {
// Cast $crmProviderId to string otherwise it won't use database index for some records
$linkedActivity = Activity::where('crm_provider_id', (string) $crmProviderId)->first();
// Check if this activity has already been assigned to a different activity.
if ($linkedActivity && $linkedActivity->id !== $activity->id) {
throw new InvalidArgumentException(
'Sorry, the linked task has already been logged under a different call. '
. 'Please choose another linked task.'
);
}
}
} catch (InvalidArgumentException $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($exception->getMessage());
} catch (Exception $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorInternalError(
'Sorry, an error occurred. Please try again or reach out to support if the problem continues.'
);
}
}
if ($categoryId) {
$category = PlaybookCategory::uuid($categoryId);
if ($category->playbook->team_id !== $team->id) {
throw new InvalidArgumentException('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
$this->logger->info('Prospect data', [
'lead_id' => $lead?->getId(),
'account_id' => $account?->getId(),
'contact_id' => $contact?->getId(),
'opportunity_id' => $opportunity?->getId(),
'stage_id' => $stage?->getId(),
]);
if ($title) {
$activity->title = $title;
}
if ($summary) {
$activity->summary = $summary;
}
if ($crmProviderId) {
$activity->crm_provider_id = $crmProviderId;
}
if ($callStage) {
$this->logger->info('Setting stage id', ['stageId' => $callStage->id]);
$activity->stage_id = $callStage->id;
}
if ($lead) {
$this->logger->info('Setting lead id', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
// If we are changed from an account > lead, unset the account data.
$this->logger->info('Unsetting account id, opportunity id, contact id, value');
$activity->account_id = null;
$activity->opportunity_id = null;
$activity->contact_id = null;
$activity->value = null;
}
if ($account) {
$this->logger->info('Setting account id', ['accountId' => $account->id]);
$activity->account_id = $account->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('unsetting lead id');
$activity->lead_id = null;
// Unset the contact if switching different accounts. Will be set up below if still applicable.
if (! $team->hasFeature(FeatureEnum::LINK_ACTIVITY_TO_MULTIPLE_PROSPECTS) || empty($contact)) {
$this->logger->info('Unsetting contact id');
$activity->contact_id = null;
}
}
if ($opportunity) {
$this->logger->info('setting opportunity id', ['opportunityId' => $opportunity->id]);
$this->logger->info('unsetting lead id');
$activity->opportunity_id = $opportunity->id;
$activity->value = $opportunity->value;
// If we are changed from an lead > account, unset the lead data.
$activity->lead_id = null;
}
if ($contact) {
$this->logger->info('setting contact id', ['contactId' => $contact->id]);
$activity->contact_id = $contact->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('Unsetting lead id');
$activity->lead_id = null;
}
$activity->is_internal = $isInternal;
$activity->save();
$activity->refresh();
$this->logger->notice('Activity saved', [
'activity_id' => $activity->getId(),
'lead_id' => $activity->lead_id,
'account_id' => $activity->account_id,
'contact_id' => $activity->contact_id,
'opportunity_id' => $activity->opportunity_id,
'stage_id' => $activity->stage_id,
'crm_provider_id' => $activity->getCrmProviderId(),
]);
// Store entities as field data on the activity.
$updatedData = $this->storeEntities($crmService, $activity, $layout, $rawEntities);
if ($activity->isLoggable()) {
// Follow-up Task or Event data.
$followupData = $this->fetchFollowupEntities($crmService, $layout, $rawEntities);
$this->logger->info('CRM LOG manual log triggered', [
'activityId' => $activity->getUuid(),
'followupData' => $followupData,
'userId' => $user->getUuid(),
]);
// Store data in the CRM.
// ++add check for crm_required
$job = new SaveActivity($activity, $followupData);
if ($updatedData) {
$job->delay(Carbon::now()->addMinutes($jobDelay));
}
dispatch($job);
// Manually dispatch log for Opportunity or Prospect added
if ($activity->hasOpportunity() || $activity->hasProspect()) {
event(new ActivityProspectAdded(
activity: $activity,
eventSource: 'manually-log-crm-data'
));
}
}
return $this->response->withOk();
}
/**
* Extract any activity data to be upserted in the Lead/Opportunity/Task etc in the CRM.
*
* @param ServiceInterface $service
* @param Activity $activity
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function storeEntities(ServiceInterface $service, Activity $activity, Layout $layout, array $entities): array
{
$updatedData = [];
$existingData = $activity->data()->get();
// We need to delete any existing data to overwrite with latest values.
$activity->data()->delete();
$layoutEntities = $layout->entities()
->with('field', 'parent')
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->get();
/** @var LayoutEntity $entity */
foreach ($layoutEntities as $entity) {
// If the user has provided a value for this entity
if (array_key_exists($entity->id_string, $entities)) {
$value = $entities[$entity->id_string];
// Convert raw data into values that the CRM can consume.
if ($value) {
$value = $service->normalizeValue($entity->field->type, $value);
}
// Check the field is part of the activity-summary section.
if ($entity->parent && $entity->parent->label === 'activity-summary' && $value) {
// This is the internal database ID, not the external CRM ID.
$objectId = null;
switch ($entity->field->object_type) {
case Field::OBJECT_ACCOUNT:
$objectId = $activity->account_id;
break;
case Field::OBJECT_CONTACT:
$objectId = $activity->contact_id;
break;
case Field::OBJECT_OPPORTUNITY:
$objectId = $activity->opportunity_id;
break;
case Field::OBJECT_LEAD:
$objectId = $activity->lead_id;
break;
case Field::OBJECT_TASK:
case Field::OBJECT_EVENT:
$objectId = $activity->id;
break;
}
if ($objectId) {
/** @var FieldData $data */
$data = $activity->data()->create([
'crm_layout_entity_id' => $entity->id,
'crm_field_id' => $entity->crm_field_id,
'object_type' => $entity->field->object_type,
'object_id' => $objectId,
'value' => $value,
]);
// Never send read-only field data to the CRM.
if ($entity->read_only === false && $entity->is_visible) {
$existingValue = $existingData
->where('crm_layout_entity_id', $entity->id)
->where('crm_field_id', $entity->crm_field_id)
->where('object_type', $entity->field->object_type)
->where('object_id', $objectId)
->first();
// If the field was actually changed, we need to reflect this in the CRM too.
if ($existingValue === null || $existingValue->value !== $value) {
$updatedData[] = $data->id;
}
}
}
}
}
}
return $updatedData;
}
/**
* Extract any followup data to be dispatched in a job to create a new Task/Event in the CRM.
*
* @param ServiceInterface $crmService
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function fetchFollowupEntities(ServiceInterface $crmService, Layout $layout, array $entities): array
{
$fieldData = [];
foreach ($entities as $entityId => $value) {
// Only bother with fields that have a value.
if ($value) {
// Extract the entity from the UUID. Check the field is valid and part of the follow-up section.
$entity = $layout->entities()
->uuid($entityId, false)
->whereHas('parent', function ($query) {
$query->where('label', 'follow-up');
})
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->first();
if ($entity) {
// Convert raw data into values that the CRM can consume.
$value = $crmService->normalizeValue($entity->field->type, $value);
// Add the field and value to the payload.
$fieldData += [
$entity->field->crm_provider_id => $value,
];
}
}
}
return $fieldData;
}
/**
* @param Activity $activity
*/
private function validateSummary(Activity $activity): void
{
$team = $activity->user->team;
$crmProvider = $team->crm->provider;
$attributes = [];
$rules = [
'layout_id' => 'required|uuid:crm_layouts,crm_configuration_id,' . $team->crm_id,
'title' => 'string|max:250',
'prospects' => 'required|array',
'opportunity_id' => new CrmReference($crmProvider),
'category_id' => 'uuid:playbook_categories|required_unless:is_internal,true',
'stage_id' => 'uuid:stages,team_id,' . $team->id, // Todo: move to proper validator
'summary' => 'max:50000',
'nId' => 'exists:notifications,id',
'crm_id' => new CrmReference($crmProvider),
'entities' => 'array',
'is_internal' => 'boolean',
];
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid($this->request->input('layout_id'));
// Only validate fields, not headers etc. If not loggable, we don't care about follow-up section.
$entities = $layout->entities()
->where('read_only', 0)
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->whereHas('parent', function ($query) use ($activity) {
if ($activity->isLoggable() === false) {
$query->where('label', '<>', 'follow-up');
}
});
$isInternal = $this->request->input('is_internal', false);
foreach ($entities->get() as $entity) {
$rules += $this->buildFieldValidator($entity, $isInternal);
$attributes += $this->buildFieldMessage($entity);
}
$this->request->validate($rules, [], $attributes);
}
private function buildFieldValidator(LayoutEntity $entity, bool $isInternal): array
{
return [
'entities.' . $entity->id_string => $entity->getValidator($isInternal),
];
}
/**
* @param LayoutEntity $entity
*
* @return array
*/
private function buildFieldMessage(LayoutEntity $entity): array
{
$label = $entity->label;
if ($label === null) {
$label = $entity->field->label;
}
return [
'entities.' . $entity->id_string => $label,
];
}
public function search(Request $request, ElasticActivityRepository $repository): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->debugLog(
$user,
'User extracted from request',
['user' => $user->getId(), 'tz' => $user->getTimezone()]
);
$searchCriteria = Criteria::createFromRequest($request->all(), $user->getTimezone());
$this->debugLog(
$user,
'ActivitySearch criteria built',
['searchCriteria' => $searchCriteria]
);
$filterSet = $this->activitySearch->getHomepageFilterSet($searchCriteria, $user);
$this->debugLog($user, 'FilterSet built', ['filterSet' => $filterSet]);
$this->validateSearch($request, $filterSet);
$this->debugLog($user, 'Request validated');
$searchResponse = $repository->onDemandSearch($user, $searchCriteria, $filterSet);
/** @var Collection<Activity> $activities */
$activities = $searchResponse['results'];
$this->debugLog($user, 'Activities ES response extracted');
$hideInternalMeetingsSetting = $this->teamRepository->getTeamSettingByTeamId(
$user->getTeamId(),
TeamSetting::HIDE_INTERNAL_SCHEDULED_MEETINGS->name(),
);
if ($hideInternalMeetingsSetting?->getValue() === '1') {
$activities = $activities->filter(function (Activity $activity) {
if ($activity->is_internal && empty($activity->actual_start_time)) {
return false;
}
return true;
});
}
$this->debugLog($user, 'Internal meetings (?!) filtered');
$this->response->getManager()
->parseIncludes([
'category',
'organizer.group',
'prospect',
'stage',
'opportunity',
'stats',
'scorecards',
'masterTrack',
'activeParticipants',
'notification',
])
->setSerializer(new JsonSerializer());
$transformerExcludes = $this->request->input('exclude');
if ($transformerExcludes) {
$this->response->getManager()->parseExcludes($transformerExcludes);
}
$this->debugLog($user, 'Response Manager (?!) applied');
$transformer = new ActivityTransformer();
$transformer->setConsumer($user);
$this->debugLog($user, 'Activity Transformer added');
$resource = new \League\Fractal\Resource\Collection($activities, $transformer);
$page = $searchCriteria->getPageNumber();
$this->debugLog($user, 'Search criteria page number called', ['page' => $page]);
$histogram = array_pluck(array_get($searchResponse, 'histogram.buckets', []), 'doc_count', 'key_as_string');
$this->debugLog($user, 'Histogram generated. Response is ready.', ['histogram' => $histogram]);
return $this->response->withArray([
'pagination' => [
'total' => $searchResponse['totalHits'],
'current' => $page,
'prev' => max($page - 1, 1),
'next' => $page + 1,
],
'results' => $this->response->getManager()->createData($resource)->toArray(),
'histogram' => $histogram,
]);
}
private function debugLog(User $user, string $logMessage, ?array $context = []): void
{
// Debug for Learning People Only
if ($user->getTeamId() !== 260) {
return;
}
Log::notice(
sprintf('[activity-search-controller] %s', $logMessage),
$context
);
}
/** @throws ValidationException */
private function validateSearch(Request $request, FilterDefinitionCollection $filterSet, ?string $prefix = null): void
{
$rules = [
'exclude' => 'array',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
];
if ($prefix !== null && mb_strpos($prefix, '.') !== false) {
$rules[rtrim($prefix, '.')] = sprintf(
'required|array|max:%d',
$filterSet->count()
);
}
$validationRules = $filterSet->getValidationRules($prefix)
->merge($rules)
->all();
$request->validate($validationRules);
}
public function createActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$search = $this->updateOrCreateActivitySearch($request);
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function updateActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('update', $search);
$this->updateOrCreateActivitySearch($request, $search);
return $this->response->withOk();
}
private function storeNamedSearchFilters(
Collection $request,
Search $search,
FilterDefinitionCollection $filterSet,
?string $prefix = null,
): self {
$arrayTypeProperties = $filterSet
->getPropertyTypes([
FilterDefinitionCollection::PROPERTY_TYPE_ARRAY,
])
->all();
$supportedRequestProperties = $filterSet->getSupportedRequestProperties($prefix);
foreach ($supportedRequestProperties as $requestPropertyName) {
if (! array_has($request, $requestPropertyName)) {
continue;
}
/** @var string|string[] $propertyValue */
$propertyValue = array_get($request, $requestPropertyName);
$propertyName = $prefix === null
? $requestPropertyName
: mb_substr($requestPropertyName, mb_strlen($prefix));
$isArrayType = array_has($arrayTypeProperties, $propertyName);
if (! $isArrayType) {
/** @var string $requestPropertyValue */
$search->filters()->updateOrCreate(
[
'filter' => $propertyName,
],
[
'value' => $propertyValue,
]
);
continue;
}
/** @var string[] $requestPropertyValue */
/** @var SearchFilter[]|Collection $existingFilterValues */
$existingFilterValuesKeyed = $search->filters()
->where('filter', $propertyName)
->get()
->keyBy('id');
// Iterate over values provided as request parameters
foreach ($propertyValue as $value) {
/** @var SearchFilter|null $valueFilter */
$valueFilter = $search->filters()
->where(
[
'filter' => $propertyName,
'value' => $value,
]
)
->first();
if ($valueFilter !== null) {
// Remove filter value pair from list to be deleted
$existingFilterValuesKeyed->forget($valueFilter->id);
} else {
// Add new filter/value pair
$search->filters()->updateOrCreate([
'filter' => $propertyName,
'value' => $value,
]);
}
}
// Delete filter value pairs for this filter that no longer exist in request parameters
foreach ($existingFilterValuesKeyed as $existingFilter) {
$existingFilter->delete();
}
}
/** @var Collection<int, SearchFilter> $filtersKeyed */
$filtersKeyed = $search->filters()->get()->keyBy('filter');
// wipe removed filters from this search
foreach ($filtersKeyed as $filterName => $filter) {
if (array_has($request, $prefix . $filterName)) {
continue;
}
// Remove all filter values for this filter
$search->filters()->where('filter', $filterName)->delete();
}
return $this;
}
/**
* @throws AuthorizationException
*/
public function fetchActivitySearch(
Search $search,
Request $request,
SearchTransformer $searchTransformer,
): JsonResponse {
$this->authorize('view', $search);
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function listActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withCollection(
$user->searches()->get(),
$searchTransformer
->withConsumer($user)
);
}
/**
* Deletes a saved search
*
* @param Request $request
* @param Search $search
*
* @throws Exception
*
* @return JsonResponse
*/
public function deleteActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('delete', $search);
$ajReportCount = $this->ajReportsRepository->countActiveReportsBySavedSearch($search->getId());
if ($ajReportCount > 0) {
return $this->response->errorWrongArgs(
"This saved search is used by {$ajReportCount} active AJ report(s). "
. 'Please remove or update those reports before deleting this saved search.'
);
}
$search->filters()->delete();
$search->delete();
return $this->response->withOk();
}
public function live(Request $request, ElasticActivityRepository $repository): JsonResponse
{
$user = $this->getUserFromRequest($request);
$this->request->validate([
'sort_direction' => 'in:asc,desc',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
]);
$activities = $repository->getLiveCoachingEligibleActivities(
user: $user,
lookBackMinutes: self::LOOK_BACK,
limit: (int) $this->request->input('limit', 25),
page: (int) $this->request->input('page', 1),
sortBy: ['actual_start_time', 'scheduled_start_time'],
sortDirection: (string) $this->request->input('sort_direction', 'asc'),
);
$this->response
->getManager()
->parseIncludes(['organizer.group', 'prospect'])
->setSerializer(new JsonSerializer());
return $this->response->withCollection($activities, new ActivityTransformer());
}
/**
* @param Activity $activity
*
* @throws AuthorizationException
*
* @return mixed
*/
public function show(Activity $activity, ActivityService $activityService): JsonResponse
{
$this->authorize('show', $activity);
$user = $activity->getUser();
$team = $user->getTeam();
// Sync the opportunity with the latest data if possible.
if ($activity->opportunity_id) {
try {
$crmService = $this->providerRegistry->get($team->crm->provider);
if (! $user->isCrmRequired()) {
$crmService->setUser($team->getOwner());
} else {
$crmService->setUser($user);
}
$crmService->syncOpportunity($activity->opportunity->crm_provider_id);
} catch (Exception $exception) {
// Move on.
}
}
$activityData = $activityService->getActivityData($this->request->user(), $activity);
return response()->json($activityData);
}
public function createRecording(Activity $activity)
{
$this->authorize('record', $activity);
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Tell Twilio to start recording this activity.
if ($activity->recording_state === Activity::RECORDING_OFF) {
$job = (new StartRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withCreated();
}
return $this->response->errorGone('Activity is already recording.');
}
public function updateRecording(Request $request, Activity $activity)
{
$this->authorize('record', $activity);
$request->validate([
'preference' => 'boolean',
'state' => [
'string',
Rule::in([
Activity::RECORDING_IN_PROGRESS,
Activity::RECORDING_PAUSED,
]),
],
]);
if ($request->has('state')) {
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Toggle the recording state between paused and resumed.
if (! $activity->isRecordingState(Activity::RECORDING_OFF)) {
$job = (new ToggleRecording($activity, $request->input('state')))
->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Recording is not toggleable.');
}
if ($request->has('preference')) {
$activity->update([
'recording_preference' => $request->input('preference') ? 1 : 0,
]);
return $this->response->withOk();
}
return $this->response->errorWrongArgs('Something went wrong');
}
public function stopRecording(Activity $activity)
{
$this->authorize('stopRecord', $activity);
// Tell Twilio to stop recording this activity.
if ($activity->isRecordingState(Activity::RECORDING_IN_PROGRESS)) {
$job = (new StopRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Activity is not recording.');
}
/**
* Add activity to this user's favorites playlist
*
* @throws AuthorizationException
*/
public function favorite(Activity $activity, PlaylistActivityRepository $playlistActivityRepository): JsonResponse
{
$this->authorize('favorite', $activity);
$user = $this->getUserFromRequest($this->request);
$favorite = $activity->wasFavoritedBy($user);
$name = $activity->activity_title ?? '';
// It needs to check at least one record.
if (! $favorite) {
$favoritePlaylist = $user->favoritePlaylist();
$playlistActivity = $playlistActivityRepository->findByBaseActivityUserAndPlaylist(
$activity,
$user,
$favoritePlaylist
);
if ($playlistActivity !== null) {
$playlistActivity->update(
// Just update, don't sort.
['start_time' => 0, 'name' => mb_strimwidth($name, 0, 100)],
);
} else {
$playlistActivity = $activity->playlistActivities()->create([
'playlist_id' => $favoritePlaylist->getId(),
'user_id' => $user->getId(),
'start_time' => 0,
'name' => mb_strimwidth($name, 0, 100),
]);
// Sort it on top.
$playlistActivity->update(
[
'sort' => $playlistActivityRepository->calculateNewSortOrder(
null,
$playlistActivity,
),
],
);
}
$playlistActivityRepository->calculateNewSortOrder(null, $playlistActivity);
return new JsonResponse([], JsonResponse::HTTP_CREATED);
}
return new JsonResponse(
[
'error' => [
'code' => AbstractResponse::CODE_CONFLICT,
'http_code' => JsonResponse::HTTP_CONFLICT,
'message' => 'Resource Already Exists',
],
],
JsonResponse::HTTP_CONFLICT,
);
}
/**
* Remove activity from this...
|
PhpStorm
|
faVsco.js – ActivityController.php
|
NULL
|
76688
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
1
41
3
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Http\Controllers\API;
use Carbon\Carbon;
use ChaseConey\LaravelDatadogHelper\Datadog;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Jiminny\Component\ActivityAnalytics;
use Jiminny\Component\ActivitySearch;
use Jiminny\Component\ActivitySearch\FilterDefinitionCollection;
use Jiminny\Component\PlaybackPage\Comments\Services\ActivityCommentService;
use Jiminny\Component\Queue\Constants;
use Jiminny\Contracts\Nudge\NudgeFactoryInterface;
use Jiminny\Contracts\Playlist\PlaylistTrackFactoryInterface;
use Jiminny\Contracts\Repositories\PlaylistActivityRepository;
use Jiminny\Contracts\Services\Crm\ServiceInterface;
use Jiminny\Enums\TeamSetting;
use Jiminny\Events\Activities\AiAutomation\ActivityProspectAdded;
use Jiminny\Events\Activities\Coaching\Coached;
use Jiminny\Contracts\Services\Crm\SupportsObjectTypeParseInterface;
use Jiminny\Exceptions\LogicException;
use Jiminny\Exceptions\SocialAccountTokenInvalidException;
use Jiminny\Http\Controllers\API\BaseController as Controller;
use Jiminny\Http\Controllers\CommentContextInterface;
use Jiminny\Http\Responses\Api\AbstractResponse;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Http\Serializers\JsonSerializer;
use Jiminny\Http\Transformers\ActivityCommentTransformer;
use Jiminny\Http\Transformers\ActivityTopicTriggerTransformer;
use Jiminny\Http\Transformers\ActivityTransformer;
use Jiminny\Http\Transformers\AvailabilityNotificationTransformer;
use Jiminny\Http\Transformers\CoachingFeedbackTransformer;
use Jiminny\Http\Transformers\CoachingSectionsTransformer;
use Jiminny\Http\Transformers\SearchTransformer;
use Jiminny\Http\Transformers\StatsTransformer;
use Jiminny\Jobs\Crm\SaveActivity;
use Jiminny\Jobs\Crm\UpdateStage;
use Jiminny\Jobs\Telephony\StartRecording;
use Jiminny\Jobs\Telephony\StopRecording;
use Jiminny\Jobs\Telephony\ToggleRecording;
use Jiminny\Models\Account;
use Jiminny\Models\Activity;
use Jiminny\Models\Activity\CoachRequest;
use Jiminny\Models\Activity\Comment;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\Activity\SearchFilter;
use Jiminny\Models\Activity\Share;
use Jiminny\Models\CoachingFeedback;
use Jiminny\Models\CoachingSection;
use Jiminny\Models\CoachingSectionCriterion;
use Jiminny\Models\CoachingSectionFeedback;
use Jiminny\Models\Contact;
use Jiminny\Models\Crm\Field;
use Jiminny\Models\Crm\FieldData;
use Jiminny\Models\Crm\Layout;
use Jiminny\Models\Crm\LayoutEntity;
use Jiminny\Models\Feature\FeatureEnum;
use Jiminny\Models\LanguageDialect;
use Jiminny\Models\Lead;
use Jiminny\Models\Nudge;
use Jiminny\Models\PlaybookCategory;
use Jiminny\Models\Playlist;
use Jiminny\Models\Stage;
use Jiminny\Models\Team;
use Jiminny\Models\Track;
use Jiminny\Models\User;
use Jiminny\Repositories\AjReportsRepository;
use Jiminny\Repositories\CoachingFeedbackRepository;
use Jiminny\Repositories\ElasticActivityRepository;
use Jiminny\Repositories\TeamRepository;
use Jiminny\Rules\CrmReference;
use Jiminny\Rules\MultidimensionalArrayMaxCharRule;
use Jiminny\Services\ActivityService;
use Jiminny\Services\Crm\ProviderRegistry;
use Jiminny\Services\PlaybackService;
use Jiminny\Services\UserService;
use Jiminny\VO\Repository\OnDemandActivitySearch\Criteria;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Sentry;
use Symfony\Component\HttpFoundation;
final class ActivityController extends Controller implements CommentContextInterface
{
// Number of minutes to look back on activities. i.e. a timeout on activity duration.
private const LOOK_BACK = 180;
public function __construct(
private ProviderRegistry $providerRegistry,
private ActivityService $activityService,
Response $response,
private UserService $userService,
private ActivitySearch\Service\ActivitySearch $activitySearch,
private NudgeFactoryInterface $nudgeFactory,
private ActivityCommentService $activityCommentService,
private LoggerInterface $logger,
private readonly CoachingFeedbackRepository $coachingFeedbackRepository,
private readonly TeamRepository $teamRepository,
private readonly AjReportsRepository $ajReportsRepository,
) {
parent::__construct($response);
}
public static function getCommentImplementation(): string
{
return Comment::class;
}
public function delete()
{
$this->request->validate([
'*' => 'uuid:activities',
]);
$deletedIds = [];
foreach ($this->request->all() as $activityId) {
$activity = Activity::idOrUuId($activityId);
try {
if ($this->authorize('delete', $activity)) {
$activity->delete();
$deletedIds[] = $activityId;
\Log::info('Soft deleted activity ' . $activity->id_string . ' by user ' . $this->getUser()->id);
}
} catch (AuthorizationException $authorizationException) {
// They didn't have permission.
}
}
return $this->response->withArray($deletedIds);
}
public function update(Request $request, Activity $activity)
{
$this->authorize('updateMetadata', $activity);
$request->validate([
'title' => 'string|max:250',
'category_id' => 'uuid:playbook_categories',
'language' => [
new In(
LanguageDialect::query()
->with('language')
->cursor()
->map(static function (LanguageDialect $languageDialect): string {
return $languageDialect->getLanguageLocale();
})
->all()
),
],
]);
if ($request->has('title')) {
$activity->title = $request->input('title');
}
if ($request->has('category_id')) {
$category = PlaybookCategory::uuid($request->input('category_id'));
if ($category->playbook->team_id !== $request->user()->team_id) {
return $this->response->errorNotFound('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
if ($request->has('language')) {
if (! $activity->isInProgress()) {
return $this->response->withError(
'Activity language can only be set while the meeting is in progress.',
400
);
}
$activity->setLanguageCode($request->input('language'));
}
$activity->save();
return $this->response->withOk();
}
// XXX: This should be merged with the update method.
/**
* @param Activity $activity
*
* @throws AuthorizationException
* @throws SocialAccountTokenInvalidException
*
* @return mixed
*/
public function summarize(Activity $activity): mixed
{
$this->logger->info('[Log Activity] Summarizing activity ', [
'activityId' => $activity->getUuid(),
'payload' => $this->request->all(),
]);
$this->authorize('update', $activity);
$this->logger->info('[Log Activity] Validating summary');
// Validate the payload.
$this->validateSummary($activity);
// All objects must belong to this team.
/** @var User $user */
$user = $this->request->user();
$team = $user->getTeam();
$crmService = $this->providerRegistry->get($team->crm->provider);
try {
$crmUser = $user;
if ($user->isCrmRequired() === false) {
$crmUser = $team->owner;
}
$crmService->setUser($crmUser);
} catch (SocialAccountTokenInvalidException $accountTokenInvalidException) {
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($accountTokenInvalidException->getMessage());
}
$rawEntities = $this->request->input('entities');
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid(
$this->request->input('layout_id')
);
// Delay execution of CRM jobs to avoid locking issues.
$jobDelay = 0;
// If we have arrived from a notification, mark it as read.
$notificationId = $this->request->input('nId');
if ($notificationId) {
$notification = $user->unreadNotifications->where('id', $notificationId)->first();
if ($notification) {
$notification->markAsRead();
}
}
$title = $this->request->input('title');
$prospects = $this->request->input('prospects');
$opportunityId = $this->request->input('opportunity_id');
$stageId = $this->request->input('stage_id');
$categoryId = $this->request->input('category_id');
$summary = $this->request->input('summary');
$crmProviderId = $this->request->input('crm_id');
$isInternal = $this->request->input('is_internal') ?? false;
$lead = null;
$category = null;
$account = null;
$contact = null;
$opportunity = null;
$stage = null;
$callStage = null;
foreach ($prospects as $prospectData) {
$objectId = $prospectData['id'];
if ($objectId === null) {
continue;
}
$objectType = $prospectData['type'];
$this->logger->info('debug', ['prospect_data' => $prospectData]);
try {
if ($objectType === null) {
$this->logger->info('no object type');
if ($crmService instanceof SupportsObjectTypeParseInterface) {
$objectType = $crmService->parseObjectType($objectId);
}
}
switch ($objectType) {
case 'lead':
$this->logger->info('Processing lead');
/** @var Lead|null $lead */
$lead = $team->crm->leads()->where('crm_provider_id', $objectId)->first();
// Lead does not exist locally, import it.
if ($lead === null) {
$this->logger->info('Lead does not exist locally');
/** @var Lead $lead */
$lead = $crmService->syncLead($objectId);
}
$this->logger->info('Lead found', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
if ($stageId === null) {
$this->logger->info('Stage ID is null');
// If it was not provided, just assume it is the current stage.
$callStage = $lead->stage;
break;
}
$this->logger->info('Looking for stage');
// Determine if they have changed the stage.
/** @var Stage $stage */
$stage = $team->crm->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_LEAD)
->firstOrFail();
$this->logger->info('Stage found', ['stageId' => $stage->id, 'lead_stage' => $lead->stage_id]);
if ($lead->stage_id && $lead->stage_id !== $stage->id) {
$this->logger->info('Stage has changed');
// Storage current stage on activity.
$callStage = $lead->stage;
// The stage has changed, update in remote CRM.
dispatch(new UpdateStage($activity, $lead, $callStage, $stage));
$this->logger->info(
sprintf(
'[%s] User changing lead stage from %s to %s',
$crmService->getDisplayName(),
$callStage->getName(),
$stage->getName()
),
[
'user' => $user->getUuid(),
'lead' => $lead->getUuid(),
]
);
} else {
$this->logger->info('Stage has not changed');
// Stage remains as current.
$callStage = $stage;
}
break;
case 'account':
$this->logger->info('Processing account');
// If the object is not a lead, it should be an account.
$account = $team->crm->accounts()->where('crm_provider_id', $objectId)->first();
// Account does not exist locally, import it.
if ($account === null) {
$this->logger->info('Account does not exist locally');
$account = $crmService->syncAccount($objectId);
}
$this->logger->info('Account found', ['accountId' => $account->id]);
break;
case 'contact':
$this->logger->info('processing contact');
$contact = $team->crm->contacts()->where('crm_provider_id', $objectId)->first();
// Contact does not exist locally, import it.
if (! $contact instanceof Contact) {
$this->logger->info('contact does not exist locally');
$contact = $crmService->syncContact($objectId);
}
$this->logger->info('resolving account');
$account = $this->resolveAccount($team, $contact, $crmService, $prospects);
break;
}
// If they have specified an opportunity, retrieve this with stage.
if ($opportunityId) {
$this->logger->info('opportunity id is set');
$opportunity = $team->crm->opportunities()->where('crm_provider_id', $opportunityId)->first();
// Opportunity does not exist locally, import it.
if ($opportunity === null) {
$this->logger->info('opportunity does not exist locally');
$opportunity = $crmService->syncOpportunity($opportunityId);
}
if ($stageId === null) {
$this->logger->info('stage id is null');
// If it was not provided, just assume it is the current stage.
$callStage = $opportunity->stage ?? null;
} else {
$this->logger->info('looking for stage');
/** @var ?Stage $opportunityStage */
$opportunityStage = $team->crm
->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_OPPORTUNITY)
->first();
// There is a chance we still cannot import this opportunity.
if ($opportunityStage !== null && $opportunity !== null && $opportunity->stage_id !== $opportunityStage->id) {
$this->logger->info('opportunity stage has changed');
// Storage current stage on activity.
$callStage = $opportunity->stage;
dispatch(new UpdateStage($activity, $opportunity, $callStage, $opportunityStage));
$this->logger->info(
sprintf(
'[%s] User changing opportunity stage from %s to %s',
$crmService->getDisplayName(),
$callStage->name,
$opportunityStage->name
),
[
'userId' => $user->id_string,
'opportunityId' => $opportunity->id_string,
]
);
} else {
$this->logger->info('opportunity stage has not changed');
// Stage remains as current.
$callStage = $opportunityStage;
}
}
}
if ($crmProviderId) {
// Cast $crmProviderId to string otherwise it won't use database index for some records
$linkedActivity = Activity::where('crm_provider_id', (string) $crmProviderId)->first();
// Check if this activity has already been assigned to a different activity.
if ($linkedActivity && $linkedActivity->id !== $activity->id) {
throw new InvalidArgumentException(
'Sorry, the linked task has already been logged under a different call. '
. 'Please choose another linked task.'
);
}
}
} catch (InvalidArgumentException $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($exception->getMessage());
} catch (Exception $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorInternalError(
'Sorry, an error occurred. Please try again or reach out to support if the problem continues.'
);
}
}
if ($categoryId) {
$category = PlaybookCategory::uuid($categoryId);
if ($category->playbook->team_id !== $team->id) {
throw new InvalidArgumentException('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
$this->logger->info('Prospect data', [
'lead_id' => $lead?->getId(),
'account_id' => $account?->getId(),
'contact_id' => $contact?->getId(),
'opportunity_id' => $opportunity?->getId(),
'stage_id' => $stage?->getId(),
]);
if ($title) {
$activity->title = $title;
}
if ($summary) {
$activity->summary = $summary;
}
if ($crmProviderId) {
$activity->crm_provider_id = $crmProviderId;
}
if ($callStage) {
$this->logger->info('Setting stage id', ['stageId' => $callStage->id]);
$activity->stage_id = $callStage->id;
}
if ($lead) {
$this->logger->info('Setting lead id', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
// If we are changed from an account > lead, unset the account data.
$this->logger->info('Unsetting account id, opportunity id, contact id, value');
$activity->account_id = null;
$activity->opportunity_id = null;
$activity->contact_id = null;
$activity->value = null;
}
if ($account) {
$this->logger->info('Setting account id', ['accountId' => $account->id]);
$activity->account_id = $account->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('unsetting lead id');
$activity->lead_id = null;
// Unset the contact if switching different accounts. Will be set up below if still applicable.
if (! $team->hasFeature(FeatureEnum::LINK_ACTIVITY_TO_MULTIPLE_PROSPECTS) || empty($contact)) {
$this->logger->info('Unsetting contact id');
$activity->contact_id = null;
}
}
if ($opportunity) {
$this->logger->info('setting opportunity id', ['opportunityId' => $opportunity->id]);
$this->logger->info('unsetting lead id');
$activity->opportunity_id = $opportunity->id;
$activity->value = $opportunity->value;
// If we are changed from an lead > account, unset the lead data.
$activity->lead_id = null;
}
if ($contact) {
$this->logger->info('setting contact id', ['contactId' => $contact->id]);
$activity->contact_id = $contact->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('Unsetting lead id');
$activity->lead_id = null;
}
$activity->is_internal = $isInternal;
$activity->save();
$activity->refresh();
$this->logger->notice('Activity saved', [
'activity_id' => $activity->getId(),
'lead_id' => $activity->lead_id,
'account_id' => $activity->account_id,
'contact_id' => $activity->contact_id,
'opportunity_id' => $activity->opportunity_id,
'stage_id' => $activity->stage_id,
'crm_provider_id' => $activity->getCrmProviderId(),
]);
// Store entities as field data on the activity.
$updatedData = $this->storeEntities($crmService, $activity, $layout, $rawEntities);
if ($activity->isLoggable()) {
// Follow-up Task or Event data.
$followupData = $this->fetchFollowupEntities($crmService, $layout, $rawEntities);
$this->logger->info('CRM LOG manual log triggered', [
'activityId' => $activity->getUuid(),
'followupData' => $followupData,
'userId' => $user->getUuid(),
]);
// Store data in the CRM.
// ++add check for crm_required
$job = new SaveActivity($activity, $followupData);
if ($updatedData) {
$job->delay(Carbon::now()->addMinutes($jobDelay));
}
dispatch($job);
// Manually dispatch log for Opportunity or Prospect added
if ($activity->hasOpportunity() || $activity->hasProspect()) {
event(new ActivityProspectAdded(
activity: $activity,
eventSource: 'manually-log-crm-data'
));
}
}
return $this->response->withOk();
}
/**
* Extract any activity data to be upserted in the Lead/Opportunity/Task etc in the CRM.
*
* @param ServiceInterface $service
* @param Activity $activity
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function storeEntities(ServiceInterface $service, Activity $activity, Layout $layout, array $entities): array
{
$updatedData = [];
$existingData = $activity->data()->get();
// We need to delete any existing data to overwrite with latest values.
$activity->data()->delete();
$layoutEntities = $layout->entities()
->with('field', 'parent')
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->get();
/** @var LayoutEntity $entity */
foreach ($layoutEntities as $entity) {
// If the user has provided a value for this entity
if (array_key_exists($entity->id_string, $entities)) {
$value = $entities[$entity->id_string];
// Convert raw data into values that the CRM can consume.
if ($value) {
$value = $service->normalizeValue($entity->field->type, $value);
}
// Check the field is part of the activity-summary section.
if ($entity->parent && $entity->parent->label === 'activity-summary' && $value) {
// This is the internal database ID, not the external CRM ID.
$objectId = null;
switch ($entity->field->object_type) {
case Field::OBJECT_ACCOUNT:
$objectId = $activity->account_id;
break;
case Field::OBJECT_CONTACT:
$objectId = $activity->contact_id;
break;
case Field::OBJECT_OPPORTUNITY:
$objectId = $activity->opportunity_id;
break;
case Field::OBJECT_LEAD:
$objectId = $activity->lead_id;
break;
case Field::OBJECT_TASK:
case Field::OBJECT_EVENT:
$objectId = $activity->id;
break;
}
if ($objectId) {
/** @var FieldData $data */
$data = $activity->data()->create([
'crm_layout_entity_id' => $entity->id,
'crm_field_id' => $entity->crm_field_id,
'object_type' => $entity->field->object_type,
'object_id' => $objectId,
'value' => $value,
]);
// Never send read-only field data to the CRM.
if ($entity->read_only === false && $entity->is_visible) {
$existingValue = $existingData
->where('crm_layout_entity_id', $entity->id)
->where('crm_field_id', $entity->crm_field_id)
->where('object_type', $entity->field->object_type)
->where('object_id', $objectId)
->first();
// If the field was actually changed, we need to reflect this in the CRM too.
if ($existingValue === null || $existingValue->value !== $value) {
$updatedData[] = $data->id;
}
}
}
}
}
}
return $updatedData;
}
/**
* Extract any followup data to be dispatched in a job to create a new Task/Event in the CRM.
*
* @param ServiceInterface $crmService
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function fetchFollowupEntities(ServiceInterface $crmService, Layout $layout, array $entities): array
{
$fieldData = [];
foreach ($entities as $entityId => $value) {
// Only bother with fields that have a value.
if ($value) {
// Extract the entity from the UUID. Check the field is valid and part of the follow-up section.
$entity = $layout->entities()
->uuid($entityId, false)
->whereHas('parent', function ($query) {
$query->where('label', 'follow-up');
})
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->first();
if ($entity) {
// Convert raw data into values that the CRM can consume.
$value = $crmService->normalizeValue($entity->field->type, $value);
// Add the field and value to the payload.
$fieldData += [
$entity->field->crm_provider_id => $value,
];
}
}
}
return $fieldData;
}
/**
* @param Activity $activity
*/
private function validateSummary(Activity $activity): void
{
$team = $activity->user->team;
$crmProvider = $team->crm->provider;
$attributes = [];
$rules = [
'layout_id' => 'required|uuid:crm_layouts,crm_configuration_id,' . $team->crm_id,
'title' => 'string|max:250',
'prospects' => 'required|array',
'opportunity_id' => new CrmReference($crmProvider),
'category_id' => 'uuid:playbook_categories|required_unless:is_internal,true',
'stage_id' => 'uuid:stages,team_id,' . $team->id, // Todo: move to proper validator
'summary' => 'max:50000',
'nId' => 'exists:notifications,id',
'crm_id' => new CrmReference($crmProvider),
'entities' => 'array',
'is_internal' => 'boolean',
];
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid($this->request->input('layout_id'));
// Only validate fields, not headers etc. If not loggable, we don't care about follow-up section.
$entities = $layout->entities()
->where('read_only', 0)
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->whereHas('parent', function ($query) use ($activity) {
if ($activity->isLoggable() === false) {
$query->where('label', '<>', 'follow-up');
}
});
$isInternal = $this->request->input('is_internal', false);
foreach ($entities->get() as $entity) {
$rules += $this->buildFieldValidator($entity, $isInternal);
$attributes += $this->buildFieldMessage($entity);
}
$this->request->validate($rules, [], $attributes);
}
private function buildFieldValidator(LayoutEntity $entity, bool $isInternal): array
{
return [
'entities.' . $entity->id_string => $entity->getValidator($isInternal),
];
}
/**
* @param LayoutEntity $entity
*
* @return array
*/
private function buildFieldMessage(LayoutEntity $entity): array
{
$label = $entity->label;
if ($label === null) {
$label = $entity->field->label;
}
return [
'entities.' . $entity->id_string => $label,
];
}
public function search(Request $request, ElasticActivityRepository $repository): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->debugLog(
$user,
'User extracted from request',
['user' => $user->getId(), 'tz' => $user->getTimezone()]
);
$searchCriteria = Criteria::createFromRequest($request->all(), $user->getTimezone());
$this->debugLog(
$user,
'ActivitySearch criteria built',
['searchCriteria' => $searchCriteria]
);
$filterSet = $this->activitySearch->getHomepageFilterSet($searchCriteria, $user);
$this->debugLog($user, 'FilterSet built', ['filterSet' => $filterSet]);
$this->validateSearch($request, $filterSet);
$this->debugLog($user, 'Request validated');
$searchResponse = $repository->onDemandSearch($user, $searchCriteria, $filterSet);
/** @var Collection<Activity> $activities */
$activities = $searchResponse['results'];
$this->debugLog($user, 'Activities ES response extracted');
$hideInternalMeetingsSetting = $this->teamRepository->getTeamSettingByTeamId(
$user->getTeamId(),
TeamSetting::HIDE_INTERNAL_SCHEDULED_MEETINGS->name(),
);
if ($hideInternalMeetingsSetting?->getValue() === '1') {
$activities = $activities->filter(function (Activity $activity) {
if ($activity->is_internal && empty($activity->actual_start_time)) {
return false;
}
return true;
});
}
$this->debugLog($user, 'Internal meetings (?!) filtered');
$this->response->getManager()
->parseIncludes([
'category',
'organizer.group',
'prospect',
'stage',
'opportunity',
'stats',
'scorecards',
'masterTrack',
'activeParticipants',
'notification',
])
->setSerializer(new JsonSerializer());
$transformerExcludes = $this->request->input('exclude');
if ($transformerExcludes) {
$this->response->getManager()->parseExcludes($transformerExcludes);
}
$this->debugLog($user, 'Response Manager (?!) applied');
$transformer = new ActivityTransformer();
$transformer->setConsumer($user);
$this->debugLog($user, 'Activity Transformer added');
$resource = new \League\Fractal\Resource\Collection($activities, $transformer);
$page = $searchCriteria->getPageNumber();
$this->debugLog($user, 'Search criteria page number called', ['page' => $page]);
$histogram = array_pluck(array_get($searchResponse, 'histogram.buckets', []), 'doc_count', 'key_as_string');
$this->debugLog($user, 'Histogram generated. Response is ready.', ['histogram' => $histogram]);
return $this->response->withArray([
'pagination' => [
'total' => $searchResponse['totalHits'],
'current' => $page,
'prev' => max($page - 1, 1),
'next' => $page + 1,
],
'results' => $this->response->getManager()->createData($resource)->toArray(),
'histogram' => $histogram,
]);
}
private function debugLog(User $user, string $logMessage, ?array $context = []): void
{
// Debug for Learning People Only
if ($user->getTeamId() !== 260) {
return;
}
Log::notice(
sprintf('[activity-search-controller] %s', $logMessage),
$context
);
}
/** @throws ValidationException */
private function validateSearch(Request $request, FilterDefinitionCollection $filterSet, ?string $prefix = null): void
{
$rules = [
'exclude' => 'array',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
];
if ($prefix !== null && mb_strpos($prefix, '.') !== false) {
$rules[rtrim($prefix, '.')] = sprintf(
'required|array|max:%d',
$filterSet->count()
);
}
$validationRules = $filterSet->getValidationRules($prefix)
->merge($rules)
->all();
$request->validate($validationRules);
}
public function createActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$search = $this->updateOrCreateActivitySearch($request);
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function updateActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('update', $search);
$this->updateOrCreateActivitySearch($request, $search);
return $this->response->withOk();
}
private function storeNamedSearchFilters(
Collection $request,
Search $search,
FilterDefinitionCollection $filterSet,
?string $prefix = null,
): self {
$arrayTypeProperties = $filterSet
->getPropertyTypes([
FilterDefinitionCollection::PROPERTY_TYPE_ARRAY,
])
->all();
$supportedRequestProperties = $filterSet->getSupportedRequestProperties($prefix);
foreach ($supportedRequestProperties as $requestPropertyName) {
if (! array_has($request, $requestPropertyName)) {
continue;
}
/** @var string|string[] $propertyValue */
$propertyValue = array_get($request, $requestPropertyName);
$propertyName = $prefix === null
? $requestPropertyName
: mb_substr($requestPropertyName, mb_strlen($prefix));
$isArrayType = array_has($arrayTypeProperties, $propertyName);
if (! $isArrayType) {
/** @var string $requestPropertyValue */
$search->filters()->updateOrCreate(
[
'filter' => $propertyName,
],
[
'value' => $propertyValue,
]
);
continue;
}
/** @var string[] $requestPropertyValue */
/** @var SearchFilter[]|Collection $existingFilterValues */
$existingFilterValuesKeyed = $search->filters()
->where('filter', $propertyName)
->get()
->keyBy('id');
// Iterate over values provided as request parameters
foreach ($propertyValue as $value) {
/** @var SearchFilter|null $valueFilter */
$valueFilter = $search->filters()
->where(
[
'filter' => $propertyName,
'value' => $value,
]
)
->first();
if ($valueFilter !== null) {
// Remove filter value pair from list to be deleted
$existingFilterValuesKeyed->forget($valueFilter->id);
} else {
// Add new filter/value pair
$search->filters()->updateOrCreate([
'filter' => $propertyName,
'value' => $value,
]);
}
}
// Delete filter value pairs for this filter that no longer exist in request parameters
foreach ($existingFilterValuesKeyed as $existingFilter) {
$existingFilter->delete();
}
}
/** @var Collection<int, SearchFilter> $filtersKeyed */
$filtersKeyed = $search->filters()->get()->keyBy('filter');
// wipe removed filters from this search
foreach ($filtersKeyed as $filterName => $filter) {
if (array_has($request, $prefix . $filterName)) {
continue;
}
// Remove all filter values for this filter
$search->filters()->where('filter', $filterName)->delete();
}
return $this;
}
/**
* @throws AuthorizationException
*/
public function fetchActivitySearch(
Search $search,
Request $request,
SearchTransformer $searchTransformer,
): JsonResponse {
$this->authorize('view', $search);
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function listActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withCollection(
$user->searches()->get(),
$searchTransformer
->withConsumer($user)
);
}
/**
* Deletes a saved search
*
* @param Request $request
* @param Search $search
*
* @throws Exception
*
* @return JsonResponse
*/
public function deleteActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('delete', $search);
$ajReportCount = $this->ajReportsRepository->countActiveReportsBySavedSearch($search->getId());
if ($ajReportCount > 0) {
return $this->response->errorWrongArgs(
"This saved search is used by {$ajReportCount} active AJ report(s). "
. 'Please remove or update those reports before deleting this saved search.'
);
}
$search->filters()->delete();
$search->delete();
return $this->response->withOk();
}
public function live(Request $request, ElasticActivityRepository $repository): JsonResponse
{
$user = $this->getUserFromRequest($request);
$this->request->validate([
'sort_direction' => 'in:asc,desc',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
]);
$activities = $repository->getLiveCoachingEligibleActivities(
user: $user,
lookBackMinutes: self::LOOK_BACK,
limit: (int) $this->request->input('limit', 25),
page: (int) $this->request->input('page', 1),
sortBy: ['actual_start_time', 'scheduled_start_time'],
sortDirection: (string) $this->request->input('sort_direction', 'asc'),
);
$this->response
->getManager()
->parseIncludes(['organizer.group', 'prospect'])
->setSerializer(new JsonSerializer());
return $this->response->withCollection($activities, new ActivityTransformer());
}
/**
* @param Activity $activity
*
* @throws AuthorizationException
*
* @return mixed
*/
public function show(Activity $activity, ActivityService $activityService): JsonResponse
{
$this->authorize('show', $activity);
$user = $activity->getUser();
$team = $user->getTeam();
// Sync the opportunity with the latest data if possible.
if ($activity->opportunity_id) {
try {
$crmService = $this->providerRegistry->get($team->crm->provider);
if (! $user->isCrmRequired()) {
$crmService->setUser($team->getOwner());
} else {
$crmService->setUser($user);
}
$crmService->syncOpportunity($activity->opportunity->crm_provider_id);
} catch (Exception $exception) {
// Move on.
}
}
$activityData = $activityService->getActivityData($this->request->user(), $activity);
return response()->json($activityData);
}
public function createRecording(Activity $activity)
{
$this->authorize('record', $activity);
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Tell Twilio to start recording this activity.
if ($activity->recording_state === Activity::RECORDING_OFF) {
$job = (new StartRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withCreated();
}
return $this->response->errorGone('Activity is already recording.');
}
public function updateRecording(Request $request, Activity $activity)
{
$this->authorize('record', $activity);
$request->validate([
'preference' => 'boolean',
'state' => [
'string',
Rule::in([
Activity::RECORDING_IN_PROGRESS,
Activity::RECORDING_PAUSED,
]),
],
]);
if ($request->has('state')) {
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Toggle the recording state between paused and resumed.
if (! $activity->isRecordingState(Activity::RECORDING_OFF)) {
$job = (new ToggleRecording($activity, $request->input('state')))
->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Recording is not toggleable.');
}
if ($request->has('preference')) {
$activity->update([
'recording_preference' => $request->input('preference') ? 1 : 0,
]);
return $this->response->withOk();
}
return $this->response->errorWrongArgs('Something went wrong');
}
public function stopRecording(Activity $activity)
{
$this->authorize('stopRecord', $activity);
// Tell Twilio to stop recording this activity.
if ($activity->isRecordingState(Activity::RECORDING_IN_PROGRESS)) {
$job = (new StopRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Activity is not recording.');
}
/**
* Add activity to this user's favorites playlist
*
* @throws AuthorizationException
*/
public function favorite(Activity $activity, PlaylistActivityRepository $playlistActivityRepository): JsonResponse
{
$this->authorize('favorite', $activity);
$user = $this->getUserFromRequest($this->request);
$favorite = $activity->wasFavoritedBy($user);
$name = $activity->activity_title ?? '';
// It needs to check at least one record.
if (! $favorite) {
$favoritePlaylist = $user->favoritePlaylist();
$playlistActivity = $playlistActivityRepository->findByBaseActivityUserAndPlaylist(
$activity,
$user,
$favoritePlaylist
);
if ($playlistActivity !== null) {
$playlistActivity->update(
// Just update, don't sort.
['start_time' => 0, 'name' => mb_strimwidth($name, 0, 100)],
);
} else {
$playlistActivity = $activity->playlistActivities()->create([
'playlist_id' => $favoritePlaylist->getId(),
'user_id' => $user->getId(),
'start_time' => 0,
'name' => mb_strimwidth($name, 0, 100),
]);
// Sort it on top.
$playlistActivity->update(
[
'sort' => $playlistActivityRepository->calculateNewSortOrder(
null,
$playlistActivity,
),
],
);
}
$playlistActivityRepository->calculateNewSortOrder(null, $playlistActivity);
return new JsonResponse([], JsonResponse::HTTP_CREATED);
}
return new JsonResponse(
[
'error' => [
'code' => AbstractResponse::CODE_CONFLICT,
'http_code' => JsonResponse::HTTP_CONFLICT,
'message' => 'Resource Already Exists',
],
],
JsonResponse::HTTP_CONFLICT,
);
}
/**
* Remove activity from th...
|
PhpStorm
|
faVsco.js – ActivityController.php
|
NULL
|
76689
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
1
41
3
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Http\Controllers\API;
use Carbon\Carbon;
use ChaseConey\LaravelDatadogHelper\Datadog;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Jiminny\Component\ActivityAnalytics;
use Jiminny\Component\ActivitySearch;
use Jiminny\Component\ActivitySearch\FilterDefinitionCollection;
use Jiminny\Component\PlaybackPage\Comments\Services\ActivityCommentService;
use Jiminny\Component\Queue\Constants;
use Jiminny\Contracts\Nudge\NudgeFactoryInterface;
use Jiminny\Contracts\Playlist\PlaylistTrackFactoryInterface;
use Jiminny\Contracts\Repositories\PlaylistActivityRepository;
use Jiminny\Contracts\Services\Crm\ServiceInterface;
use Jiminny\Enums\TeamSetting;
use Jiminny\Events\Activities\AiAutomation\ActivityProspectAdded;
use Jiminny\Events\Activities\Coaching\Coached;
use Jiminny\Contracts\Services\Crm\SupportsObjectTypeParseInterface;
use Jiminny\Exceptions\LogicException;
use Jiminny\Exceptions\SocialAccountTokenInvalidException;
use Jiminny\Http\Controllers\API\BaseController as Controller;
use Jiminny\Http\Controllers\CommentContextInterface;
use Jiminny\Http\Responses\Api\AbstractResponse;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Http\Serializers\JsonSerializer;
use Jiminny\Http\Transformers\ActivityCommentTransformer;
use Jiminny\Http\Transformers\ActivityTopicTriggerTransformer;
use Jiminny\Http\Transformers\ActivityTransformer;
use Jiminny\Http\Transformers\AvailabilityNotificationTransformer;
use Jiminny\Http\Transformers\CoachingFeedbackTransformer;
use Jiminny\Http\Transformers\CoachingSectionsTransformer;
use Jiminny\Http\Transformers\SearchTransformer;
use Jiminny\Http\Transformers\StatsTransformer;
use Jiminny\Jobs\Crm\SaveActivity;
use Jiminny\Jobs\Crm\UpdateStage;
use Jiminny\Jobs\Telephony\StartRecording;
use Jiminny\Jobs\Telephony\StopRecording;
use Jiminny\Jobs\Telephony\ToggleRecording;
use Jiminny\Models\Account;
use Jiminny\Models\Activity;
use Jiminny\Models\Activity\CoachRequest;
use Jiminny\Models\Activity\Comment;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\Activity\SearchFilter;
use Jiminny\Models\Activity\Share;
use Jiminny\Models\CoachingFeedback;
use Jiminny\Models\CoachingSection;
use Jiminny\Models\CoachingSectionCriterion;
use Jiminny\Models\CoachingSectionFeedback;
use Jiminny\Models\Contact;
use Jiminny\Models\Crm\Field;
use Jiminny\Models\Crm\FieldData;
use Jiminny\Models\Crm\Layout;
use Jiminny\Models\Crm\LayoutEntity;
use Jiminny\Models\Feature\FeatureEnum;
use Jiminny\Models\LanguageDialect;
use Jiminny\Models\Lead;
use Jiminny\Models\Nudge;
use Jiminny\Models\PlaybookCategory;
use Jiminny\Models\Playlist;
use Jiminny\Models\Stage;
use Jiminny\Models\Team;
use Jiminny\Models\Track;
use Jiminny\Models\User;
use Jiminny\Repositories\AjReportsRepository;
use Jiminny\Repositories\CoachingFeedbackRepository;
use Jiminny\Repositories\ElasticActivityRepository;
use Jiminny\Repositories\TeamRepository;
use Jiminny\Rules\CrmReference;
use Jiminny\Rules\MultidimensionalArrayMaxCharRule;
use Jiminny\Services\ActivityService;
use Jiminny\Services\Crm\ProviderRegistry;
use Jiminny\Services\PlaybackService;
use Jiminny\Services\UserService;
use Jiminny\VO\Repository\OnDemandActivitySearch\Criteria;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Sentry;
use Symfony\Component\HttpFoundation;
final class ActivityController extends Controller implements CommentContextInterface
{
// Number of minutes to look back on activities. i.e. a timeout on activity duration.
private const LOOK_BACK = 180;
public function __construct(
private ProviderRegistry $providerRegistry,
private ActivityService $activityService,
Response $response,
private UserService $userService,
private ActivitySearch\Service\ActivitySearch $activitySearch,
private NudgeFactoryInterface $nudgeFactory,
private ActivityCommentService $activityCommentService,
private LoggerInterface $logger,
private readonly CoachingFeedbackRepository $coachingFeedbackRepository,
private readonly TeamRepository $teamRepository,
private readonly AjReportsRepository $ajReportsRepository,
) {
parent::__construct($response);
}
public static function getCommentImplementation(): string
{
return Comment::class;
}
public function delete()
{
$this->request->validate([
'*' => 'uuid:activities',
]);
$deletedIds = [];
foreach ($this->request->all() as $activityId) {
$activity = Activity::idOrUuId($activityId);
try {
if ($this->authorize('delete', $activity)) {
$activity->delete();
$deletedIds[] = $activityId;
\Log::info('Soft deleted activity ' . $activity->id_string . ' by user ' . $this->getUser()->id);
}
} catch (AuthorizationException $authorizationException) {
// They didn't have permission.
}
}
return $this->response->withArray($deletedIds);
}
public function update(Request $request, Activity $activity)
{
$this->authorize('updateMetadata', $activity);
$request->validate([
'title' => 'string|max:250',
'category_id' => 'uuid:playbook_categories',
'language' => [
new In(
LanguageDialect::query()
->with('language')
->cursor()
->map(static function (LanguageDialect $languageDialect): string {
return $languageDialect->getLanguageLocale();
})
->all()
),
],
]);
if ($request->has('title')) {
$activity->title = $request->input('title');
}
if ($request->has('category_id')) {
$category = PlaybookCategory::uuid($request->input('category_id'));
if ($category->playbook->team_id !== $request->user()->team_id) {
return $this->response->errorNotFound('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
if ($request->has('language')) {
if (! $activity->isInProgress()) {
return $this->response->withError(
'Activity language can only be set while the meeting is in progress.',
400
);
}
$activity->setLanguageCode($request->input('language'));
}
$activity->save();
return $this->response->withOk();
}
// XXX: This should be merged with the update method.
/**
* @param Activity $activity
*
* @throws AuthorizationException
* @throws SocialAccountTokenInvalidException
*
* @return mixed
*/
public function summarize(Activity $activity): mixed
{
$this->logger->info('[Log Activity] Summarizing activity ', [
'activityId' => $activity->getUuid(),
'payload' => $this->request->all(),
]);
$this->authorize('update', $activity);
$this->logger->info('[Log Activity] Validating summary');
// Validate the payload.
$this->validateSummary($activity);
// All objects must belong to this team.
/** @var User $user */
$user = $this->request->user();
$team = $user->getTeam();
$crmService = $this->providerRegistry->get($team->crm->provider);
try {
$crmUser = $user;
if ($user->isCrmRequired() === false) {
$crmUser = $team->owner;
}
$crmService->setUser($crmUser);
} catch (SocialAccountTokenInvalidException $accountTokenInvalidException) {
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($accountTokenInvalidException->getMessage());
}
$rawEntities = $this->request->input('entities');
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid(
$this->request->input('layout_id')
);
// Delay execution of CRM jobs to avoid locking issues.
$jobDelay = 0;
// If we have arrived from a notification, mark it as read.
$notificationId = $this->request->input('nId');
if ($notificationId) {
$notification = $user->unreadNotifications->where('id', $notificationId)->first();
if ($notification) {
$notification->markAsRead();
}
}
$title = $this->request->input('title');
$prospects = $this->request->input('prospects');
$opportunityId = $this->request->input('opportunity_id');
$stageId = $this->request->input('stage_id');
$categoryId = $this->request->input('category_id');
$summary = $this->request->input('summary');
$crmProviderId = $this->request->input('crm_id');
$isInternal = $this->request->input('is_internal') ?? false;
$lead = null;
$category = null;
$account = null;
$contact = null;
$opportunity = null;
$stage = null;
$callStage = null;
foreach ($prospects as $prospectData) {
$objectId = $prospectData['id'];
if ($objectId === null) {
continue;
}
$objectType = $prospectData['type'];
$this->logger->info('debug', ['prospect_data' => $prospectData]);
try {
if ($objectType === null) {
$this->logger->info('no object type');
if ($crmService instanceof SupportsObjectTypeParseInterface) {
$objectType = $crmService->parseObjectType($objectId);
}
}
switch ($objectType) {
case 'lead':
$this->logger->info('Processing lead');
/** @var Lead|null $lead */
$lead = $team->crm->leads()->where('crm_provider_id', $objectId)->first();
// Lead does not exist locally, import it.
if ($lead === null) {
$this->logger->info('Lead does not exist locally');
/** @var Lead $lead */
$lead = $crmService->syncLead($objectId);
}
$this->logger->info('Lead found', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
if ($stageId === null) {
$this->logger->info('Stage ID is null');
// If it was not provided, just assume it is the current stage.
$callStage = $lead->stage;
break;
}
$this->logger->info('Looking for stage');
// Determine if they have changed the stage.
/** @var Stage $stage */
$stage = $team->crm->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_LEAD)
->firstOrFail();
$this->logger->info('Stage found', ['stageId' => $stage->id, 'lead_stage' => $lead->stage_id]);
if ($lead->stage_id && $lead->stage_id !== $stage->id) {
$this->logger->info('Stage has changed');
// Storage current stage on activity.
$callStage = $lead->stage;
// The stage has changed, update in remote CRM.
dispatch(new UpdateStage($activity, $lead, $callStage, $stage));
$this->logger->info(
sprintf(
'[%s] User changing lead stage from %s to %s',
$crmService->getDisplayName(),
$callStage->getName(),
$stage->getName()
),
[
'user' => $user->getUuid(),
'lead' => $lead->getUuid(),
]
);
} else {
$this->logger->info('Stage has not changed');
// Stage remains as current.
$callStage = $stage;
}
break;
case 'account':
$this->logger->info('Processing account');
// If the object is not a lead, it should be an account.
$account = $team->crm->accounts()->where('crm_provider_id', $objectId)->first();
// Account does not exist locally, import it.
if ($account === null) {
$this->logger->info('Account does not exist locally');
$account = $crmService->syncAccount($objectId);
}
$this->logger->info('Account found', ['accountId' => $account->id]);
break;
case 'contact':
$this->logger->info('processing contact');
$contact = $team->crm->contacts()->where('crm_provider_id', $objectId)->first();
// Contact does not exist locally, import it.
if (! $contact instanceof Contact) {
$this->logger->info('contact does not exist locally');
$contact = $crmService->syncContact($objectId);
}
$this->logger->info('resolving account');
$account = $this->resolveAccount($team, $contact, $crmService, $prospects);
break;
}
// If they have specified an opportunity, retrieve this with stage.
if ($opportunityId) {
$this->logger->info('opportunity id is set');
$opportunity = $team->crm->opportunities()->where('crm_provider_id', $opportunityId)->first();
// Opportunity does not exist locally, import it.
if ($opportunity === null) {
$this->logger->info('opportunity does not exist locally');
$opportunity = $crmService->syncOpportunity($opportunityId);
}
if ($stageId === null) {
$this->logger->info('stage id is null');
// If it was not provided, just assume it is the current stage.
$callStage = $opportunity->stage ?? null;
} else {
$this->logger->info('looking for stage');
/** @var ?Stage $opportunityStage */
$opportunityStage = $team->crm
->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_OPPORTUNITY)
->first();
// There is a chance we still cannot import this opportunity.
if ($opportunityStage !== null && $opportunity !== null && $opportunity->stage_id !== $opportunityStage->id) {
$this->logger->info('opportunity stage has changed');
// Storage current stage on activity.
$callStage = $opportunity->stage;
dispatch(new UpdateStage($activity, $opportunity, $callStage, $opportunityStage));
$this->logger->info(
sprintf(
'[%s] User changing opportunity stage from %s to %s',
$crmService->getDisplayName(),
$callStage->name,
$opportunityStage->name
),
[
'userId' => $user->id_string,
'opportunityId' => $opportunity->id_string,
]
);
} else {
$this->logger->info('opportunity stage has not changed');
// Stage remains as current.
$callStage = $opportunityStage;
}
}
}
if ($crmProviderId) {
// Cast $crmProviderId to string otherwise it won't use database index for some records
$linkedActivity = Activity::where('crm_provider_id', (string) $crmProviderId)->first();
// Check if this activity has already been assigned to a different activity.
if ($linkedActivity && $linkedActivity->id !== $activity->id) {
throw new InvalidArgumentException(
'Sorry, the linked task has already been logged under a different call. '
. 'Please choose another linked task.'
);
}
}
} catch (InvalidArgumentException $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($exception->getMessage());
} catch (Exception $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorInternalError(
'Sorry, an error occurred. Please try again or reach out to support if the problem continues.'
);
}
}
if ($categoryId) {
$category = PlaybookCategory::uuid($categoryId);
if ($category->playbook->team_id !== $team->id) {
throw new InvalidArgumentException('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
$this->logger->info('Prospect data', [
'lead_id' => $lead?->getId(),
'account_id' => $account?->getId(),
'contact_id' => $contact?->getId(),
'opportunity_id' => $opportunity?->getId(),
'stage_id' => $stage?->getId(),
]);
if ($title) {
$activity->title = $title;
}
if ($summary) {
$activity->summary = $summary;
}
if ($crmProviderId) {
$activity->crm_provider_id = $crmProviderId;
}
if ($callStage) {
$this->logger->info('Setting stage id', ['stageId' => $callStage->id]);
$activity->stage_id = $callStage->id;
}
if ($lead) {
$this->logger->info('Setting lead id', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
// If we are changed from an account > lead, unset the account data.
$this->logger->info('Unsetting account id, opportunity id, contact id, value');
$activity->account_id = null;
$activity->opportunity_id = null;
$activity->contact_id = null;
$activity->value = null;
}
if ($account) {
$this->logger->info('Setting account id', ['accountId' => $account->id]);
$activity->account_id = $account->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('unsetting lead id');
$activity->lead_id = null;
// Unset the contact if switching different accounts. Will be set up below if still applicable.
if (! $team->hasFeature(FeatureEnum::LINK_ACTIVITY_TO_MULTIPLE_PROSPECTS) || empty($contact)) {
$this->logger->info('Unsetting contact id');
$activity->contact_id = null;
}
}
if ($opportunity) {
$this->logger->info('setting opportunity id', ['opportunityId' => $opportunity->id]);
$this->logger->info('unsetting lead id');
$activity->opportunity_id = $opportunity->id;
$activity->value = $opportunity->value;
// If we are changed from an lead > account, unset the lead data.
$activity->lead_id = null;
}
if ($contact) {
$this->logger->info('setting contact id', ['contactId' => $contact->id]);
$activity->contact_id = $contact->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('Unsetting lead id');
$activity->lead_id = null;
}
$activity->is_internal = $isInternal;
$activity->save();
$activity->refresh();
$this->logger->notice('Activity saved', [
'activity_id' => $activity->getId(),
'lead_id' => $activity->lead_id,
'account_id' => $activity->account_id,
'contact_id' => $activity->contact_id,
'opportunity_id' => $activity->opportunity_id,
'stage_id' => $activity->stage_id,
'crm_provider_id' => $activity->getCrmProviderId(),
]);
// Store entities as field data on the activity.
$updatedData = $this->storeEntities($crmService, $activity, $layout, $rawEntities);
if ($activity->isLoggable()) {
// Follow-up Task or Event data.
$followupData = $this->fetchFollowupEntities($crmService, $layout, $rawEntities);
$this->logger->info('CRM LOG manual log triggered', [
'activityId' => $activity->getUuid(),
'followupData' => $followupData,
'userId' => $user->getUuid(),
]);
// Store data in the CRM.
// ++add check for crm_required
$job = new SaveActivity($activity, $followupData);
if ($updatedData) {
$job->delay(Carbon::now()->addMinutes($jobDelay));
}
dispatch($job);
// Manually dispatch log for Opportunity or Prospect added
if ($activity->hasOpportunity() || $activity->hasProspect()) {
event(new ActivityProspectAdded(
activity: $activity,
eventSource: 'manually-log-crm-data'
));
}
}
return $this->response->withOk();
}
/**
* Extract any activity data to be upserted in the Lead/Opportunity/Task etc in the CRM.
*
* @param ServiceInterface $service
* @param Activity $activity
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function storeEntities(ServiceInterface $service, Activity $activity, Layout $layout, array $entities): array
{
$updatedData = [];
$existingData = $activity->data()->get();
// We need to delete any existing data to overwrite with latest values.
$activity->data()->delete();
$layoutEntities = $layout->entities()
->with('field', 'parent')
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->get();
/** @var LayoutEntity $entity */
foreach ($layoutEntities as $entity) {
// If the user has provided a value for this entity
if (array_key_exists($entity->id_string, $entities)) {
$value = $entities[$entity->id_string];
// Convert raw data into values that the CRM can consume.
if ($value) {
$value = $service->normalizeValue($entity->field->type, $value);
}
// Check the field is part of the activity-summary section.
if ($entity->parent && $entity->parent->label === 'activity-summary' && $value) {
// This is the internal database ID, not the external CRM ID.
$objectId = null;
switch ($entity->field->object_type) {
case Field::OBJECT_ACCOUNT:
$objectId = $activity->account_id;
break;
case Field::OBJECT_CONTACT:
$objectId = $activity->contact_id;
break;
case Field::OBJECT_OPPORTUNITY:
$objectId = $activity->opportunity_id;
break;
case Field::OBJECT_LEAD:
$objectId = $activity->lead_id;
break;
case Field::OBJECT_TASK:
case Field::OBJECT_EVENT:
$objectId = $activity->id;
break;
}
if ($objectId) {
/** @var FieldData $data */
$data = $activity->data()->create([
'crm_layout_entity_id' => $entity->id,
'crm_field_id' => $entity->crm_field_id,
'object_type' => $entity->field->object_type,
'object_id' => $objectId,
'value' => $value,
]);
// Never send read-only field data to the CRM.
if ($entity->read_only === false && $entity->is_visible) {
$existingValue = $existingData
->where('crm_layout_entity_id', $entity->id)
->where('crm_field_id', $entity->crm_field_id)
->where('object_type', $entity->field->object_type)
->where('object_id', $objectId)
->first();
// If the field was actually changed, we need to reflect this in the CRM too.
if ($existingValue === null || $existingValue->value !== $value) {
$updatedData[] = $data->id;
}
}
}
}
}
}
return $updatedData;
}
/**
* Extract any followup data to be dispatched in a job to create a new Task/Event in the CRM.
*
* @param ServiceInterface $crmService
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function fetchFollowupEntities(ServiceInterface $crmService, Layout $layout, array $entities): array
{
$fieldData = [];
foreach ($entities as $entityId => $value) {
// Only bother with fields that have a value.
if ($value) {
// Extract the entity from the UUID. Check the field is valid and part of the follow-up section.
$entity = $layout->entities()
->uuid($entityId, false)
->whereHas('parent', function ($query) {
$query->where('label', 'follow-up');
})
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->first();
if ($entity) {
// Convert raw data into values that the CRM can consume.
$value = $crmService->normalizeValue($entity->field->type, $value);
// Add the field and value to the payload.
$fieldData += [
$entity->field->crm_provider_id => $value,
];
}
}
}
return $fieldData;
}
/**
* @param Activity $activity
*/
private function validateSummary(Activity $activity): void
{
$team = $activity->user->team;
$crmProvider = $team->crm->provider;
$attributes = [];
$rules = [
'layout_id' => 'required|uuid:crm_layouts,crm_configuration_id,' . $team->crm_id,
'title' => 'string|max:250',
'prospects' => 'required|array',
'opportunity_id' => new CrmReference($crmProvider),
'category_id' => 'uuid:playbook_categories|required_unless:is_internal,true',
'stage_id' => 'uuid:stages,team_id,' . $team->id, // Todo: move to proper validator
'summary' => 'max:50000',
'nId' => 'exists:notifications,id',
'crm_id' => new CrmReference($crmProvider),
'entities' => 'array',
'is_internal' => 'boolean',
];
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid($this->request->input('layout_id'));
// Only validate fields, not headers etc. If not loggable, we don't care about follow-up section.
$entities = $layout->entities()
->where('read_only', 0)
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->whereHas('parent', function ($query) use ($activity) {
if ($activity->isLoggable() === false) {
$query->where('label', '<>', 'follow-up');
}
});
$isInternal = $this->request->input('is_internal', false);
foreach ($entities->get() as $entity) {
$rules += $this->buildFieldValidator($entity, $isInternal);
$attributes += $this->buildFieldMessage($entity);
}
$this->request->validate($rules, [], $attributes);
}
private function buildFieldValidator(LayoutEntity $entity, bool $isInternal): array
{
return [
'entities.' . $entity->id_string => $entity->getValidator($isInternal),
];
}
/**
* @param LayoutEntity $entity
*
* @return array
*/
private function buildFieldMessage(LayoutEntity $entity): array
{
$label = $entity->label;
if ($label === null) {
$label = $entity->field->label;
}
return [
'entities.' . $entity->id_string => $label,
];
}
public function search(Request $request, ElasticActivityRepository $repository): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->debugLog(
$user,
'User extracted from request',
['user' => $user->getId(), 'tz' => $user->getTimezone()]
);
$searchCriteria = Criteria::createFromRequest($request->all(), $user->getTimezone());
$this->debugLog(
$user,
'ActivitySearch criteria built',
['searchCriteria' => $searchCriteria]
);
$filterSet = $this->activitySearch->getHomepageFilterSet($searchCriteria, $user);
$this->debugLog($user, 'FilterSet built', ['filterSet' => $filterSet]);
$this->validateSearch($request, $filterSet);
$this->debugLog($user, 'Request validated');
$searchResponse = $repository->onDemandSearch($user, $searchCriteria, $filterSet);
/** @var Collection<Activity> $activities */
$activities = $searchResponse['results'];
$this->debugLog($user, 'Activities ES response extracted');
$hideInternalMeetingsSetting = $this->teamRepository->getTeamSettingByTeamId(
$user->getTeamId(),
TeamSetting::HIDE_INTERNAL_SCHEDULED_MEETINGS->name(),
);
if ($hideInternalMeetingsSetting?->getValue() === '1') {
$activities = $activities->filter(function (Activity $activity) {
if ($activity->is_internal && empty($activity->actual_start_time)) {
return false;
}
return true;
});
}
$this->debugLog($user, 'Internal meetings (?!) filtered');
$this->response->getManager()
->parseIncludes([
'category',
'organizer.group',
'prospect',
'stage',
'opportunity',
'stats',
'scorecards',
'masterTrack',
'activeParticipants',
'notification',
])
->setSerializer(new JsonSerializer());
$transformerExcludes = $this->request->input('exclude');
if ($transformerExcludes) {
$this->response->getManager()->parseExcludes($transformerExcludes);
}
$this->debugLog($user, 'Response Manager (?!) applied');
$transformer = new ActivityTransformer();
$transformer->setConsumer($user);
$this->debugLog($user, 'Activity Transformer added');
$resource = new \League\Fractal\Resource\Collection($activities, $transformer);
$page = $searchCriteria->getPageNumber();
$this->debugLog($user, 'Search criteria page number called', ['page' => $page]);
$histogram = array_pluck(array_get($searchResponse, 'histogram.buckets', []), 'doc_count', 'key_as_string');
$this->debugLog($user, 'Histogram generated. Response is ready.', ['histogram' => $histogram]);
return $this->response->withArray([
'pagination' => [
'total' => $searchResponse['totalHits'],
'current' => $page,
'prev' => max($page - 1, 1),
'next' => $page + 1,
],
'results' => $this->response->getManager()->createData($resource)->toArray(),
'histogram' => $histogram,
]);
}
private function debugLog(User $user, string $logMessage, ?array $context = []): void
{
// Debug for Learning People Only
if ($user->getTeamId() !== 260) {
return;
}
Log::notice(
sprintf('[activity-search-controller] %s', $logMessage),
$context
);
}
/** @throws ValidationException */
private function validateSearch(Request $request, FilterDefinitionCollection $filterSet, ?string $prefix = null): void
{
$rules = [
'exclude' => 'array',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
];
if ($prefix !== null && mb_strpos($prefix, '.') !== false) {
$rules[rtrim($prefix, '.')] = sprintf(
'required|array|max:%d',
$filterSet->count()
);
}
$validationRules = $filterSet->getValidationRules($prefix)
->merge($rules)
->all();
$request->validate($validationRules);
}
public function createActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$search = $this->updateOrCreateActivitySearch($request);
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function updateActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('update', $search);
$this->updateOrCreateActivitySearch($request, $search);
return $this->response->withOk();
}
private function storeNamedSearchFilters(
Collection $request,
Search $search,
FilterDefinitionCollection $filterSet,
?string $prefix = null,
): self {
$arrayTypeProperties = $filterSet
->getPropertyTypes([
FilterDefinitionCollection::PROPERTY_TYPE_ARRAY,
])
->all();
$supportedRequestProperties = $filterSet->getSupportedRequestProperties($prefix);
foreach ($supportedRequestProperties as $requestPropertyName) {
if (! array_has($request, $requestPropertyName)) {
continue;
}
/** @var string|string[] $propertyValue */
$propertyValue = array_get($request, $requestPropertyName);
$propertyName = $prefix === null
? $requestPropertyName
: mb_substr($requestPropertyName, mb_strlen($prefix));
$isArrayType = array_has($arrayTypeProperties, $propertyName);
if (! $isArrayType) {
/** @var string $requestPropertyValue */
$search->filters()->updateOrCreate(
[
'filter' => $propertyName,
],
[
'value' => $propertyValue,
]
);
continue;
}
/** @var string[] $requestPropertyValue */
/** @var SearchFilter[]|Collection $existingFilterValues */
$existingFilterValuesKeyed = $search->filters()
->where('filter', $propertyName)
->get()
->keyBy('id');
// Iterate over values provided as request parameters
foreach ($propertyValue as $value) {
/** @var SearchFilter|null $valueFilter */
$valueFilter = $search->filters()
->where(
[
'filter' => $propertyName,
'value' => $value,
]
)
->first();
if ($valueFilter !== null) {
// Remove filter value pair from list to be deleted
$existingFilterValuesKeyed->forget($valueFilter->id);
} else {
// Add new filter/value pair
$search->filters()->updateOrCreate([
'filter' => $propertyName,
'value' => $value,
]);
}
}
// Delete filter value pairs for this filter that no longer exist in request parameters
foreach ($existingFilterValuesKeyed as $existingFilter) {
$existingFilter->delete();
}
}
/** @var Collection<int, SearchFilter> $filtersKeyed */
$filtersKeyed = $search->filters()->get()->keyBy('filter');
// wipe removed filters from this search
foreach ($filtersKeyed as $filterName => $filter) {
if (array_has($request, $prefix . $filterName)) {
continue;
}
// Remove all filter values for this filter
$search->filters()->where('filter', $filterName)->delete();
}
return $this;
}
/**
* @throws AuthorizationException
*/
public function fetchActivitySearch(
Search $search,
Request $request,
SearchTransformer $searchTransformer,
): JsonResponse {
$this->authorize('view', $search);
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function listActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withCollection(
$user->searches()->get(),
$searchTransformer
->withConsumer($user)
);
}
/**
* Deletes a saved search
*
* @param Request $request
* @param Search $search
*
* @throws Exception
*
* @return JsonResponse
*/
public function deleteActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('delete', $search);
$ajReportCount = $this->ajReportsRepository->countActiveReportsBySavedSearch($search->getId());
if ($ajReportCount > 0) {
return $this->response->errorWrongArgs(
"This saved search is used by {$ajReportCount} active AJ report(s). "
. 'Please remove or update those reports before deleting this saved search.'
);
}
$search->filters()->delete();
$search->delete();
return $this->response->withOk();
}
public function live(Request $request, ElasticActivityRepository $repository): JsonResponse
{
$user = $this->getUserFromRequest($request);
$this->request->validate([
'sort_direction' => 'in:asc,desc',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
]);
$activities = $repository->getLiveCoachingEligibleActivities(
user: $user,
lookBackMinutes: self::LOOK_BACK,
limit: (int) $this->request->input('limit', 25),
page: (int) $this->request->input('page', 1),
sortBy: ['actual_start_time', 'scheduled_start_time'],
sortDirection: (string) $this->request->input('sort_direction', 'asc'),
);
$this->response
->getManager()
->parseIncludes(['organizer.group', 'prospect'])
->setSerializer(new JsonSerializer());
return $this->response->withCollection($activities, new ActivityTransformer());
}
/**
* @param Activity $activity
*
* @throws AuthorizationException
*
* @return mixed
*/
public function show(Activity $activity, ActivityService $activityService): JsonResponse
{
$this->authorize('show', $activity);
$user = $activity->getUser();
$team = $user->getTeam();
// Sync the opportunity with the latest data if possible.
if ($activity->opportunity_id) {
try {
$crmService = $this->providerRegistry->get($team->crm->provider);
if (! $user->isCrmRequired()) {
$crmService->setUser($team->getOwner());
} else {
$crmService->setUser($user);
}
$crmService->syncOpportunity($activity->opportunity->crm_provider_id);
} catch (Exception $exception) {
// Move on.
}
}
$activityData = $activityService->getActivityData($this->request->user(), $activity);
return response()->json($activityData);
}
public function createRecording(Activity $activity)
{
$this->authorize('record', $activity);
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Tell Twilio to start recording this activity.
if ($activity->recording_state === Activity::RECORDING_OFF) {
$job = (new StartRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withCreated();
}
return $this->response->errorGone('Activity is already recording.');
}
public function updateRecording(Request $request, Activity $activity)
{
$this->authorize('record', $activity);
$request->validate([
'preference' => 'boolean',
'state' => [
'string',
Rule::in([
Activity::RECORDING_IN_PROGRESS,
Activity::RECORDING_PAUSED,
]),
],
]);
if ($request->has('state')) {
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Toggle the recording state between paused and resumed.
if (! $activity->isRecordingState(Activity::RECORDING_OFF)) {
$job = (new ToggleRecording($activity, $request->input('state')))
->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Recording is not toggleable.');
}
if ($request->has('preference')) {
$activity->update([
'recording_preference' => $request->input('preference') ? 1 : 0,
]);
return $this->response->withOk();
}
return $this->response->errorWrongArgs('Something went wrong');
}
public function stopRecording(Activity $activity)
{
$this->authorize('stopRecord', $activity);
// Tell Twilio to stop recording this activity.
if ($activity->isRecordingState(Activity::RECORDING_IN_PROGRESS)) {
$job = (new StopRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Activity is not recording.');
}
/**
* Add activity to this user's favorites playlist
*
* @throws AuthorizationException
*/
public function favorite(Activity $activity, PlaylistActivityRepository $playlistActivityRepository): JsonResponse
{
$this->authorize('favorite', $activity);
$user = $this->getUserFromRequest($this->request);
$favorite = $activity->wasFavoritedBy($user);
$name = $activity->activity_title ?? '';
// It needs to check at least one record.
if (! $favorite) {
$favoritePlaylist = $user->favoritePlaylist();
$playlistActivity = $playlistActivityRepository->findByBaseActivityUserAndPlaylist(
$activity,
$user,
$favoritePlaylist
);
if ($playlistActivity !== null) {
$playlistActivity->update(
// Just update, don't sort.
['start_time' => 0, 'name' => mb_strimwidth($name, 0, 100)],
);
} else {
$playlistActivity = $activity->playlistActivities()->create([
'playlist_id' => $favoritePlaylist->getId(),
'user_id' => $user->getId(),
'start_time' => 0,
'name' => mb_strimwidth($name, 0, 100),
]);
// Sort it on top.
$playlistActivity->update(
[
'sort' => $playlistActivityRepository->calculateNewSortOrder(
null,
$playlistActivity,
),
],
);
}
$playlistActivityRepository->calculateNewSortOrder(null, $playlistActivity);
return new JsonResponse([], JsonResponse::HTTP_CREATED);
}
return new JsonResponse(
[
'error' => [
'code' => AbstractResponse::CODE_CONFLICT,
'http_code' => JsonResponse::HTTP_CONFLICT,
'message' => 'Resource Already Exists',
],
],
JsonResponse::HTTP_CONFLICT,
);
}
/**
* Remove activity from th...
|
PhpStorm
|
faVsco.js – ActivityController.php
|
NULL
|
76690
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
2
45
3
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Http\Controllers\API;
use Carbon\Carbon;
use ChaseConey\LaravelDatadogHelper\Datadog;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Jiminny\Component\ActivityAnalytics;
use Jiminny\Component\ActivitySearch;
use Jiminny\Component\ActivitySearch\FilterDefinitionCollection;
use Jiminny\Component\PlaybackPage\Comments\Services\ActivityCommentService;
use Jiminny\Component\Queue\Constants;
use Jiminny\Contracts\Nudge\NudgeFactoryInterface;
use Jiminny\Contracts\Playlist\PlaylistTrackFactoryInterface;
use Jiminny\Contracts\Repositories\PlaylistActivityRepository;
use Jiminny\Contracts\Services\Crm\ServiceInterface;
use Jiminny\Enums\TeamSetting;
use Jiminny\Events\Activities\AiAutomation\ActivityProspectAdded;
use Jiminny\Events\Activities\Coaching\Coached;
use Jiminny\Contracts\Services\Crm\SupportsObjectTypeParseInterface;
use Jiminny\Exceptions\LogicException;
use Jiminny\Exceptions\SocialAccountTokenInvalidException;
use Jiminny\Http\Controllers\API\BaseController as Controller;
use Jiminny\Http\Controllers\CommentContextInterface;
use Jiminny\Http\Responses\Api\AbstractResponse;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Http\Serializers\JsonSerializer;
use Jiminny\Http\Transformers\ActivityCommentTransformer;
use Jiminny\Http\Transformers\ActivityTopicTriggerTransformer;
use Jiminny\Http\Transformers\ActivityTransformer;
use Jiminny\Http\Transformers\AvailabilityNotificationTransformer;
use Jiminny\Http\Transformers\CoachingFeedbackTransformer;
use Jiminny\Http\Transformers\CoachingSectionsTransformer;
use Jiminny\Http\Transformers\SearchTransformer;
use Jiminny\Http\Transformers\StatsTransformer;
use Jiminny\Jobs\Crm\SaveActivity;
use Jiminny\Jobs\Crm\UpdateStage;
use Jiminny\Jobs\Telephony\StartRecording;
use Jiminny\Jobs\Telephony\StopRecording;
use Jiminny\Jobs\Telephony\ToggleRecording;
use Jiminny\Models\Account;
use Jiminny\Models\Activity;
use Jiminny\Models\Activity\CoachRequest;
use Jiminny\Models\Activity\Comment;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\Activity\SearchFilter;
use Jiminny\Models\Activity\Share;
use Jiminny\Models\CoachingFeedback;
use Jiminny\Models\CoachingSection;
use Jiminny\Models\CoachingSectionCriterion;
use Jiminny\Models\CoachingSectionFeedback;
use Jiminny\Models\Contact;
use Jiminny\Models\Crm\Field;
use Jiminny\Models\Crm\FieldData;
use Jiminny\Models\Crm\Layout;
use Jiminny\Models\Crm\LayoutEntity;
use Jiminny\Models\Feature\FeatureEnum;
use Jiminny\Models\LanguageDialect;
use Jiminny\Models\Lead;
use Jiminny\Models\Nudge;
use Jiminny\Models\PlaybookCategory;
use Jiminny\Models\Playlist;
use Jiminny\Models\Stage;
use Jiminny\Models\Team;
use Jiminny\Models\Track;
use Jiminny\Models\User;
use Jiminny\Repositories\AjReportsRepository;
use Jiminny\Repositories\CoachingFeedbackRepository;
use Jiminny\Repositories\ElasticActivityRepository;
use Jiminny\Repositories\TeamRepository;
use Jiminny\Rules\CrmReference;
use Jiminny\Rules\MultidimensionalArrayMaxCharRule;
use Jiminny\Services\ActivityService;
use Jiminny\Services\Crm\ProviderRegistry;
use Jiminny\Services\PlaybackService;
use Jiminny\Services\UserService;
use Jiminny\VO\Repository\OnDemandActivitySearch\Criteria;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Sentry;
use Symfony\Component\HttpFoundation;
final class ActivityController extends Controller implements CommentContextInterface
{
// Number of minutes to look back on activities. i.e. a timeout on activity duration.
private const LOOK_BACK = 180;
public function __construct(
private ProviderRegistry $providerRegistry,
private ActivityService $activityService,
Response $response,
private UserService $userService,
private ActivitySearch\Service\ActivitySearch $activitySearch,
private NudgeFactoryInterface $nudgeFactory,
private ActivityCommentService $activityCommentService,
private LoggerInterface $logger,
private readonly CoachingFeedbackRepository $coachingFeedbackRepository,
private readonly TeamRepository $teamRepository,
private readonly AjReportsRepository $ajReportsRepository,
) {
parent::__construct($response);
}
public static function getCommentImplementation(): string
{
return Comment::class;
}
public function delete()
{
$this->request->validate([
'*' => 'uuid:activities',
]);
$deletedIds = [];
foreach ($this->request->all() as $activityId) {
$activity = Activity::idOrUuId($activityId);
try {
if ($this->authorize('delete', $activity)) {
$activity->delete();
$deletedIds[] = $activityId;
\Log::info('Soft deleted activity ' . $activity->id_string . ' by user ' . $this->getUser()->id);
}
} catch (AuthorizationException $authorizationException) {
// They didn't have permission.
}
}
return $this->response->withArray($deletedIds);
}
public function update(Request $request, Activity $activity)
{
$this->authorize('updateMetadata', $activity);
$request->validate([
'title' => 'string|max:250',
'category_id' => 'uuid:playbook_categories',
'language' => [
new In(
LanguageDialect::query()
->with('language')
->cursor()
->map(static function (LanguageDialect $languageDialect): string {
return $languageDialect->getLanguageLocale();
})
->all()
),
],
]);
if ($request->has('title')) {
$activity->title = $request->input('title');
}
if ($request->has('category_id')) {
$category = PlaybookCategory::uuid($request->input('category_id'));
if ($category->playbook->team_id !== $request->user()->team_id) {
return $this->response->errorNotFound('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
if ($request->has('language')) {
if (! $activity->isInProgress()) {
return $this->response->withError(
'Activity language can only be set while the meeting is in progress.',
400
);
}
$activity->setLanguageCode($request->input('language'));
}
$activity->save();
return $this->response->withOk();
}
// XXX: This should be merged with the update method.
/**
* @param Activity $activity
*
* @throws AuthorizationException
* @throws SocialAccountTokenInvalidException
*
* @return mixed
*/
public function summarize(Activity $activity): mixed
{
$this->logger->info('[Log Activity] Summarizing activity ', [
'activityId' => $activity->getUuid(),
'payload' => $this->request->all(),
]);
$this->authorize('update', $activity);
$this->logger->info('[Log Activity] Validating summary');
// Validate the payload.
$this->validateSummary($activity);
// All objects must belong to this team.
/** @var User $user */
$user = $this->request->user();
$team = $user->getTeam();
$crmService = $this->providerRegistry->get($team->crm->provider);
try {
$crmUser = $user;
if ($user->isCrmRequired() === false) {
$crmUser = $team->owner;
}
$crmService->setUser($crmUser);
} catch (SocialAccountTokenInvalidException $accountTokenInvalidException) {
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($accountTokenInvalidException->getMessage());
}
$rawEntities = $this->request->input('entities');
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid(
$this->request->input('layout_id')
);
// Delay execution of CRM jobs to avoid locking issues.
$jobDelay = 0;
// If we have arrived from a notification, mark it as read.
$notificationId = $this->request->input('nId');
if ($notificationId) {
$notification = $user->unreadNotifications->where('id', $notificationId)->first();
if ($notification) {
$notification->markAsRead();
}
}
$title = $this->request->input('title');
$prospects = $this->request->input('prospects');
$opportunityId = $this->request->input('opportunity_id');
$stageId = $this->request->input('stage_id');
$categoryId = $this->request->input('category_id');
$summary = $this->request->input('summary');
$crmProviderId = $this->request->input('crm_id');
$isInternal = $this->request->input('is_internal') ?? false;
$lead = null;
$category = null;
$account = null;
$contact = null;
$opportunity = null;
$stage = null;
$callStage = null;
foreach ($prospects as $prospectData) {
$objectId = $prospectData['id'];
if ($objectId === null) {
continue;
}
$objectType = $prospectData['type'];
$this->logger->info('debug', ['prospect_data' => $prospectData]);
try {
if ($objectType === null) {
$this->logger->info('no object type');
if ($crmService instanceof SupportsObjectTypeParseInterface) {
$objectType = $crmService->parseObjectType($objectId);
}
}
switch ($objectType) {
case 'lead':
$this->logger->info('Processing lead');
/** @var Lead|null $lead */
$lead = $team->crm->leads()->where('crm_provider_id', $objectId)->first();
// Lead does not exist locally, import it.
if ($lead === null) {
$this->logger->info('Lead does not exist locally');
/** @var Lead $lead */
$lead = $crmService->syncLead($objectId);
}
$this->logger->info('Lead found', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
if ($stageId === null) {
$this->logger->info('Stage ID is null');
// If it was not provided, just assume it is the current stage.
$callStage = $lead->stage;
break;
}
$this->logger->info('Looking for stage');
// Determine if they have changed the stage.
/** @var Stage $stage */
$stage = $team->crm->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_LEAD)
->firstOrFail();
$this->logger->info('Stage found', ['stageId' => $stage->id, 'lead_stage' => $lead->stage_id]);
if ($lead->stage_id && $lead->stage_id !== $stage->id) {
$this->logger->info('Stage has changed');
// Storage current stage on activity.
$callStage = $lead->stage;
// The stage has changed, update in remote CRM.
dispatch(new UpdateStage($activity, $lead, $callStage, $stage));
$this->logger->info(
sprintf(
'[%s] User changing lead stage from %s to %s',
$crmService->getDisplayName(),
$callStage->getName(),
$stage->getName()
),
[
'user' => $user->getUuid(),
'lead' => $lead->getUuid(),
]
);
} else {
$this->logger->info('Stage has not changed');
// Stage remains as current.
$callStage = $stage;
}
break;
case 'account':
$this->logger->info('Processing account');
// If the object is not a lead, it should be an account.
$account = $team->crm->accounts()->where('crm_provider_id', $objectId)->first();
// Account does not exist locally, import it.
if ($account === null) {
$this->logger->info('Account does not exist locally');
$account = $crmService->syncAccount($objectId);
}
$this->logger->info('Account found', ['accountId' => $account->id]);
break;
case 'contact':
$this->logger->info('processing contact');
$contact = $team->crm->contacts()->where('crm_provider_id', $objectId)->first();
// Contact does not exist locally, import it.
if (! $contact instanceof Contact) {
$this->logger->info('contact does not exist locally');
$contact = $crmService->syncContact($objectId);
}
$this->logger->info('resolving account');
$account = $this->resolveAccount($team, $contact, $crmService, $prospects);
break;
}
// If they have specified an opportunity, retrieve this with stage.
if ($opportunityId) {
$this->logger->info('opportunity id is set');
$opportunity = $team->crm->opportunities()->where('crm_provider_id', $opportunityId)->first();
// Opportunity does not exist locally, import it.
if ($opportunity === null) {
$this->logger->info('opportunity does not exist locally');
$opportunity = $crmService->syncOpportunity($opportunityId);
}
if ($stageId === null) {
$this->logger->info('stage id is null');
// If it was not provided, just assume it is the current stage.
$callStage = $opportunity->stage ?? null;
} else {
$this->logger->info('looking for stage');
/** @var ?Stage $opportunityStage */
$opportunityStage = $team->crm
->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_OPPORTUNITY)
->first();
// There is a chance we still cannot import this opportunity.
if ($opportunityStage !== null && $opportunity !== null && $opportunity->stage_id !== $opportunityStage->id) {
$this->logger->info('opportunity stage has changed');
// Storage current stage on activity.
$callStage = $opportunity->stage;
dispatch(new UpdateStage($activity, $opportunity, $callStage, $opportunityStage));
$this->logger->info(
sprintf(
'[%s] User changing opportunity stage from %s to %s',
$crmService->getDisplayName(),
$callStage->name,
$opportunityStage->name
),
[
'userId' => $user->id_string,
'opportunityId' => $opportunity->id_string,
]
);
} else {
$this->logger->info('opportunity stage has not changed');
// Stage remains as current.
$callStage = $opportunityStage;
}
}
}
if ($crmProviderId) {
// Cast $crmProviderId to string otherwise it won't use database index for some records
$linkedActivity = Activity::where('crm_provider_id', (string) $crmProviderId)->first();
// Check if this activity has already been assigned to a different activity.
if ($linkedActivity && $linkedActivity->id !== $activity->id) {
throw new InvalidArgumentException(
'Sorry, the linked task has already been logged under a different call. '
. 'Please choose another linked task.'
);
}
}
} catch (InvalidArgumentException $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($exception->getMessage());
} catch (Exception $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorInternalError(
'Sorry, an error occurred. Please try again or reach out to support if the problem continues.'
);
}
}
if ($categoryId) {
$category = PlaybookCategory::uuid($categoryId);
if ($category->playbook->team_id !== $team->id) {
throw new InvalidArgumentException('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
$this->logger->info('Prospect data', [
'lead_id' => $lead?->getId(),
'account_id' => $account?->getId(),
'contact_id' => $contact?->getId(),
'opportunity_id' => $opportunity?->getId(),
'stage_id' => $stage?->getId(),
]);
if ($title) {
$activity->title = $title;
}
if ($summary) {
$activity->summary = $summary;
}
if ($crmProviderId) {
$activity->crm_provider_id = $crmProviderId;
}
if ($callStage) {
$this->logger->info('Setting stage id', ['stageId' => $callStage->id]);
$activity->stage_id = $callStage->id;
}
if ($lead) {
$this->logger->info('Setting lead id', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
// If we are changed from an account > lead, unset the account data.
$this->logger->info('Unsetting account id, opportunity id, contact id, value');
$activity->account_id = null;
$activity->opportunity_id = null;
$activity->contact_id = null;
$activity->value = null;
}
if ($account) {
$this->logger->info('Setting account id', ['accountId' => $account->id]);
$activity->account_id = $account->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('unsetting lead id');
$activity->lead_id = null;
// Unset the contact if switching different accounts. Will be set up below if still applicable.
if (! $team->hasFeature(FeatureEnum::LINK_ACTIVITY_TO_MULTIPLE_PROSPECTS) || empty($contact)) {
$this->logger->info('Unsetting contact id');
$activity->contact_id = null;
}
}
if ($opportunity) {
$this->logger->info('setting opportunity id', ['opportunityId' => $opportunity->id]);
$this->logger->info('unsetting lead id');
$activity->opportunity_id = $opportunity->id;
$activity->value = $opportunity->value;
// If we are changed from an lead > account, unset the lead data.
$activity->lead_id = null;
}
if ($contact) {
$this->logger->info('setting contact id', ['contactId' => $contact->id]);
$activity->contact_id = $contact->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('Unsetting lead id');
$activity->lead_id = null;
}
$activity->is_internal = $isInternal;
$activity->save();
$activity->refresh();
$this->logger->notice('Activity saved', [
'activity_id' => $activity->getId(),
'lead_id' => $activity->lead_id,
'account_id' => $activity->account_id,
'contact_id' => $activity->contact_id,
'opportunity_id' => $activity->opportunity_id,
'stage_id' => $activity->stage_id,
'crm_provider_id' => $activity->getCrmProviderId(),
]);
// Store entities as field data on the activity.
$updatedData = $this->storeEntities($crmService, $activity, $layout, $rawEntities);
if ($activity->isLoggable()) {
// Follow-up Task or Event data.
$followupData = $this->fetchFollowupEntities($crmService, $layout, $rawEntities);
$this->logger->info('CRM LOG manual log triggered', [
'activityId' => $activity->getUuid(),
'followupData' => $followupData,
'userId' => $user->getUuid(),
]);
// Store data in the CRM.
// ++add check for crm_required
$job = new SaveActivity($activity, $followupData);
if ($updatedData) {
$job->delay(Carbon::now()->addMinutes($jobDelay));
}
dispatch($job);
// Manually dispatch log for Opportunity or Prospect added
if ($activity->hasOpportunity() || $activity->hasProspect()) {
event(new ActivityProspectAdded(
activity: $activity,
eventSource: 'manually-log-crm-data'
));
}
}
return $this->response->withOk();
}
/**
* Extract any activity data to be upserted in the Lead/Opportunity/Task etc in the CRM.
*
* @param ServiceInterface $service
* @param Activity $activity
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function storeEntities(ServiceInterface $service, Activity $activity, Layout $layout, array $entities): array
{
$updatedData = [];
$existingData = $activity->data()->get();
// We need to delete any existing data to overwrite with latest values.
$activity->data()->delete();
$layoutEntities = $layout->entities()
->with('field', 'parent')
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->get();
/** @var LayoutEntity $entity */
foreach ($layoutEntities as $entity) {
// If the user has provided a value for this entity
if (array_key_exists($entity->id_string, $entities)) {
$value = $entities[$entity->id_string];
// Convert raw data into values that the CRM can consume.
if ($value) {
$value = $service->normalizeValue($entity->field->type, $value);
}
// Check the field is part of the activity-summary section.
if ($entity->parent && $entity->parent->label === 'activity-summary' && $value) {
// This is the internal database ID, not the external CRM ID.
$objectId = null;
switch ($entity->field->object_type) {
case Field::OBJECT_ACCOUNT:
$objectId = $activity->account_id;
break;
case Field::OBJECT_CONTACT:
$objectId = $activity->contact_id;
break;
case Field::OBJECT_OPPORTUNITY:
$objectId = $activity->opportunity_id;
break;
case Field::OBJECT_LEAD:
$objectId = $activity->lead_id;
break;
case Field::OBJECT_TASK:
case Field::OBJECT_EVENT:
$objectId = $activity->id;
break;
}
if ($objectId) {
/** @var FieldData $data */
$data = $activity->data()->create([
'crm_layout_entity_id' => $entity->id,
'crm_field_id' => $entity->crm_field_id,
'object_type' => $entity->field->object_type,
'object_id' => $objectId,
'value' => $value,
]);
// Never send read-only field data to the CRM.
if ($entity->read_only === false && $entity->is_visible) {
$existingValue = $existingData
->where('crm_layout_entity_id', $entity->id)
->where('crm_field_id', $entity->crm_field_id)
->where('object_type', $entity->field->object_type)
->where('object_id', $objectId)
->first();
// If the field was actually changed, we need to reflect this in the CRM too.
if ($existingValue === null || $existingValue->value !== $value) {
$updatedData[] = $data->id;
}
}
}
}
}
}
return $updatedData;
}
/**
* Extract any followup data to be dispatched in a job to create a new Task/Event in the CRM.
*
* @param ServiceInterface $crmService
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function fetchFollowupEntities(ServiceInterface $crmService, Layout $layout, array $entities): array
{
$fieldData = [];
foreach ($entities as $entityId => $value) {
// Only bother with fields that have a value.
if ($value) {
// Extract the entity from the UUID. Check the field is valid and part of the follow-up section.
$entity = $layout->entities()
->uuid($entityId, false)
->whereHas('parent', function ($query) {
$query->where('label', 'follow-up');
})
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->first();
if ($entity) {
// Convert raw data into values that the CRM can consume.
$value = $crmService->normalizeValue($entity->field->type, $value);
// Add the field and value to the payload.
$fieldData += [
$entity->field->crm_provider_id => $value,
];
}
}
}
return $fieldData;
}
/**
* @param Activity $activity
*/
private function validateSummary(Activity $activity): void
{
$team = $activity->user->team;
$crmProvider = $team->crm->provider;
$attributes = [];
$rules = [
'layout_id' => 'required|uuid:crm_layouts,crm_configuration_id,' . $team->crm_id,
'title' => 'string|max:250',
'prospects' => 'required|array',
'opportunity_id' => new CrmReference($crmProvider),
'category_id' => 'uuid:playbook_categories|required_unless:is_internal,true',
'stage_id' => 'uuid:stages,team_id,' . $team->id, // Todo: move to proper validator
'summary' => 'max:50000',
'nId' => 'exists:notifications,id',
'crm_id' => new CrmReference($crmProvider),
'entities' => 'array',
'is_internal' => 'boolean',
];
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid($this->request->input('layout_id'));
// Only validate fields, not headers etc. If not loggable, we don't care about follow-up section.
$entities = $layout->entities()
->where('read_only', 0)
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->whereHas('parent', function ($query) use ($activity) {
if ($activity->isLoggable() === false) {
$query->where('label', '<>', 'follow-up');
}
});
$isInternal = $this->request->input('is_internal', false);
foreach ($entities->get() as $entity) {
$rules += $this->buildFieldValidator($entity, $isInternal);
$attributes += $this->buildFieldMessage($entity);
}
$this->request->validate($rules, [], $attributes);
}
private function buildFieldValidator(LayoutEntity $entity, bool $isInternal): array
{
return [
'entities.' . $entity->id_string => $entity->getValidator($isInternal),
];
}
/**
* @param LayoutEntity $entity
*
* @return array
*/
private function buildFieldMessage(LayoutEntity $entity): array
{
$label = $entity->label;
if ($label === null) {
$label = $entity->field->label;
}
return [
'entities.' . $entity->id_string => $label,
];
}
public function search(Request $request, ElasticActivityRepository $repository): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->debugLog(
$user,
'User extracted from request',
['user' => $user->getId(), 'tz' => $user->getTimezone()]
);
$searchCriteria = Criteria::createFromRequest($request->all(), $user->getTimezone());
$this->debugLog(
$user,
'ActivitySearch criteria built',
['searchCriteria' => $searchCriteria]
);
$filterSet = $this->activitySearch->getHomepageFilterSet($searchCriteria, $user);
$this->debugLog($user, 'FilterSet built', ['filterSet' => $filterSet]);
$this->validateSearch($request, $filterSet);
$this->debugLog($user, 'Request validated');
$searchResponse = $repository->onDemandSearch($user, $searchCriteria, $filterSet);
/** @var Collection<Activity> $activities */
$activities = $searchResponse['results'];
$this->debugLog($user, 'Activities ES response extracted');
$hideInternalMeetingsSetting = $this->teamRepository->getTeamSettingByTeamId(
$user->getTeamId(),
TeamSetting::HIDE_INTERNAL_SCHEDULED_MEETINGS->name(),
);
if ($hideInternalMeetingsSetting?->getValue() === '1') {
$activities = $activities->filter(function (Activity $activity) {
if ($activity->is_internal && empty($activity->actual_start_time)) {
return false;
}
return true;
});
}
$this->debugLog($user, 'Internal meetings (?!) filtered');
$this->response->getManager()
->parseIncludes([
'category',
'organizer.group',
'prospect',
'stage',
'opportunity',
'stats',
'scorecards',
'masterTrack',
'activeParticipants',
'notification',
])
->setSerializer(new JsonSerializer());
$transformerExcludes = $this->request->input('exclude');
if ($transformerExcludes) {
$this->response->getManager()->parseExcludes($transformerExcludes);
}
$this->debugLog($user, 'Response Manager (?!) applied');
$transformer = new ActivityTransformer();
$transformer->setConsumer($user);
$this->debugLog($user, 'Activity Transformer added');
$resource = new \League\Fractal\Resource\Collection($activities, $transformer);
$page = $searchCriteria->getPageNumber();
$this->debugLog($user, 'Search criteria page number called', ['page' => $page]);
$histogram = array_pluck(array_get($searchResponse, 'histogram.buckets', []), 'doc_count', 'key_as_string');
$this->debugLog($user, 'Histogram generated. Response is ready.', ['histogram' => $histogram]);
return $this->response->withArray([
'pagination' => [
'total' => $searchResponse['totalHits'],
'current' => $page,
'prev' => max($page - 1, 1),
'next' => $page + 1,
],
'results' => $this->response->getManager()->createData($resource)->toArray(),
'histogram' => $histogram,
]);
}
private function debugLog(User $user, string $logMessage, ?array $context = []): void
{
// Debug for Learning People Only
if ($user->getTeamId() !== 260) {
return;
}
Log::notice(
sprintf('[activity-search-controller] %s', $logMessage),
$context
);
}
/** @throws ValidationException */
private function validateSearch(Request $request, FilterDefinitionCollection $filterSet, ?string $prefix = null): void
{
$rules = [
'exclude' => 'array',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
];
if ($prefix !== null && mb_strpos($prefix, '.') !== false) {
$rules[rtrim($prefix, '.')] = sprintf(
'required|array|max:%d',
$filterSet->count()
);
}
$validationRules = $filterSet->getValidationRules($prefix)
->merge($rules)
->all();
$request->validate($validationRules);
}
public function createActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$search = $this->updateOrCreateActivitySearch($request);
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function updateActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('update', $search);
$this->updateOrCreateActivitySearch($request, $search);
return $this->response->withOk();
}
private function storeNamedSearchFilters(
Collection $request,
Search $search,
FilterDefinitionCollection $filterSet,
?string $prefix = null,
): self {
$arrayTypeProperties = $filterSet
->getPropertyTypes([
FilterDefinitionCollection::PROPERTY_TYPE_ARRAY,
])
->all();
$supportedRequestProperties = $filterSet->getSupportedRequestProperties($prefix);
foreach ($supportedRequestProperties as $requestPropertyName) {
if (! array_has($request, $requestPropertyName)) {
continue;
}
/** @var string|string[] $propertyValue */
$propertyValue = array_get($request, $requestPropertyName);
$propertyName = $prefix === null
? $requestPropertyName
: mb_substr($requestPropertyName, mb_strlen($prefix));
$isArrayType = array_has($arrayTypeProperties, $propertyName);
if (! $isArrayType) {
/** @var string $requestPropertyValue */
$search->filters()->updateOrCreate(
[
'filter' => $propertyName,
],
[
'value' => $propertyValue,
]
);
continue;
}
/** @var string[] $requestPropertyValue */
/** @var SearchFilter[]|Collection $existingFilterValues */
$existingFilterValuesKeyed = $search->filters()
->where('filter', $propertyName)
->get()
->keyBy('id');
// Iterate over values provided as request parameters
foreach ($propertyValue as $value) {
/** @var SearchFilter|null $valueFilter */
$valueFilter = $search->filters()
->where(
[
'filter' => $propertyName,
'value' => $value,
]
)
->first();
if ($valueFilter !== null) {
// Remove filter value pair from list to be deleted
$existingFilterValuesKeyed->forget($valueFilter->id);
} else {
// Add new filter/value pair
$search->filters()->updateOrCreate([
'filter' => $propertyName,
'value' => $value,
]);
}
}
// Delete filter value pairs for this filter that no longer exist in request parameters
foreach ($existingFilterValuesKeyed as $existingFilter) {
$existingFilter->delete();
}
}
/** @var Collection<int, SearchFilter> $filtersKeyed */
$filtersKeyed = $search->filters()->get()->keyBy('filter');
// wipe removed filters from this search
foreach ($filtersKeyed as $filterName => $filter) {
if (array_has($request, $prefix . $filterName)) {
continue;
}
// Remove all filter values for this filter
$search->filters()->where('filter', $filterName)->delete();
}
return $this;
}
/**
* @throws AuthorizationException
*/
public function fetchActivitySearch(
Search $search,
Request $request,
SearchTransformer $searchTransformer,
): JsonResponse {
$this->authorize('view', $search);
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function listActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withCollection(
$user->searches()->get(),
$searchTransformer
->withConsumer($user)
);
}
/**
* Deletes a saved search
*
* @param Request $request
* @param Search $search
*
* @throws Exception
*
* @return JsonResponse
*/
public function deleteActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('delete', $search);
$ajReportCount = $this->ajReportsRepository->countActiveReportsBySavedSearch($search->getId());
if ($ajReportCount > 0) {
return $this->response->errorWrongArgs(
"This saved search is used by {$ajReportCount} active AJ report(s). "
. 'Please remove or update those reports before deleting this saved search.'
);
}
$search->filters()->delete();
$search->delete();
return $this->response->withOk();
}
public function live(Request $request, ElasticActivityRepository $repository): JsonResponse
{
$user = $this->getUserFromRequest($request);
$this->request->validate([
'sort_direction' => 'in:asc,desc',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
]);
$activities = $repository->getLiveCoachingEligibleActivities(
user: $user,
lookBackMinutes: self::LOOK_BACK,
limit: (int) $this->request->input('limit', 25),
page: (int) $this->request->input('page', 1),
sortBy: ['actual_start_time', 'scheduled_start_time'],
sortDirection: (string) $this->request->input('sort_direction', 'asc'),
);
$this->response
->getManager()
->parseIncludes(['organizer.group', 'prospect'])
->setSerializer(new JsonSerializer());
return $this->response->withCollection($activities, new ActivityTransformer());
}
/**
* @param Activity $activity
*
* @throws AuthorizationException
*
* @return mixed
*/
public function show(Activity $activity, ActivityService $activityService): JsonResponse
{
$this->authorize('show', $activity);
$user = $activity->getUser();
$team = $user->getTeam();
// Sync the opportunity with the latest data if possible.
if ($activity->opportunity_id) {
try {
$crmService = $this->providerRegistry->get($team->crm->provider);
if (! $user->isCrmRequired()) {
$crmService->setUser($team->getOwner());
} else {
$crmService->setUser($user);
}
$crmService->syncOpportunity($activity->opportunity->crm_provider_id);
} catch (Exception $exception) {
// Move on.
}
}
$activityData = $activityService->getActivityData($this->request->user(), $activity);
return response()->json($activityData);
}
public function createRecording(Activity $activity)
{
$this->authorize('record', $activity);
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Tell Twilio to start recording this activity.
if ($activity->recording_state === Activity::RECORDING_OFF) {
$job = (new StartRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withCreated();
}
return $this->response->errorGone('Activity is already recording.');
}
public function updateRecording(Request $request, Activity $activity)
{
$this->authorize('record', $activity);
$request->validate([
'preference' => 'boolean',
'state' => [
'string',
Rule::in([
Activity::RECORDING_IN_PROGRESS,
Activity::RECORDING_PAUSED,
]),
],
]);
if ($request->has('state')) {
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Toggle the recording state between paused and resumed.
if (! $activity->isRecordingState(Activity::RECORDING_OFF)) {
$job = (new ToggleRecording($activity, $request->input('state')))
->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Recording is not toggleable.');
}
if ($request->has('preference')) {
$activity->update([
'recording_preference' => $request->input('preference') ? 1 : 0,
]);
return $this->response->withOk();
}
return $this->response->errorWrongArgs('Something went wrong');
}
public function stopRecording(Activity $activity)
{
$this->authorize('stopRecord', $activity);
// Tell Twilio to stop recording this activity.
if ($activity->isRecordingState(Activity::RECORDING_IN_PROGRESS)) {
$job = (new StopRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Activity is not recording.');
}
/**
* Add activity to this user's favorites playlist
*
* @throws AuthorizationException
*/
public function favorite(Activity $activity, PlaylistActivityRepository $playlistActivityRepository): JsonResponse
{
$this->authorize('favorite', $activity);
$user = $this->getUserFromRequest($this->request);
$favorite = $activity->wasFavoritedBy($user);
$name = $activity->activity_title ?? '';
// It needs to check at least one record.
if (! $favorite) {
$favoritePlaylist = $user->favoritePlaylist();
$playlistActivity = $playlistActivityRepository->findByBaseActivityUserAndPlaylist(
$activity,
$user,
$favoritePlaylist
);
if ($playlistActivity !== null) {
$playlistActivity->update(
// Just update, don't sort.
['start_time' => 0, 'name' => mb_strimwidth($name, 0, 100)],
);
} else {
$playlistActivity = $activity->playlistActivities()->create([
'playlist_id' => $favoritePlaylist->getId(),
'user_id' => $user->getId(),
'start_time' => 0,
'name' => mb_strimwidth($name, 0, 100),
]);
// Sort it on top.
$playlistActivity->update(
[
'sort' => $playlistActivityRepository->calculateNewSortOrder(
null,
$playlistActivity,
),
],
);
}
$playlistActivityRepository->calculateNewSortOrder(null, $playlistActivity);
return new JsonResponse([], JsonResponse::HTTP_CREATED);
}
return new JsonResponse(
[
'error' => [
'code' => AbstractResponse::CODE_CONFLICT,
'http_code' => JsonResponse::HTTP_CONFLICT,
'message' => 'Resource Already Exists',
],
],
JsonResponse::HTTP_CONFLICT,
);
}
/**
* Remove activity from th...
|
PhpStorm
|
faVsco.js – ActivityController.php
|
NULL
|
76691
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
2
45
3
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Http\Controllers\API;
use Carbon\Carbon;
use ChaseConey\LaravelDatadogHelper\Datadog;
use Exception;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\In;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Jiminny\Component\ActivityAnalytics;
use Jiminny\Component\ActivitySearch;
use Jiminny\Component\ActivitySearch\FilterDefinitionCollection;
use Jiminny\Component\PlaybackPage\Comments\Services\ActivityCommentService;
use Jiminny\Component\Queue\Constants;
use Jiminny\Contracts\Nudge\NudgeFactoryInterface;
use Jiminny\Contracts\Playlist\PlaylistTrackFactoryInterface;
use Jiminny\Contracts\Repositories\PlaylistActivityRepository;
use Jiminny\Contracts\Services\Crm\ServiceInterface;
use Jiminny\Enums\TeamSetting;
use Jiminny\Events\Activities\AiAutomation\ActivityProspectAdded;
use Jiminny\Events\Activities\Coaching\Coached;
use Jiminny\Contracts\Services\Crm\SupportsObjectTypeParseInterface;
use Jiminny\Exceptions\LogicException;
use Jiminny\Exceptions\SocialAccountTokenInvalidException;
use Jiminny\Http\Controllers\API\BaseController as Controller;
use Jiminny\Http\Controllers\CommentContextInterface;
use Jiminny\Http\Responses\Api\AbstractResponse;
use Jiminny\Http\Responses\Api\Response;
use Jiminny\Http\Serializers\JsonSerializer;
use Jiminny\Http\Transformers\ActivityCommentTransformer;
use Jiminny\Http\Transformers\ActivityTopicTriggerTransformer;
use Jiminny\Http\Transformers\ActivityTransformer;
use Jiminny\Http\Transformers\AvailabilityNotificationTransformer;
use Jiminny\Http\Transformers\CoachingFeedbackTransformer;
use Jiminny\Http\Transformers\CoachingSectionsTransformer;
use Jiminny\Http\Transformers\SearchTransformer;
use Jiminny\Http\Transformers\StatsTransformer;
use Jiminny\Jobs\Crm\SaveActivity;
use Jiminny\Jobs\Crm\UpdateStage;
use Jiminny\Jobs\Telephony\StartRecording;
use Jiminny\Jobs\Telephony\StopRecording;
use Jiminny\Jobs\Telephony\ToggleRecording;
use Jiminny\Models\Account;
use Jiminny\Models\Activity;
use Jiminny\Models\Activity\CoachRequest;
use Jiminny\Models\Activity\Comment;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\Activity\SearchFilter;
use Jiminny\Models\Activity\Share;
use Jiminny\Models\CoachingFeedback;
use Jiminny\Models\CoachingSection;
use Jiminny\Models\CoachingSectionCriterion;
use Jiminny\Models\CoachingSectionFeedback;
use Jiminny\Models\Contact;
use Jiminny\Models\Crm\Field;
use Jiminny\Models\Crm\FieldData;
use Jiminny\Models\Crm\Layout;
use Jiminny\Models\Crm\LayoutEntity;
use Jiminny\Models\Feature\FeatureEnum;
use Jiminny\Models\LanguageDialect;
use Jiminny\Models\Lead;
use Jiminny\Models\Nudge;
use Jiminny\Models\PlaybookCategory;
use Jiminny\Models\Playlist;
use Jiminny\Models\Stage;
use Jiminny\Models\Team;
use Jiminny\Models\Track;
use Jiminny\Models\User;
use Jiminny\Repositories\AjReportsRepository;
use Jiminny\Repositories\CoachingFeedbackRepository;
use Jiminny\Repositories\ElasticActivityRepository;
use Jiminny\Repositories\TeamRepository;
use Jiminny\Rules\CrmReference;
use Jiminny\Rules\MultidimensionalArrayMaxCharRule;
use Jiminny\Services\ActivityService;
use Jiminny\Services\Crm\ProviderRegistry;
use Jiminny\Services\PlaybackService;
use Jiminny\Services\UserService;
use Jiminny\VO\Repository\OnDemandActivitySearch\Criteria;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\Uuid;
use Sentry;
use Symfony\Component\HttpFoundation;
final class ActivityController extends Controller implements CommentContextInterface
{
// Number of minutes to look back on activities. i.e. a timeout on activity duration.
private const LOOK_BACK = 180;
public function __construct(
private ProviderRegistry $providerRegistry,
private ActivityService $activityService,
Response $response,
private UserService $userService,
private ActivitySearch\Service\ActivitySearch $activitySearch,
private NudgeFactoryInterface $nudgeFactory,
private ActivityCommentService $activityCommentService,
private LoggerInterface $logger,
private readonly CoachingFeedbackRepository $coachingFeedbackRepository,
private readonly TeamRepository $teamRepository,
private readonly AjReportsRepository $ajReportsRepository,
) {
parent::__construct($response);
}
public static function getCommentImplementation(): string
{
return Comment::class;
}
public function delete()
{
$this->request->validate([
'*' => 'uuid:activities',
]);
$deletedIds = [];
foreach ($this->request->all() as $activityId) {
$activity = Activity::idOrUuId($activityId);
try {
if ($this->authorize('delete', $activity)) {
$activity->delete();
$deletedIds[] = $activityId;
\Log::info('Soft deleted activity ' . $activity->id_string . ' by user ' . $this->getUser()->id);
}
} catch (AuthorizationException $authorizationException) {
// They didn't have permission.
}
}
return $this->response->withArray($deletedIds);
}
public function update(Request $request, Activity $activity)
{
$this->authorize('updateMetadata', $activity);
$request->validate([
'title' => 'string|max:250',
'category_id' => 'uuid:playbook_categories',
'language' => [
new In(
LanguageDialect::query()
->with('language')
->cursor()
->map(static function (LanguageDialect $languageDialect): string {
return $languageDialect->getLanguageLocale();
})
->all()
),
],
]);
if ($request->has('title')) {
$activity->title = $request->input('title');
}
if ($request->has('category_id')) {
$category = PlaybookCategory::uuid($request->input('category_id'));
if ($category->playbook->team_id !== $request->user()->team_id) {
return $this->response->errorNotFound('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
if ($request->has('language')) {
if (! $activity->isInProgress()) {
return $this->response->withError(
'Activity language can only be set while the meeting is in progress.',
400
);
}
$activity->setLanguageCode($request->input('language'));
}
$activity->save();
return $this->response->withOk();
}
// XXX: This should be merged with the update method.
/**
* @param Activity $activity
*
* @throws AuthorizationException
* @throws SocialAccountTokenInvalidException
*
* @return mixed
*/
public function summarize(Activity $activity): mixed
{
$this->logger->info('[Log Activity] Summarizing activity ', [
'activityId' => $activity->getUuid(),
'payload' => $this->request->all(),
]);
$this->authorize('update', $activity);
$this->logger->info('[Log Activity] Validating summary');
// Validate the payload.
$this->validateSummary($activity);
// All objects must belong to this team.
/** @var User $user */
$user = $this->request->user();
$team = $user->getTeam();
$crmService = $this->providerRegistry->get($team->crm->provider);
try {
$crmUser = $user;
if ($user->isCrmRequired() === false) {
$crmUser = $team->owner;
}
$crmService->setUser($crmUser);
} catch (SocialAccountTokenInvalidException $accountTokenInvalidException) {
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($accountTokenInvalidException->getMessage());
}
$rawEntities = $this->request->input('entities');
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid(
$this->request->input('layout_id')
);
// Delay execution of CRM jobs to avoid locking issues.
$jobDelay = 0;
// If we have arrived from a notification, mark it as read.
$notificationId = $this->request->input('nId');
if ($notificationId) {
$notification = $user->unreadNotifications->where('id', $notificationId)->first();
if ($notification) {
$notification->markAsRead();
}
}
$title = $this->request->input('title');
$prospects = $this->request->input('prospects');
$opportunityId = $this->request->input('opportunity_id');
$stageId = $this->request->input('stage_id');
$categoryId = $this->request->input('category_id');
$summary = $this->request->input('summary');
$crmProviderId = $this->request->input('crm_id');
$isInternal = $this->request->input('is_internal') ?? false;
$lead = null;
$category = null;
$account = null;
$contact = null;
$opportunity = null;
$stage = null;
$callStage = null;
foreach ($prospects as $prospectData) {
$objectId = $prospectData['id'];
if ($objectId === null) {
continue;
}
$objectType = $prospectData['type'];
$this->logger->info('debug', ['prospect_data' => $prospectData]);
try {
if ($objectType === null) {
$this->logger->info('no object type');
if ($crmService instanceof SupportsObjectTypeParseInterface) {
$objectType = $crmService->parseObjectType($objectId);
}
}
switch ($objectType) {
case 'lead':
$this->logger->info('Processing lead');
/** @var Lead|null $lead */
$lead = $team->crm->leads()->where('crm_provider_id', $objectId)->first();
// Lead does not exist locally, import it.
if ($lead === null) {
$this->logger->info('Lead does not exist locally');
/** @var Lead $lead */
$lead = $crmService->syncLead($objectId);
}
$this->logger->info('Lead found', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
if ($stageId === null) {
$this->logger->info('Stage ID is null');
// If it was not provided, just assume it is the current stage.
$callStage = $lead->stage;
break;
}
$this->logger->info('Looking for stage');
// Determine if they have changed the stage.
/** @var Stage $stage */
$stage = $team->crm->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_LEAD)
->firstOrFail();
$this->logger->info('Stage found', ['stageId' => $stage->id, 'lead_stage' => $lead->stage_id]);
if ($lead->stage_id && $lead->stage_id !== $stage->id) {
$this->logger->info('Stage has changed');
// Storage current stage on activity.
$callStage = $lead->stage;
// The stage has changed, update in remote CRM.
dispatch(new UpdateStage($activity, $lead, $callStage, $stage));
$this->logger->info(
sprintf(
'[%s] User changing lead stage from %s to %s',
$crmService->getDisplayName(),
$callStage->getName(),
$stage->getName()
),
[
'user' => $user->getUuid(),
'lead' => $lead->getUuid(),
]
);
} else {
$this->logger->info('Stage has not changed');
// Stage remains as current.
$callStage = $stage;
}
break;
case 'account':
$this->logger->info('Processing account');
// If the object is not a lead, it should be an account.
$account = $team->crm->accounts()->where('crm_provider_id', $objectId)->first();
// Account does not exist locally, import it.
if ($account === null) {
$this->logger->info('Account does not exist locally');
$account = $crmService->syncAccount($objectId);
}
$this->logger->info('Account found', ['accountId' => $account->id]);
break;
case 'contact':
$this->logger->info('processing contact');
$contact = $team->crm->contacts()->where('crm_provider_id', $objectId)->first();
// Contact does not exist locally, import it.
if (! $contact instanceof Contact) {
$this->logger->info('contact does not exist locally');
$contact = $crmService->syncContact($objectId);
}
$this->logger->info('resolving account');
$account = $this->resolveAccount($team, $contact, $crmService, $prospects);
break;
}
// If they have specified an opportunity, retrieve this with stage.
if ($opportunityId) {
$this->logger->info('opportunity id is set');
$opportunity = $team->crm->opportunities()->where('crm_provider_id', $opportunityId)->first();
// Opportunity does not exist locally, import it.
if ($opportunity === null) {
$this->logger->info('opportunity does not exist locally');
$opportunity = $crmService->syncOpportunity($opportunityId);
}
if ($stageId === null) {
$this->logger->info('stage id is null');
// If it was not provided, just assume it is the current stage.
$callStage = $opportunity->stage ?? null;
} else {
$this->logger->info('looking for stage');
/** @var ?Stage $opportunityStage */
$opportunityStage = $team->crm
->stages()
->uuid($stageId, false)
->where('type', Stage::TYPE_OPPORTUNITY)
->first();
// There is a chance we still cannot import this opportunity.
if ($opportunityStage !== null && $opportunity !== null && $opportunity->stage_id !== $opportunityStage->id) {
$this->logger->info('opportunity stage has changed');
// Storage current stage on activity.
$callStage = $opportunity->stage;
dispatch(new UpdateStage($activity, $opportunity, $callStage, $opportunityStage));
$this->logger->info(
sprintf(
'[%s] User changing opportunity stage from %s to %s',
$crmService->getDisplayName(),
$callStage->name,
$opportunityStage->name
),
[
'userId' => $user->id_string,
'opportunityId' => $opportunity->id_string,
]
);
} else {
$this->logger->info('opportunity stage has not changed');
// Stage remains as current.
$callStage = $opportunityStage;
}
}
}
if ($crmProviderId) {
// Cast $crmProviderId to string otherwise it won't use database index for some records
$linkedActivity = Activity::where('crm_provider_id', (string) $crmProviderId)->first();
// Check if this activity has already been assigned to a different activity.
if ($linkedActivity && $linkedActivity->id !== $activity->id) {
throw new InvalidArgumentException(
'Sorry, the linked task has already been logged under a different call. '
. 'Please choose another linked task.'
);
}
}
} catch (InvalidArgumentException $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorWrongArgs($exception->getMessage());
} catch (Exception $exception) {
$this->logger->error('Failed to process prospect', [
'prospect_data' => $prospectData,
'reason' => $exception->getMessage(),
]);
// Return a JSON response with the response array and status code.
return $this->response->errorInternalError(
'Sorry, an error occurred. Please try again or reach out to support if the problem continues.'
);
}
}
if ($categoryId) {
$category = PlaybookCategory::uuid($categoryId);
if ($category->playbook->team_id !== $team->id) {
throw new InvalidArgumentException('Sorry, this category does not belong to your playbook.');
}
$activity->playbook_category_id = $category->id;
}
$this->logger->info('Prospect data', [
'lead_id' => $lead?->getId(),
'account_id' => $account?->getId(),
'contact_id' => $contact?->getId(),
'opportunity_id' => $opportunity?->getId(),
'stage_id' => $stage?->getId(),
]);
if ($title) {
$activity->title = $title;
}
if ($summary) {
$activity->summary = $summary;
}
if ($crmProviderId) {
$activity->crm_provider_id = $crmProviderId;
}
if ($callStage) {
$this->logger->info('Setting stage id', ['stageId' => $callStage->id]);
$activity->stage_id = $callStage->id;
}
if ($lead) {
$this->logger->info('Setting lead id', ['leadId' => $lead->id]);
$activity->lead_id = $lead->id;
// If we are changed from an account > lead, unset the account data.
$this->logger->info('Unsetting account id, opportunity id, contact id, value');
$activity->account_id = null;
$activity->opportunity_id = null;
$activity->contact_id = null;
$activity->value = null;
}
if ($account) {
$this->logger->info('Setting account id', ['accountId' => $account->id]);
$activity->account_id = $account->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('unsetting lead id');
$activity->lead_id = null;
// Unset the contact if switching different accounts. Will be set up below if still applicable.
if (! $team->hasFeature(FeatureEnum::LINK_ACTIVITY_TO_MULTIPLE_PROSPECTS) || empty($contact)) {
$this->logger->info('Unsetting contact id');
$activity->contact_id = null;
}
}
if ($opportunity) {
$this->logger->info('setting opportunity id', ['opportunityId' => $opportunity->id]);
$this->logger->info('unsetting lead id');
$activity->opportunity_id = $opportunity->id;
$activity->value = $opportunity->value;
// If we are changed from an lead > account, unset the lead data.
$activity->lead_id = null;
}
if ($contact) {
$this->logger->info('setting contact id', ['contactId' => $contact->id]);
$activity->contact_id = $contact->id;
// If we are changed from an lead > account, unset the lead data.
$this->logger->info('Unsetting lead id');
$activity->lead_id = null;
}
$activity->is_internal = $isInternal;
$activity->save();
$activity->refresh();
$this->logger->notice('Activity saved', [
'activity_id' => $activity->getId(),
'lead_id' => $activity->lead_id,
'account_id' => $activity->account_id,
'contact_id' => $activity->contact_id,
'opportunity_id' => $activity->opportunity_id,
'stage_id' => $activity->stage_id,
'crm_provider_id' => $activity->getCrmProviderId(),
]);
// Store entities as field data on the activity.
$updatedData = $this->storeEntities($crmService, $activity, $layout, $rawEntities);
if ($activity->isLoggable()) {
// Follow-up Task or Event data.
$followupData = $this->fetchFollowupEntities($crmService, $layout, $rawEntities);
$this->logger->info('CRM LOG manual log triggered', [
'activityId' => $activity->getUuid(),
'followupData' => $followupData,
'userId' => $user->getUuid(),
]);
// Store data in the CRM.
// ++add check for crm_required
$job = new SaveActivity($activity, $followupData);
if ($updatedData) {
$job->delay(Carbon::now()->addMinutes($jobDelay));
}
dispatch($job);
// Manually dispatch log for Opportunity or Prospect added
if ($activity->hasOpportunity() || $activity->hasProspect()) {
event(new ActivityProspectAdded(
activity: $activity,
eventSource: 'manually-log-crm-data'
));
}
}
return $this->response->withOk();
}
/**
* Extract any activity data to be upserted in the Lead/Opportunity/Task etc in the CRM.
*
* @param ServiceInterface $service
* @param Activity $activity
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function storeEntities(ServiceInterface $service, Activity $activity, Layout $layout, array $entities): array
{
$updatedData = [];
$existingData = $activity->data()->get();
// We need to delete any existing data to overwrite with latest values.
$activity->data()->delete();
$layoutEntities = $layout->entities()
->with('field', 'parent')
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->get();
/** @var LayoutEntity $entity */
foreach ($layoutEntities as $entity) {
// If the user has provided a value for this entity
if (array_key_exists($entity->id_string, $entities)) {
$value = $entities[$entity->id_string];
// Convert raw data into values that the CRM can consume.
if ($value) {
$value = $service->normalizeValue($entity->field->type, $value);
}
// Check the field is part of the activity-summary section.
if ($entity->parent && $entity->parent->label === 'activity-summary' && $value) {
// This is the internal database ID, not the external CRM ID.
$objectId = null;
switch ($entity->field->object_type) {
case Field::OBJECT_ACCOUNT:
$objectId = $activity->account_id;
break;
case Field::OBJECT_CONTACT:
$objectId = $activity->contact_id;
break;
case Field::OBJECT_OPPORTUNITY:
$objectId = $activity->opportunity_id;
break;
case Field::OBJECT_LEAD:
$objectId = $activity->lead_id;
break;
case Field::OBJECT_TASK:
case Field::OBJECT_EVENT:
$objectId = $activity->id;
break;
}
if ($objectId) {
/** @var FieldData $data */
$data = $activity->data()->create([
'crm_layout_entity_id' => $entity->id,
'crm_field_id' => $entity->crm_field_id,
'object_type' => $entity->field->object_type,
'object_id' => $objectId,
'value' => $value,
]);
// Never send read-only field data to the CRM.
if ($entity->read_only === false && $entity->is_visible) {
$existingValue = $existingData
->where('crm_layout_entity_id', $entity->id)
->where('crm_field_id', $entity->crm_field_id)
->where('object_type', $entity->field->object_type)
->where('object_id', $objectId)
->first();
// If the field was actually changed, we need to reflect this in the CRM too.
if ($existingValue === null || $existingValue->value !== $value) {
$updatedData[] = $data->id;
}
}
}
}
}
}
return $updatedData;
}
/**
* Extract any followup data to be dispatched in a job to create a new Task/Event in the CRM.
*
* @param ServiceInterface $crmService
* @param Layout $layout
* @param array $entities The raw entity data from user
*
* @return array
*/
private function fetchFollowupEntities(ServiceInterface $crmService, Layout $layout, array $entities): array
{
$fieldData = [];
foreach ($entities as $entityId => $value) {
// Only bother with fields that have a value.
if ($value) {
// Extract the entity from the UUID. Check the field is valid and part of the follow-up section.
$entity = $layout->entities()
->uuid($entityId, false)
->whereHas('parent', function ($query) {
$query->where('label', 'follow-up');
})
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->first();
if ($entity) {
// Convert raw data into values that the CRM can consume.
$value = $crmService->normalizeValue($entity->field->type, $value);
// Add the field and value to the payload.
$fieldData += [
$entity->field->crm_provider_id => $value,
];
}
}
}
return $fieldData;
}
/**
* @param Activity $activity
*/
private function validateSummary(Activity $activity): void
{
$team = $activity->user->team;
$crmProvider = $team->crm->provider;
$attributes = [];
$rules = [
'layout_id' => 'required|uuid:crm_layouts,crm_configuration_id,' . $team->crm_id,
'title' => 'string|max:250',
'prospects' => 'required|array',
'opportunity_id' => new CrmReference($crmProvider),
'category_id' => 'uuid:playbook_categories|required_unless:is_internal,true',
'stage_id' => 'uuid:stages,team_id,' . $team->id, // Todo: move to proper validator
'summary' => 'max:50000',
'nId' => 'exists:notifications,id',
'crm_id' => new CrmReference($crmProvider),
'entities' => 'array',
'is_internal' => 'boolean',
];
/** @var Layout $layout */
$layout = $team->crm->layouts()->uuid($this->request->input('layout_id'));
// Only validate fields, not headers etc. If not loggable, we don't care about follow-up section.
$entities = $layout->entities()
->where('read_only', 0)
->whereHas('field', function ($query) {
$query->where('is_selectable', 1);
})
->whereHas('parent', function ($query) use ($activity) {
if ($activity->isLoggable() === false) {
$query->where('label', '<>', 'follow-up');
}
});
$isInternal = $this->request->input('is_internal', false);
foreach ($entities->get() as $entity) {
$rules += $this->buildFieldValidator($entity, $isInternal);
$attributes += $this->buildFieldMessage($entity);
}
$this->request->validate($rules, [], $attributes);
}
private function buildFieldValidator(LayoutEntity $entity, bool $isInternal): array
{
return [
'entities.' . $entity->id_string => $entity->getValidator($isInternal),
];
}
/**
* @param LayoutEntity $entity
*
* @return array
*/
private function buildFieldMessage(LayoutEntity $entity): array
{
$label = $entity->label;
if ($label === null) {
$label = $entity->field->label;
}
return [
'entities.' . $entity->id_string => $label,
];
}
public function search(Request $request, ElasticActivityRepository $repository): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->debugLog(
$user,
'User extracted from request',
['user' => $user->getId(), 'tz' => $user->getTimezone()]
);
$searchCriteria = Criteria::createFromRequest($request->all(), $user->getTimezone());
$this->debugLog(
$user,
'ActivitySearch criteria built',
['searchCriteria' => $searchCriteria]
);
$filterSet = $this->activitySearch->getHomepageFilterSet($searchCriteria, $user);
$this->debugLog($user, 'FilterSet built', ['filterSet' => $filterSet]);
$this->validateSearch($request, $filterSet);
$this->debugLog($user, 'Request validated');
$searchResponse = $repository->onDemandSearch($user, $searchCriteria, $filterSet);
/** @var Collection<Activity> $activities */
$activities = $searchResponse['results'];
$this->debugLog($user, 'Activities ES response extracted');
$hideInternalMeetingsSetting = $this->teamRepository->getTeamSettingByTeamId(
$user->getTeamId(),
TeamSetting::HIDE_INTERNAL_SCHEDULED_MEETINGS->name(),
);
if ($hideInternalMeetingsSetting?->getValue() === '1') {
$activities = $activities->filter(function (Activity $activity) {
if ($activity->is_internal && empty($activity->actual_start_time)) {
return false;
}
return true;
});
}
$this->debugLog($user, 'Internal meetings (?!) filtered');
$this->response->getManager()
->parseIncludes([
'category',
'organizer.group',
'prospect',
'stage',
'opportunity',
'stats',
'scorecards',
'masterTrack',
'activeParticipants',
'notification',
])
->setSerializer(new JsonSerializer());
$transformerExcludes = $this->request->input('exclude');
if ($transformerExcludes) {
$this->response->getManager()->parseExcludes($transformerExcludes);
}
$this->debugLog($user, 'Response Manager (?!) applied');
$transformer = new ActivityTransformer();
$transformer->setConsumer($user);
$this->debugLog($user, 'Activity Transformer added');
$resource = new \League\Fractal\Resource\Collection($activities, $transformer);
$page = $searchCriteria->getPageNumber();
$this->debugLog($user, 'Search criteria page number called', ['page' => $page]);
$histogram = array_pluck(array_get($searchResponse, 'histogram.buckets', []), 'doc_count', 'key_as_string');
$this->debugLog($user, 'Histogram generated. Response is ready.', ['histogram' => $histogram]);
return $this->response->withArray([
'pagination' => [
'total' => $searchResponse['totalHits'],
'current' => $page,
'prev' => max($page - 1, 1),
'next' => $page + 1,
],
'results' => $this->response->getManager()->createData($resource)->toArray(),
'histogram' => $histogram,
]);
}
private function debugLog(User $user, string $logMessage, ?array $context = []): void
{
// Debug for Learning People Only
if ($user->getTeamId() !== 260) {
return;
}
Log::notice(
sprintf('[activity-search-controller] %s', $logMessage),
$context
);
}
/** @throws ValidationException */
private function validateSearch(Request $request, FilterDefinitionCollection $filterSet, ?string $prefix = null): void
{
$rules = [
'exclude' => 'array',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
];
if ($prefix !== null && mb_strpos($prefix, '.') !== false) {
$rules[rtrim($prefix, '.')] = sprintf(
'required|array|max:%d',
$filterSet->count()
);
}
$validationRules = $filterSet->getValidationRules($prefix)
->merge($rules)
->all();
$request->validate($validationRules);
}
public function createActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$search = $this->updateOrCreateActivitySearch($request);
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function updateActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('update', $search);
$this->updateOrCreateActivitySearch($request, $search);
return $this->response->withOk();
}
private function storeNamedSearchFilters(
Collection $request,
Search $search,
FilterDefinitionCollection $filterSet,
?string $prefix = null,
): self {
$arrayTypeProperties = $filterSet
->getPropertyTypes([
FilterDefinitionCollection::PROPERTY_TYPE_ARRAY,
])
->all();
$supportedRequestProperties = $filterSet->getSupportedRequestProperties($prefix);
foreach ($supportedRequestProperties as $requestPropertyName) {
if (! array_has($request, $requestPropertyName)) {
continue;
}
/** @var string|string[] $propertyValue */
$propertyValue = array_get($request, $requestPropertyName);
$propertyName = $prefix === null
? $requestPropertyName
: mb_substr($requestPropertyName, mb_strlen($prefix));
$isArrayType = array_has($arrayTypeProperties, $propertyName);
if (! $isArrayType) {
/** @var string $requestPropertyValue */
$search->filters()->updateOrCreate(
[
'filter' => $propertyName,
],
[
'value' => $propertyValue,
]
);
continue;
}
/** @var string[] $requestPropertyValue */
/** @var SearchFilter[]|Collection $existingFilterValues */
$existingFilterValuesKeyed = $search->filters()
->where('filter', $propertyName)
->get()
->keyBy('id');
// Iterate over values provided as request parameters
foreach ($propertyValue as $value) {
/** @var SearchFilter|null $valueFilter */
$valueFilter = $search->filters()
->where(
[
'filter' => $propertyName,
'value' => $value,
]
)
->first();
if ($valueFilter !== null) {
// Remove filter value pair from list to be deleted
$existingFilterValuesKeyed->forget($valueFilter->id);
} else {
// Add new filter/value pair
$search->filters()->updateOrCreate([
'filter' => $propertyName,
'value' => $value,
]);
}
}
// Delete filter value pairs for this filter that no longer exist in request parameters
foreach ($existingFilterValuesKeyed as $existingFilter) {
$existingFilter->delete();
}
}
/** @var Collection<int, SearchFilter> $filtersKeyed */
$filtersKeyed = $search->filters()->get()->keyBy('filter');
// wipe removed filters from this search
foreach ($filtersKeyed as $filterName => $filter) {
if (array_has($request, $prefix . $filterName)) {
continue;
}
// Remove all filter values for this filter
$search->filters()->where('filter', $filterName)->delete();
}
return $this;
}
/**
* @throws AuthorizationException
*/
public function fetchActivitySearch(
Search $search,
Request $request,
SearchTransformer $searchTransformer,
): JsonResponse {
$this->authorize('view', $search);
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withItem(
$search,
$searchTransformer
->withConsumer($user)
);
}
public function listActivitySearch(Request $request, SearchTransformer $searchTransformer): JsonResponse
{
/** @var User $user */
$user = $request->user();
$this->response
->getManager()
->setSerializer(new JsonSerializer());
return $this->response->withCollection(
$user->searches()->get(),
$searchTransformer
->withConsumer($user)
);
}
/**
* Deletes a saved search
*
* @param Request $request
* @param Search $search
*
* @throws Exception
*
* @return JsonResponse
*/
public function deleteActivitySearch(Request $request, Search $search): JsonResponse
{
$this->authorize('delete', $search);
$ajReportCount = $this->ajReportsRepository->countActiveReportsBySavedSearch($search->getId());
if ($ajReportCount > 0) {
return $this->response->errorWrongArgs(
"This saved search is used by {$ajReportCount} active AJ report(s). "
. 'Please remove or update those reports before deleting this saved search.'
);
}
$search->filters()->delete();
$search->delete();
return $this->response->withOk();
}
public function live(Request $request, ElasticActivityRepository $repository): JsonResponse
{
$user = $this->getUserFromRequest($request);
$this->request->validate([
'sort_direction' => 'in:asc,desc',
'limit' => 'integer|min:1|max:50',
'page' => 'integer|min:1',
]);
$activities = $repository->getLiveCoachingEligibleActivities(
user: $user,
lookBackMinutes: self::LOOK_BACK,
limit: (int) $this->request->input('limit', 25),
page: (int) $this->request->input('page', 1),
sortBy: ['actual_start_time', 'scheduled_start_time'],
sortDirection: (string) $this->request->input('sort_direction', 'asc'),
);
$this->response
->getManager()
->parseIncludes(['organizer.group', 'prospect'])
->setSerializer(new JsonSerializer());
return $this->response->withCollection($activities, new ActivityTransformer());
}
/**
* @param Activity $activity
*
* @throws AuthorizationException
*
* @return mixed
*/
public function show(Activity $activity, ActivityService $activityService): JsonResponse
{
$this->authorize('show', $activity);
$user = $activity->getUser();
$team = $user->getTeam();
// Sync the opportunity with the latest data if possible.
if ($activity->opportunity_id) {
try {
$crmService = $this->providerRegistry->get($team->crm->provider);
if (! $user->isCrmRequired()) {
$crmService->setUser($team->getOwner());
} else {
$crmService->setUser($user);
}
$crmService->syncOpportunity($activity->opportunity->crm_provider_id);
} catch (Exception $exception) {
// Move on.
}
}
$activityData = $activityService->getActivityData($this->request->user(), $activity);
return response()->json($activityData);
}
public function createRecording(Activity $activity)
{
$this->authorize('record', $activity);
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Tell Twilio to start recording this activity.
if ($activity->recording_state === Activity::RECORDING_OFF) {
$job = (new StartRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withCreated();
}
return $this->response->errorGone('Activity is already recording.');
}
public function updateRecording(Request $request, Activity $activity)
{
$this->authorize('record', $activity);
$request->validate([
'preference' => 'boolean',
'state' => [
'string',
Rule::in([
Activity::RECORDING_IN_PROGRESS,
Activity::RECORDING_PAUSED,
]),
],
]);
if ($request->has('state')) {
if ($activity->hasRecordingReasonComplianceRestricted()) {
return $this->response->errorGone('Recording this number has been disabled by your organization.');
}
// Toggle the recording state between paused and resumed.
if (! $activity->isRecordingState(Activity::RECORDING_OFF)) {
$job = (new ToggleRecording($activity, $request->input('state')))
->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Recording is not toggleable.');
}
if ($request->has('preference')) {
$activity->update([
'recording_preference' => $request->input('preference') ? 1 : 0,
]);
return $this->response->withOk();
}
return $this->response->errorWrongArgs('Something went wrong');
}
public function stopRecording(Activity $activity)
{
$this->authorize('stopRecord', $activity);
// Tell Twilio to stop recording this activity.
if ($activity->isRecordingState(Activity::RECORDING_IN_PROGRESS)) {
$job = (new StopRecording($activity))->onQueue(Constants::QUEUE_CONFERENCES);
dispatch($job);
return $this->response->withOk();
}
return $this->response->errorGone('Activity is not recording.');
}
/**
* Add activity to this user's favorites playlist
*
* @throws AuthorizationException
*/
public function favorite(Activity $activity, PlaylistActivityRepository $playlistActivityRepository): JsonResponse
{
$this->authorize('favorite', $activity);
$user = $this->getUserFromRequest($this->request);
$favorite = $activity->wasFavoritedBy($user);
$name = $activity->activity_title ?? '';
// It needs to check at least one record.
if (! $favorite) {
$favoritePlaylist = $user->favoritePlaylist();
$playlistActivity = $playlistActivityRepository->findByBaseActivityUserAndPlaylist(
$activity,
$user,
$favoritePlaylist
);
if ($playlistActivity !== null) {
$playlistActivity->update(
// Just update, don't sort.
['start_time' => 0, 'name' => mb_strimwidth($name, 0, 100)],
);
} else {
$playlistActivity = $activity->playlistActivities()->create([
'playlist_id' => $favoritePlaylist->getId(),
'user_id' => $user->getId(),
'start_time' => 0,
'name' => mb_strimwidth($name, 0, 100),
]);
// Sort it on top.
$playlistActivity->update(
[
'sort' => $playlistActivityRepository->calculateNewSortOrder(
null,
$playlistActivity,
),
],
);
}
$playlistActivityRepository->calculateNewSortOrder(null, $playlistActivity);
return new JsonResponse([], JsonResponse::HTTP_CREATED);
}
return new JsonResponse(
[
'error' => [
'code' => AbstractResponse::CODE_CONFLICT,
'http_code' => JsonResponse::HTTP_CONFLICT,
'message' => 'Resource Already Exists',
],
],
JsonResponse::HTTP_CONFLICT,
);
}
/**
* Remove activity from th...
|
PhpStorm
|
faVsco.js – ActivityController.php
|
NULL
|
76692
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
1
1
1
1
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Listeners\Activities\Coaching\Planhat;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Component\Model\VisibilityConstraintsInterface;
use Jiminny\Events\Activities\Coaching\Commented;
use Jiminny\Listeners\Activities\PlanhatActivityListener;
use Jiminny\Models\Activity\Comment;
class CreateCommentedEvent extends PlanhatActivityListener
{
/**
* Handle the event.
*/
public function handle(Commented $event): void
{
// Don't attempt to run this on environments with Planhat not configured.
if (! config('services.planhat.enabled')) {
return;
}
if ($event->comment->getType() !== Comment::TYPE_NEUTRAL) {
return;
}
if ($event->comment->getVisibility() !== VisibilityConstraintsInterface::VISIBILITY_PUBLIC) {
// Uncomment if you want to stop non public comments
// return;
}
try {
$this->planhatService->track(
$event->comment->getUser(),
'commented-on-activity',
$this->fetchPayload($event->comment->getActivity())
);
} catch (GuzzleException $e) {
\Log::error('Failed to track event', [
'exception' => $e,
]);
// Retry later.
$this->release(3600);
}
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – CreateCommentedEvent.php
|
NULL
|
76693
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
1
1
1
1
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Listeners\Activities\Coaching\Planhat;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Component\Model\VisibilityConstraintsInterface;
use Jiminny\Events\Activities\Coaching\Commented;
use Jiminny\Listeners\Activities\PlanhatActivityListener;
use Jiminny\Models\Activity\Comment;
class CreateCommentedEvent extends PlanhatActivityListener
{
/**
* Handle the event.
*/
public function handle(Commented $event): void
{
// Don't attempt to run this on environments with Planhat not configured.
if (! config('services.planhat.enabled')) {
return;
}
if ($event->comment->getType() !== Comment::TYPE_NEUTRAL) {
return;
}
if ($event->comment->getVisibility() !== VisibilityConstraintsInterface::VISIBILITY_PUBLIC) {
// Uncomment if you want to stop non public comments
// return;
}
try {
$this->planhatService->track(
$event->comment->getUser(),
'commented-on-activity',
$this->fetchPayload($event->comment->getActivity())
);
} catch (GuzzleException $e) {
\Log::error('Failed to track event', [
'exception' => $e,
]);
// Retry later.
$this->release(3600);
}
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – CreateCommentedEvent.php
|
NULL
|
76694
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
1
1
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Listeners\Activities\Coaching\Planhat;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\Activities\Coaching\Coached;
use Jiminny\Listeners\Activities\PlanhatActivityListener;
class CreateSelfCoachedEvent extends PlanhatActivityListener
{
/**
* Handle the event.
*/
public function handle(Coached $event): void
{
// Don't attempt to run this on environments with Planhat not configured.
if (! config('services.planhat.enabled')) {
return;
}
if ($event->coachingFeedback->coachee_id === $event->coachingFeedback->coach_id) {
try {
$this->planhatService->track(
$event->coachingFeedback->coach,
'self-coached-activity',
$this->fetchPayload($event->coachingFeedback->activity)
);
} catch (GuzzleException $e) {
\Log::error('Failed to track event', [
'exception' => $e,
]);
// Retry later.
$this->release(3600);
}
}
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – CreateSelfCoachedEvent.php
|
NULL
|
76695
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Code changed:
Hide
Sync Changes
Hide This Notification
1
1
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Listeners\Activities\Coaching\Planhat;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\Activities\Coaching\Coached;
use Jiminny\Listeners\Activities\PlanhatActivityListener;
class CreateSelfCoachedEvent extends PlanhatActivityListener
{
/**
* Handle the event.
*/
public function handle(Coached $event): void
{
// Don't attempt to run this on environments with Planhat not configured.
if (! config('services.planhat.enabled')) {
return;
}
if ($event->coachingFeedback->coachee_id === $event->coachingFeedback->coach_id) {
try {
$this->planhatService->track(
$event->coachingFeedback->coach,
'self-coached-activity',
$this->fetchPayload($event->coachingFeedback->activity)
);
} catch (GuzzleException $e) {
\Log::error('Failed to track event', [
'exception' => $e,
]);
// Retry later.
$this->release(3600);
}
}
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – CreateSelfCoachedEvent.php
|
NULL
|
76696
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
8
1
7
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\AskAnything\AskAnythingPrompt;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Traits\RequiresUUID;
/**
* Jiminny\Models\AutomatedReport
*
* @property int $id
* @property string $uuid
* @property int $team_id
* @property string $type
* @property bool $status
* @property string $frequency
* @property Carbon|null $from
* @property Carbon|null $to
* @property int|null $deal_value_min
* @property int|null $deal_value_max
* @property array $call_types
* @property array $media_types
* @property int|null $call_duration_min
* @property int|null $call_duration_max
* @property array|null $groups
* @property array|null $playbook_categories
* @property array|null $deal_at_call_stages
* @property array|null $current_deal_stages
* @property array $recipients
* @property string|null $additional_prompt_input
* @property string|null $custom_name
* @property int|null $activity_search_id
* @property int|null $ask_anything_prompt_id
* @property Carbon|null $expires_at
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
* @property-read \Jiminny\Models\Team $team
* @property-read \Jiminny\Models\Activity\Search|null $savedSearch
* @property-read \Jiminny\Models\AskAnything\AskAnythingPrompt|null $askAnythingPrompt
*/
class AutomatedReport extends Model
{
use RequiresUUID;
use SoftDeletes;
protected $table = 'automated_reports';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'team_id',
'type',
'status',
'frequency',
'from',
'to',
'deal_value_min',
'deal_value_max',
'call_types',
'media_types',
'call_duration_min',
'call_duration_max',
'groups',
'playbook_categories',
'deal_at_call_stages',
'current_deal_stages',
'recipients',
'jiminny_recipients',
'additional_prompt_input',
'custom_name',
'created_by',
'activity_search_id',
'ask_anything_prompt_id',
'expires_at',
];
protected $hidden = ['uuid'];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'status' => 'boolean',
'from' => 'datetime',
'to' => 'datetime',
'call_types' => 'array',
'media_types' => 'array',
'groups' => 'array',
'playbook_categories' => 'array',
'deal_at_call_stages' => 'array',
'current_deal_stages' => 'array',
'recipients' => 'array',
'jiminny_recipients' => 'array',
'expires_at' => 'date',
'deleted_at' => 'datetime',
];
}
/**
* Get the team that owns the automated report.
*/
public function team()
{
return $this->belongsTo(Team::class);
}
/**
*
* Get the user who created the report.
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
public function savedSearch(): BelongsTo
{
return $this->belongsTo(Search::class, 'activity_search_id');
}
public function askAnythingPrompt(): BelongsTo
{
return $this->belongsTo(AskAnythingPrompt::class, 'ask_anything_prompt_id');
}
public function isAskJiminnyReport(): bool
{
return $this->getType() === AutomatedReportsService::TYPE_ASK_JIMINNY;
}
public function isExpired(): bool
{
$expiresAt = $this->getExpiresAt();
return $expiresAt !== null && $expiresAt->isPast();
}
public function getActivitySearchId(): ?int
{
return $this->getAttribute('activity_search_id');
}
public function getAskAnythingPromptId(): ?int
{
return $this->getAttribute('ask_anything_prompt_id');
}
public function getExpiresAt(): ?Carbon
{
return $this->getAttribute('expires_at');
}
public function getSavedSearch(): ?Search
{
return $this->getAttribute('savedSearch');
}
public function getAskAnythingPrompt(): ?AskAnythingPrompt
{
return $this->getAttribute('askAnythingPrompt');
}
/**
* Get the ID of the automated report.
*
* @return int
*/
public function getId(): int
{
return $this->getAttribute('id');
}
/**
* Get the UUID of the automated report.
*
* @return string
*/
public function getUuid(): string
{
return $this->getAttribute('id_string');
}
/**
* Get the team ID of the automated report.
*
* @return int
*/
public function getTeamId(): int
{
return $this->getAttribute('team_id');
}
/**
* Get the type of the automated report.
*
* @return string
*/
public function getType(): string
{
return $this->getAttribute('type');
}
/**
* Get the status of the automated report.
* True means active, false means inactive.
*
* @return bool
*/
public function getStatus(): bool
{
return $this->getAttribute('status');
}
/**
* Get the frequency of the automated report.
*
* @return string
*/
public function getFrequency(): string
{
return $this->getAttribute('frequency');
}
/**
* Get the from date of the automated report.
*
* @return Carbon|null
*/
public function getFrom(): ?Carbon
{
return $this->getAttribute('from');
}
/**
* Get the to date of the automated report.
*
* @return Carbon|null
*/
public function getTo(): ?Carbon
{
return $this->getAttribute('to');
}
/**
* Get the minimum deal value of the automated report.
*
* @return int|null
*/
public function getDealValueMin(): ?int
{
return $this->getAttribute('deal_value_min');
}
/**
* Get the maximum deal value of the automated report.
*
* @return int|null
*/
public function getDealValueMax(): ?int
{
return $this->getAttribute('deal_value_max');
}
/**
* Get the call types of the automated report.
*
* @return array
*/
public function getCallTypes(): array
{
return $this->getAttribute('call_types') ?? [];
}
public function getMediaTypes(): array
{
return $this->getAttribute('media_types') ?? [];
}
/**
* Get the minimum call duration of the automated report.
*
* @return int|null
*/
public function getCallDurationMin(): ?int
{
return $this->getAttribute('call_duration_min');
}
/**
* Get the maximum call duration of the automated report.
*
* @return int|null
*/
public function getCallDurationMax(): ?int
{
return $this->getAttribute('call_duration_max');
}
/**
* Get the groups of the automated report.
*
* @return array
*/
public function getGroups(): array
{
return $this->getAttribute('groups') ?? [];
}
/**
* Get the playbook categories of the automated report.
*
* @return array
*/
public function getPlaybookCategories(): array
{
return $this->getAttribute('playbook_categories') ?? [];
}
/**
* Get the deal at call stages of the automated report.
*
* @return array
*/
public function getDealAtCallStages(): array
{
return $this->getAttribute('deal_at_call_stages') ?? [];
}
/**
* Get the current deal stages of the automated report.
*
* @return array
*/
public function getCurrentDealStages(): array
{
return $this->getAttribute('current_deal_stages') ?? [];
}
/**
* Get the recipients of the automated report.
*
* @return array
*/
public function getRecipients(): array
{
return $this->getAttribute('recipients') ?? [];
}
/**
* Get the Jiminny's recipients of the automated report.
*
* @return array
*/
public function getJiminnyRecipients(): array
{
return $this->getAttribute('jiminny_recipients') ?? [];
}
/**
* Get the additional prompt input of the automated report.
*
* @return string|null
*/
public function getAdditionalPromptInput(): ?string
{
return $this->getAttribute('additional_prompt_input');
}
public function getCustomName(): ?string
{
return $this->getAttribute('custom_name');
}
/**
* Get the created at date of the automated report.
*
* @return Carbon
*/
public function getCreatedAt(): Carbon
{
return $this->getAttribute('created_at');
}
/**
* Get the updated at date of the automated report.
*
* @return Carbon
*/
public function getUpdatedAt(): Carbon
{
return $this->getAttribute('updated_at');
}
/**
* Get the deleted at date of the automated report.
*
* @return Carbon|null
*/
public function getDeletedAt(): ?Carbon
{
return $this->getAttribute('deleted_at');
}
public function getTeam(): Team
{
return $this->getAttribute('team');
}
public function getCreator(): ?User
{
return $this->getAttribute('creator');
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – AutomatedReport.php
|
NULL
|
76697
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
4
3
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\AskAnything\AskAnythingPrompt;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Traits\RequiresUUID;
/**
* Jiminny\Models\AutomatedReport
*
* @property int $id
* @property string $uuid
* @property int $team_id
* @property string $type
* @property bool $status
* @property string $frequency
* @property Carbon|null $from
* @property Carbon|null $to
* @property int|null $deal_value_min
* @property int|null $deal_value_max
* @property array $call_types
* @property array $media_types
* @property int|null $call_duration_min
* @property int|null $call_duration_max
* @property array|null $groups
* @property array|null $playbook_categories
* @property array|null $deal_at_call_stages
* @property array|null $current_deal_stages
* @property array $recipients
* @property string|null $additional_prompt_input
* @property string|null $custom_name
* @property int|null $activity_search_id
* @property int|null $ask_anything_prompt_id
* @property Carbon|null $expires_at
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
* @property-read \Jiminny\Models\Team $team
* @property-read \Jiminny\Models\Activity\Search|null $savedSearch
* @property-read \Jiminny\Models\AskAnything\AskAnythingPrompt|null $askAnythingPrompt
*/
class AutomatedReport extends Model
{
use RequiresUUID;
use SoftDeletes;
protected $table = 'automated_reports';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'team_id',
'type',
'status',
'frequency',
'from',
'to',
'deal_value_min',
'deal_value_max',
'call_types',
'media_types',
'call_duration_min',
'call_duration_max',
'groups',
'playbook_categories',
'deal_at_call_stages',
'current_deal_stages',
'recipients',
'jiminny_recipients',
'additional_prompt_input',
'custom_name',
'created_by',
'activity_search_id',
'ask_anything_prompt_id',
'expires_at',
];
protected $hidden = ['uuid'];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'status' => 'boolean',
'from' => 'datetime',
'to' => 'datetime',
'call_types' => 'array',
'media_types' => 'array',
'groups' => 'array',
'playbook_categories' => 'array',
'deal_at_call_stages' => 'array',
'current_deal_stages' => 'array',
'recipients' => 'array',
'jiminny_recipients' => 'array',
'expires_at' => 'date',
'deleted_at' => 'datetime',
];
}
/**
* Get the team that owns the automated report.
*/
public function team()
{
return $this->belongsTo(Team::class);
}
/**
*
* Get the user who created the report.
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
public function savedSearch(): BelongsTo
{
return $this->belongsTo(Search::class, 'activity_search_id');
}
public function askAnythingPrompt(): BelongsTo
{
return $this->belongsTo(AskAnythingPrompt::class, 'ask_anything_prompt_id');
}
public function isAskJiminnyReport(): bool
{
return $this->getType() === AutomatedReportsService::TYPE_ASK_JIMINNY;
}
public function isExpired(): bool
{
$expiresAt = $this->getExpiresAt();
return $expiresAt !== null && $expiresAt->isPast();
}
public function getActivitySearchId(): ?int
{
return $this->getAttribute('activity_search_id');
}
public function getAskAnythingPromptId(): ?int
{
return $this->getAttribute('ask_anything_prompt_id');
}
public function getExpiresAt(): ?Carbon
{
return $this->getAttribute('expires_at');
}
public function getSavedSearch(): ?Search
{
return $this->getAttribute('savedSearch');
}
public function getAskAnythingPrompt(): ?AskAnythingPrompt
{
return $this->getAttribute('askAnythingPrompt');
}
/**
* Get the ID of the automated report.
*
* @return int
*/
public function getId(): int
{
return $this->getAttribute('id');
}
/**
* Get the UUID of the automated report.
*
* @return string
*/
public function getUuid(): string
{
return $this->getAttribute('id_string');
}
/**
* Get the team ID of the automated report.
*
* @return int
*/
public function getTeamId(): int
{
return $this->getAttribute('team_id');
}
/**
* Get the type of the automated report.
*
* @return string
*/
public function getType(): string
{
return $this->getAttribute('type');
}
/**
* Get the status of the automated report.
* True means active, false means inactive.
*
* @return bool
*/
public function getStatus(): bool
{
return $this->getAttribute('status');
}
/**
* Get the frequency of the automated report.
*
* @return string
*/
public function getFrequency(): string
{
return $this->getAttribute('frequency');
}
/**
* Get the from date of the automated report.
*
* @return Carbon|null
*/
public function getFrom(): ?Carbon
{
return $this->getAttribute('from');
}
/**
* Get the to date of the automated report.
*
* @return Carbon|null
*/
public function getTo(): ?Carbon
{
return $this->getAttribute('to');
}
/**
* Get the minimum deal value of the automated report.
*
* @return int|null
*/
public function getDealValueMin(): ?int
{
return $this->getAttribute('deal_value_min');
}
/**
* Get the maximum deal value of the automated report.
*
* @return int|null
*/
public function getDealValueMax(): ?int
{
return $this->getAttribute('deal_value_max');
}
/**
* Get the call types of the automated report.
*
* @return array
*/
public function getCallTypes(): array
{
return $this->getAttribute('call_types') ?? [];
}
public function getMediaTypes(): array
{
return $this->getAttribute('media_types') ?? [];
}
/**
* Get the minimum call duration of the automated report.
*
* @return int|null
*/
public function getCallDurationMin(): ?int
{
return $this->getAttribute('call_duration_min');
}
/**
* Get the maximum call duration of the automated report.
*
* @return int|null
*/
public function getCallDurationMax(): ?int
{
return $this->getAttribute('call_duration_max');
}
/**
* Get the groups of the automated report.
*
* @return array
*/
public function getGroups(): array
{
return $this->getAttribute('groups') ?? [];
}
/**
* Get the playbook categories of the automated report.
*
* @return array
*/
public function getPlaybookCategories(): array
{
return $this->getAttribute('playbook_categories') ?? [];
}
/**
* Get the deal at call stages of the automated report.
*
* @return array
*/
public function getDealAtCallStages(): array
{
return $this->getAttribute('deal_at_call_stages') ?? [];
}
/**
* Get the current deal stages of the automated report.
*
* @return array
*/
public function getCurrentDealStages(): array
{
return $this->getAttribute('current_deal_stages') ?? [];
}
/**
* Get the recipients of the automated report.
*
* @return array
*/
public function getRecipients(): array
{
return $this->getAttribute('recipients') ?? [];
}
/**
* Get the Jiminny's recipients of the automated report.
*
* @return array
*/
public function getJiminnyRecipients(): array
{
return $this->getAttribute('jiminny_recipients') ?? [];
}
/**
* Get the additional prompt input of the automated report.
*
* @return string|null
*/
public function getAdditionalPromptInput(): ?string
{
return $this->getAttribute('additional_prompt_input');
}
public function getCustomName(): ?string
{
return $this->getAttribute('custom_name');
}
/**
* Get the created at date of the automated report.
*
* @return Carbon
*/
public function getCreatedAt(): Carbon
{
return $this->getAttribute('created_at');
}
/**
* Get the updated at date of the automated report.
*
* @return Carbon
*/
public function getUpdatedAt(): Carbon
{
return $this->getAttribute('updated_at');
}
/**
* Get the deleted at date of the automated report.
*
* @return Carbon|null
*/
public function getDeletedAt(): ?Carbon
{
return $this->getAttribute('deleted_at');
}
public function getTeam(): Team
{
return $this->getAttribute('team');
}
public function getCreator(): ?User
{
return $this->getAttribute('creator');
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – AutomatedReport.php
|
NULL
|
76698
|
|
DMSActivityMorerireroxToolsHelpcalMistorbookmarksJ DMSActivityMorerireroxToolsHelpcalMistorbookmarksJiminny …..vXStarredi• jiminny-x-integrati..8 platform-inner-teamE) Channels# ai-chapter# ai-team# alerts# backend# c-learning-peoplei confusion-clinic# curiosity_labadeal-insichts-dev# engineering# frontend# general# infra-changes# jiminny-bg8 people-with-copilo...8 people-with-zoom-# platform-team# platform-tickets# product_launches# random# releases# sofia-office# support# thank-yous# the Deople of iimi...ProtllesWindow& platform-inner-...& 10MessagesChannel OverviewMoreYesterdayjinnylaop Aor 22nd Added by GitHubNikolay Ivanov 3:24 PMнякой нещо да е настроивал по githubactions. Почна да прави къмити вместо менбез да съм му разрешевал?https:/github.com/lminnv/app/pull/1200//changes/a68f42f210859f838a4fdced451f750627besoioИли нещо аз не разбирам?0AA0e 20 replies Last reply 18...Nikolay Yankov 3:50PMreplied to a uhread: някои нешо ла е насто..лол. ами предлагам маи ла му заораним лапускам към всички ла вилятNikolav Yankov 9.38 AMЩе се забавя за дейлито. Започнете без Мен.Aneliva Angelova 9:43 AMIДобро утро, няма да успея да вляза влейлито. Пествам ньлжовете.Message & platform-inner-team+ Aa I..•) New TabAl reports promotion pages by nik• JY-9712 | Nuges to expire after on8 Jiminnyu Userpilot Logged-activityJY-20157 add not enough activ XPipelines - jiminny/app+ New Tab©github.com/jimjiminny / app 8<> Code87 Pull requests 31( Agents |© Actions•• Wiki © Security and quality 32 ~ Insights 3 Settings@ On April 24 we'll start using GitHub Copilot interaction data for Al model training unless you opt out. Review this update and manage your preferences in your GitHub account settings.JY-20157 add not enough activities notification #12011 •$1 Open LakyLak wants to merge 2 commits into master from JY-20157-AJ-report-not-send-notification@) Conversation o• Commits 2|- Checks 21E Files changed 13A © All commits +Q Filter files...apo/Console/Commands/Reports/AutomatedReportsCommand.ohp@ -61,21 +61,29 @ public function handle(): intv = Console/Commands/Renorts|Snow = Carbon: : now();E AutomatedReportsCommand...v Jobs/AutomatedReportsE RequestGenerateAskJiminnyR...SendReportNotGeneratedMail...v @ Listeners/AutomatedReports/U….E TrackAutomatedReportGener...v # Mail/ReportsS1sMondav = Snow->1SMonday)S1sr1rstDayUtMonth = Snow->day === 1;ScurrentMonth = Snow->month.// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);$this->logger->info(self::LOG_PREFIX . ' Checking conditions', [+ ReportNotGenerated.ohp |"1SMonday' => S1SMonday,~ E Services/Kiosk/AutomatedRepo…..AskJiminnyReportActivityServ…'isFirstDay0fMonth' => SisFirstDay0fMonth,'currentMonth' => ScurrentMonth.E AutomatedReportsService.php'isQuarterlyMonth' => SisQuarterlyMonth,~E resources/views/emails/reportsreport-not-generated.blade.php/I Process dailv revortsl• F tests/UnitSthis->processReports(AutomatedReportsService::FREQUENCYDAILY):~ Jobs/AutomatedReportsE ReguestGenerateAsk JiminnvR....v = listeners/AutomatedRenorts/U.₴ TrackAutomatedReportGener..v E Services/Kiosk/AutomatedRepo…..E AskJiminnyReportActivityServ....AutomatedReportsServiceActi…./ Process weekly renorts on Mondavcif (SisMondav) {64 +67 +74 +86 +@40@ Daily - Platform - nowQ Type to search100% C4 8• Fri 24 Apr 9:46:13• Checks pending Code • (Preview) -+384 -52 9000C 0 I 13 viewedSubmit review+10 -2 mane [ Viewed0 ...Snow = Carhon:.nowdSisMondav = Snow->1SMonday)"Sisweekend = $now->isWeekend():SisFirstDay0fMonth = Snow->day === 1;ScurrentMonth = Snow->month.SisManualTrigger = $this->option('report-id') !== null;// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);Sthis->loager->info(self::L0G PREFIX . ' Checkina conditions'. [I"isMonday' => SisMonday,'isweekend' => $isWeekend,'isFirstDay0fMonth' => $isFirstDay0fMonth,'currentMonth' => ScurrentMonth.l'isQuarterlyMonth' => SisQuarterlyMonth,/ Process dailv renorts on weekdavs onlv (skio Saturdav/Sundav)...// Manual triggers via --report-id bypass the weekend skip.if (I Sisweekend || SisManualTriager) {Sthis->processReports(AutomatedReportsService::FREQUENCY_DAILY):} else {ISthis->logger->info(self::L0G PREFIX . ' Skipping daily reports on weekend'):/ Process weekly renorts on Mondavslif (SisMonday) {...
|
PhpStorm
|
faVsco.js – AutomatedReport.php
|
NULL
|
76699
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
4
1
7
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\AskAnything\AskAnythingPrompt;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Traits\RequiresUUID;
/**
* Jiminny\Models\AutomatedReport
*
* @property int $id
* @property string $uuid
* @property int $team_id
* @property string $type
* @property bool $status
* @property string $frequency
* @property Carbon|null $from
* @property Carbon|null $to
* @property int|null $deal_value_min
* @property int|null $deal_value_max
* @property array $call_types
* @property array $media_types
* @property int|null $call_duration_min
* @property int|null $call_duration_max
* @property array|null $groups
* @property array|null $playbook_categories
* @property array|null $deal_at_call_stages
* @property array|null $current_deal_stages
* @property array $recipients
* @property string|null $additional_prompt_input
* @property string|null $custom_name
* @property int|null $activity_search_id
* @property int|null $ask_anything_prompt_id
* @property Carbon|null $expires_at
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
* @property-read \Jiminny\Models\Team $team
* @property-read \Jiminny\Models\Activity\Search|null $savedSearch
* @property-read \Jiminny\Models\AskAnything\AskAnythingPrompt|null $askAnythingPrompt
*/
class AutomatedReport extends Model
{
use RequiresUUID;
use SoftDeletes;
protected $table = 'automated_reports';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'team_id',
'type',
'status',
'frequency',
'from',
'to',
'deal_value_min',
'deal_value_max',
'call_types',
'media_types',
'call_duration_min',
'call_duration_max',
'groups',
'playbook_categories',
'deal_at_call_stages',
'current_deal_stages',
'recipients',
'jiminny_recipients',
'additional_prompt_input',
'custom_name',
'created_by',
'activity_search_id',
'ask_anything_prompt_id',
'expires_at',
];
protected $hidden = ['uuid'];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'status' => 'boolean',
'from' => 'datetime',
'to' => 'datetime',
'call_types' => 'array',
'media_types' => 'array',
'groups' => 'array',
'playbook_categories' => 'array',
'deal_at_call_stages' => 'array',
'current_deal_stages' => 'array',
'recipients' => 'array',
'jiminny_recipients' => 'array',
'expires_at' => 'date',
'deleted_at' => 'datetime',
];
}
/**
* Get the team that owns the automated report.
*/
public function team()
{
return $this->belongsTo(Team::class);
}
/**
*
* Get the user who created the report.
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
public function savedSearch(): BelongsTo
{
return $this->belongsTo(Search::class, 'activity_search_id');
}
public function askAnythingPrompt(): BelongsTo
{
return $this->belongsTo(AskAnythingPrompt::class, 'ask_anything_prompt_id');
}
public function isAskJiminnyReport(): bool
{
return $this->getType() === AutomatedReportsService::TYPE_ASK_JIMINNY;
}
public function isExpired(): bool
{
$expiresAt = $this->getExpiresAt();
return $expiresAt !== null && $expiresAt->isPast();
}
public function getActivitySearchId(): ?int
{
return $this->getAttribute('activity_search_id');
}
public function getAskAnythingPromptId(): ?int
{
return $this->getAttribute('ask_anything_prompt_id');
}
public function getExpiresAt(): ?Carbon
{
return $this->getAttribute('expires_at');
}
public function getSavedSearch(): ?Search
{
return $this->getAttribute('savedSearch');
}
public function getAskAnythingPrompt(): ?AskAnythingPrompt
{
return $this->getAttribute('askAnythingPrompt');
}
/**
* Get the ID of the automated report.
*
* @return int
*/
public function getId(): int
{
return $this->getAttribute('id');
}
/**
* Get the UUID of the automated report.
*
* @return string
*/
public function getUuid(): string
{
return $this->getAttribute('id_string');
}
/**
* Get the team ID of the automated report.
*
* @return int
*/
public function getTeamId(): int
{
return $this->getAttribute('team_id');
}
/**
* Get the type of the automated report.
*
* @return string
*/
public function getType(): string
{
return $this->getAttribute('type');
}
/**
* Get the status of the automated report.
* True means active, false means inactive.
*
* @return bool
*/
public function getStatus(): bool
{
return $this->getAttribute('status');
}
/**
* Get the frequency of the automated report.
*
* @return string
*/
public function getFrequency(): string
{
return $this->getAttribute('frequency');
}
/**
* Get the from date of the automated report.
*
* @return Carbon|null
*/
public function getFrom(): ?Carbon
{
return $this->getAttribute('from');
}
/**
* Get the to date of the automated report.
*
* @return Carbon|null
*/
public function getTo(): ?Carbon
{
return $this->getAttribute('to');
}
/**
* Get the minimum deal value of the automated report.
*
* @return int|null
*/
public function getDealValueMin(): ?int
{
return $this->getAttribute('deal_value_min');
}
/**
* Get the maximum deal value of the automated report.
*
* @return int|null
*/
public function getDealValueMax(): ?int
{
return $this->getAttribute('deal_value_max');
}
/**
* Get the call types of the automated report.
*
* @return array
*/
public function getCallTypes(): array
{
return $this->getAttribute('call_types') ?? [];
}
public function getMediaTypes(): array
{
return $this->getAttribute('media_types') ?? [];
}
/**
* Get the minimum call duration of the automated report.
*
* @return int|null
*/
public function getCallDurationMin(): ?int
{
return $this->getAttribute('call_duration_min');
}
/**
* Get the maximum call duration of the automated report.
*
* @return int|null
*/
public function getCallDurationMax(): ?int
{
return $this->getAttribute('call_duration_max');
}
/**
* Get the groups of the automated report.
*
* @return array
*/
public function getGroups(): array
{
return $this->getAttribute('groups') ?? [];
}
/**
* Get the playbook categories of the automated report.
*
* @return array
*/
public function getPlaybookCategories(): array
{
return $this->getAttribute('playbook_categories') ?? [];
}
/**
* Get the deal at call stages of the automated report.
*
* @return array
*/
public function getDealAtCallStages(): array
{
return $this->getAttribute('deal_at_call_stages') ?? [];
}
/**
* Get the current deal stages of the automated report.
*
* @return array
*/
public function getCurrentDealStages(): array
{
return $this->getAttribute('current_deal_stages') ?? [];
}
/**
* Get the recipients of the automated report.
*
* @return array
*/
public function getRecipients(): array
{
return $this->getAttribute('recipients') ?? [];
}
/**
* Get the Jiminny's recipients of the automated report.
*
* @return array
*/
public function getJiminnyRecipients(): array
{
return $this->getAttribute('jiminny_recipients') ?? [];
}
/**
* Get the additional prompt input of the automated report.
*
* @return string|null
*/
public function getAdditionalPromptInput(): ?string
{
return $this->getAttribute('additional_prompt_input');
}
public function getCustomName(): ?string
{
return $this->getAttribute('custom_name');
}
/**
* Get the created at date of the automated report.
*
* @return Carbon
*/
public function getCreatedAt(): Carbon
{
return $this->getAttribute('created_at');
}
/**
* Get the updated at date of the automated report.
*
* @return Carbon
*/
public function getUpdatedAt(): Carbon
{
return $this->getAttribute('updated_at');
}
/**
* Get the deleted at date of the automated report.
*
* @return Carbon|null
*/
public function getDeletedAt(): ?Carbon
{
return $this->getAttribute('deleted_at');
}
public function getTeam(): Team
{
return $this->getAttribute('team');
}
public function getCreator(): ?User
{
return $this->getAttribute('creator');
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – AutomatedReport.php
|
NULL
|
76700
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
4
1
7
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\AskAnything\AskAnythingPrompt;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Traits\RequiresUUID;
/**
* Jiminny\Models\AutomatedReport
*
* @property int $id
* @property string $uuid
* @property int $team_id
* @property string $type
* @property bool $status
* @property string $frequency
* @property Carbon|null $from
* @property Carbon|null $to
* @property int|null $deal_value_min
* @property int|null $deal_value_max
* @property array $call_types
* @property array $media_types
* @property int|null $call_duration_min
* @property int|null $call_duration_max
* @property array|null $groups
* @property array|null $playbook_categories
* @property array|null $deal_at_call_stages
* @property array|null $current_deal_stages
* @property array $recipients
* @property string|null $additional_prompt_input
* @property string|null $custom_name
* @property int|null $activity_search_id
* @property int|null $ask_anything_prompt_id
* @property Carbon|null $expires_at
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
* @property-read \Jiminny\Models\Team $team
* @property-read \Jiminny\Models\Activity\Search|null $savedSearch
* @property-read \Jiminny\Models\AskAnything\AskAnythingPrompt|null $askAnythingPrompt
*/
class AutomatedReport extends Model
{
use RequiresUUID;
use SoftDeletes;
protected $table = 'automated_reports';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'team_id',
'type',
'status',
'frequency',
'from',
'to',
'deal_value_min',
'deal_value_max',
'call_types',
'media_types',
'call_duration_min',
'call_duration_max',
'groups',
'playbook_categories',
'deal_at_call_stages',
'current_deal_stages',
'recipients',
'jiminny_recipients',
'additional_prompt_input',
'custom_name',
'created_by',
'activity_search_id',
'ask_anything_prompt_id',
'expires_at',
];
protected $hidden = ['uuid'];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'status' => 'boolean',
'from' => 'datetime',
'to' => 'datetime',
'call_types' => 'array',
'media_types' => 'array',
'groups' => 'array',
'playbook_categories' => 'array',
'deal_at_call_stages' => 'array',
'current_deal_stages' => 'array',
'recipients' => 'array',
'jiminny_recipients' => 'array',
'expires_at' => 'date',
'deleted_at' => 'datetime',
];
}
/**
* Get the team that owns the automated report.
*/
public function team()
{
return $this->belongsTo(Team::class);
}
/**
*
* Get the user who created the report.
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
public function savedSearch(): BelongsTo
{
return $this->belongsTo(Search::class, 'activity_search_id');
}
public function askAnythingPrompt(): BelongsTo
{
return $this->belongsTo(AskAnythingPrompt::class, 'ask_anything_prompt_id');
}
public function isAskJiminnyReport(): bool
{
return $this->getType() === AutomatedReportsService::TYPE_ASK_JIMINNY;
}
public function isExpired(): bool
{
$expiresAt = $this->getExpiresAt();
return $expiresAt !== null && $expiresAt->isPast();
}
public function getActivitySearchId(): ?int
{
return $this->getAttribute('activity_search_id');
}
public function getAskAnythingPromptId(): ?int
{
return $this->getAttribute('ask_anything_prompt_id');
}
public function getExpiresAt(): ?Carbon
{
return $this->getAttribute('expires_at');
}
public function getSavedSearch(): ?Search
{
return $this->getAttribute('savedSearch');
}
public function getAskAnythingPrompt(): ?AskAnythingPrompt
{
return $this->getAttribute('askAnythingPrompt');
}
/**
* Get the ID of the automated report.
*
* @return int
*/
public function getId(): int
{
return $this->getAttribute('id');
}
/**
* Get the UUID of the automated report.
*
* @return string
*/
public function getUuid(): string
{
return $this->getAttribute('id_string');
}
/**
* Get the team ID of the automated report.
*
* @return int
*/
public function getTeamId(): int
{
return $this->getAttribute('team_id');
}
/**
* Get the type of the automated report.
*
* @return string
*/
public function getType(): string
{
return $this->getAttribute('type');
}
/**
* Get the status of the automated report.
* True means active, false means inactive.
*
* @return bool
*/
public function getStatus(): bool
{
return $this->getAttribute('status');
}
/**
* Get the frequency of the automated report.
*
* @return string
*/
public function getFrequency(): string
{
return $this->getAttribute('frequency');
}
/**
* Get the from date of the automated report.
*
* @return Carbon|null
*/
public function getFrom(): ?Carbon
{
return $this->getAttribute('from');
}
/**
* Get the to date of the automated report.
*
* @return Carbon|null
*/
public function getTo(): ?Carbon
{
return $this->getAttribute('to');
}
/**
* Get the minimum deal value of the automated report.
*
* @return int|null
*/
public function getDealValueMin(): ?int
{
return $this->getAttribute('deal_value_min');
}
/**
* Get the maximum deal value of the automated report.
*
* @return int|null
*/
public function getDealValueMax(): ?int
{
return $this->getAttribute('deal_value_max');
}
/**
* Get the call types of the automated report.
*
* @return array
*/
public function getCallTypes(): array
{
return $this->getAttribute('call_types') ?? [];
}
public function getMediaTypes(): array
{
return $this->getAttribute('media_types') ?? [];
}
/**
* Get the minimum call duration of the automated report.
*
* @return int|null
*/
public function getCallDurationMin(): ?int
{
return $this->getAttribute('call_duration_min');
}
/**
* Get the maximum call duration of the automated report.
*
* @return int|null
*/
public function getCallDurationMax(): ?int
{
return $this->getAttribute('call_duration_max');
}
/**
* Get the groups of the automated report.
*
* @return array
*/
public function getGroups(): array
{
return $this->getAttribute('groups') ?? [];
}
/**
* Get the playbook categories of the automated report.
*
* @return array
*/
public function getPlaybookCategories(): array
{
return $this->getAttribute('playbook_categories') ?? [];
}
/**
* Get the deal at call stages of the automated report.
*
* @return array
*/
public function getDealAtCallStages(): array
{
return $this->getAttribute('deal_at_call_stages') ?? [];
}
/**
* Get the current deal stages of the automated report.
*
* @return array
*/
public function getCurrentDealStages(): array
{
return $this->getAttribute('current_deal_stages') ?? [];
}
/**
* Get the recipients of the automated report.
*
* @return array
*/
public function getRecipients(): array
{
return $this->getAttribute('recipients') ?? [];
}
/**
* Get the Jiminny's recipients of the automated report.
*
* @return array
*/
public function getJiminnyRecipients(): array
{
return $this->getAttribute('jiminny_recipients') ?? [];
}
/**
* Get the additional prompt input of the automated report.
*
* @return string|null
*/
public function getAdditionalPromptInput(): ?string
{
return $this->getAttribute('additional_prompt_input');
}
public function getCustomName(): ?string
{
return $this->getAttribute('custom_name');
}
/**
* Get the created at date of the automated report.
*
* @return Carbon
*/
public function getCreatedAt(): Carbon
{
return $this->getAttribute('created_at');
}
/**
* Get the updated at date of the automated report.
*
* @return Carbon
*/
public function getUpdatedAt(): Carbon
{
return $this->getAttribute('updated_at');
}
/**
* Get the deleted at date of the automated report.
*
* @return Carbon|null
*/
public function getDeletedAt(): ?Carbon
{
return $this->getAttribute('deleted_at');
}
public function getTeam(): Team
{
return $this->getAttribute('team');
}
public function getCreator(): ?User
{
return $this->getAttribute('creator');
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – AutomatedReport.php
|
NULL
|
76701
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Firefox• 0FileEditViewHistory→BookmarksProfilesToolsWindowHelpmeet.google.com/agt-teir-cwt?authuser=lukas.kovalik%40jiminny.com•Daily - Platform - now100% K78 • Fri 24 Apr 9:46:13|=Pop out this videoNikolay NikolovStefka StoyanovaGalya DimitrovaLukas Kovalik9:46 AM | Daily - Platform• 0:27...
|
PhpStorm
|
faVsco.js – AutomatedReport.php
|
NULL
|
76702
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
Debug 'AskJiminnyReportActivityServiceTest'
More Actions
JetBrains AI
Search Everywhere
IDE and Project Settings
Sync Changes
Hide This Notification
Code changed:
Hide
4
1
7
Previous Highlighted Error
Next Highlighted Error
<?php
namespace Jiminny\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
use Jiminny\Models\Activity\Search;
use Jiminny\Models\AskAnything\AskAnythingPrompt;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Traits\RequiresUUID;
/**
* Jiminny\Models\AutomatedReport
*
* @property int $id
* @property string $uuid
* @property int $team_id
* @property string $type
* @property bool $status
* @property string $frequency
* @property Carbon|null $from
* @property Carbon|null $to
* @property int|null $deal_value_min
* @property int|null $deal_value_max
* @property array $call_types
* @property array $media_types
* @property int|null $call_duration_min
* @property int|null $call_duration_max
* @property array|null $groups
* @property array|null $playbook_categories
* @property array|null $deal_at_call_stages
* @property array|null $current_deal_stages
* @property array $recipients
* @property string|null $additional_prompt_input
* @property string|null $custom_name
* @property int|null $activity_search_id
* @property int|null $ask_anything_prompt_id
* @property Carbon|null $expires_at
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property Carbon|null $deleted_at
* @property-read \Jiminny\Models\Team $team
* @property-read \Jiminny\Models\Activity\Search|null $savedSearch
* @property-read \Jiminny\Models\AskAnything\AskAnythingPrompt|null $askAnythingPrompt
*/
class AutomatedReport extends Model
{
use RequiresUUID;
use SoftDeletes;
protected $table = 'automated_reports';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'team_id',
'type',
'status',
'frequency',
'from',
'to',
'deal_value_min',
'deal_value_max',
'call_types',
'media_types',
'call_duration_min',
'call_duration_max',
'groups',
'playbook_categories',
'deal_at_call_stages',
'current_deal_stages',
'recipients',
'jiminny_recipients',
'additional_prompt_input',
'custom_name',
'created_by',
'activity_search_id',
'ask_anything_prompt_id',
'expires_at',
];
protected $hidden = ['uuid'];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'status' => 'boolean',
'from' => 'datetime',
'to' => 'datetime',
'call_types' => 'array',
'media_types' => 'array',
'groups' => 'array',
'playbook_categories' => 'array',
'deal_at_call_stages' => 'array',
'current_deal_stages' => 'array',
'recipients' => 'array',
'jiminny_recipients' => 'array',
'expires_at' => 'date',
'deleted_at' => 'datetime',
];
}
/**
* Get the team that owns the automated report.
*/
public function team()
{
return $this->belongsTo(Team::class);
}
/**
*
* Get the user who created the report.
*/
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by');
}
public function savedSearch(): BelongsTo
{
return $this->belongsTo(Search::class, 'activity_search_id');
}
public function askAnythingPrompt(): BelongsTo
{
return $this->belongsTo(AskAnythingPrompt::class, 'ask_anything_prompt_id');
}
public function isAskJiminnyReport(): bool
{
return $this->getType() === AutomatedReportsService::TYPE_ASK_JIMINNY;
}
public function isExpired(): bool
{
$expiresAt = $this->getExpiresAt();
return $expiresAt !== null && $expiresAt->isPast();
}
public function getActivitySearchId(): ?int
{
return $this->getAttribute('activity_search_id');
}
public function getAskAnythingPromptId(): ?int
{
return $this->getAttribute('ask_anything_prompt_id');
}
public function getExpiresAt(): ?Carbon
{
return $this->getAttribute('expires_at');
}
public function getSavedSearch(): ?Search
{
return $this->getAttribute('savedSearch');
}
public function getAskAnythingPrompt(): ?AskAnythingPrompt
{
return $this->getAttribute('askAnythingPrompt');
}
/**
* Get the ID of the automated report.
*
* @return int
*/
public function getId(): int
{
return $this->getAttribute('id');
}
/**
* Get the UUID of the automated report.
*
* @return string
*/
public function getUuid(): string
{
return $this->getAttribute('id_string');
}
/**
* Get the team ID of the automated report.
*
* @return int
*/
public function getTeamId(): int
{
return $this->getAttribute('team_id');
}
/**
* Get the type of the automated report.
*
* @return string
*/
public function getType(): string
{
return $this->getAttribute('type');
}
/**
* Get the status of the automated report.
* True means active, false means inactive.
*
* @return bool
*/
public function getStatus(): bool
{
return $this->getAttribute('status');
}
/**
* Get the frequency of the automated report.
*
* @return string
*/
public function getFrequency(): string
{
return $this->getAttribute('frequency');
}
/**
* Get the from date of the automated report.
*
* @return Carbon|null
*/
public function getFrom(): ?Carbon
{
return $this->getAttribute('from');
}
/**
* Get the to date of the automated report.
*
* @return Carbon|null
*/
public function getTo(): ?Carbon
{
return $this->getAttribute('to');
}
/**
* Get the minimum deal value of the automated report.
*
* @return int|null
*/
public function getDealValueMin(): ?int
{
return $this->getAttribute('deal_value_min');
}
/**
* Get the maximum deal value of the automated report.
*
* @return int|null
*/
public function getDealValueMax(): ?int
{
return $this->getAttribute('deal_value_max');
}
/**
* Get the call types of the automated report.
*
* @return array
*/
public function getCallTypes(): array
{
return $this->getAttribute('call_types') ?? [];
}
public function getMediaTypes(): array
{
return $this->getAttribute('media_types') ?? [];
}
/**
* Get the minimum call duration of the automated report.
*
* @return int|null
*/
public function getCallDurationMin(): ?int
{
return $this->getAttribute('call_duration_min');
}
/**
* Get the maximum call duration of the automated report.
*
* @return int|null
*/
public function getCallDurationMax(): ?int
{
return $this->getAttribute('call_duration_max');
}
/**
* Get the groups of the automated report.
*
* @return array
*/
public function getGroups(): array
{
return $this->getAttribute('groups') ?? [];
}
/**
* Get the playbook categories of the automated report.
*
* @return array
*/
public function getPlaybookCategories(): array
{
return $this->getAttribute('playbook_categories') ?? [];
}
/**
* Get the deal at call stages of the automated report.
*
* @return array
*/
public function getDealAtCallStages(): array
{
return $this->getAttribute('deal_at_call_stages') ?? [];
}
/**
* Get the current deal stages of the automated report.
*
* @return array
*/
public function getCurrentDealStages(): array
{
return $this->getAttribute('current_deal_stages') ?? [];
}
/**
* Get the recipients of the automated report.
*
* @return array
*/
public function getRecipients(): array
{
return $this->getAttribute('recipients') ?? [];
}
/**
* Get the Jiminny's recipients of the automated report.
*
* @return array
*/
public function getJiminnyRecipients(): array
{
return $this->getAttribute('jiminny_recipients') ?? [];
}
/**
* Get the additional prompt input of the automated report.
*
* @return string|null
*/
public function getAdditionalPromptInput(): ?string
{
return $this->getAttribute('additional_prompt_input');
}
public function getCustomName(): ?string
{
return $this->getAttribute('custom_name');
}
/**
* Get the created at date of the automated report.
*
* @return Carbon
*/
public function getCreatedAt(): Carbon
{
return $this->getAttribute('created_at');
}
/**
* Get the updated at date of the automated report.
*
* @return Carbon
*/
public function getUpdatedAt(): Carbon
{
return $this->getAttribute('updated_at');
}
/**
* Get the deleted at date of the automated report.
*
* @return Carbon|null
*/
public function getDeletedAt(): ?Carbon
{
return $this->getAttribute('deleted_at');
}
public function getTeam(): Team
{
return $this->getAttribute('team');
}
public function getCreator(): ?User
{
return $this->getAttribute('creator');
}
}
Sync Changes
Hide This Notification
Code changed:
Hide
43
Previous Highlighted Error
Next Highlighted Error
<?php
declare(strict_types=1);
namespace Jiminny\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Jiminny\Component\Queue\Constants;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\Contracts\UserContract;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use Illuminate\Support\Facades\Log;
class TrackAutomatedReportGeneratedEvent implements ShouldQueue
{
use InteractsWithQueue;
private const string EVENT_NAME_AUTOMATED_REPORT = 'automated-report-generated';
private const string EVENT_NAME_ASK_JIMINNY_REPORT = 'ask-jiminny-report-generated';
public string $queue = Constants::QUEUE_DELAYABLE;
public function __construct(
private readonly UserPilotClient $userPilotClient,
private readonly AutomatedReportsService $automatedReportsService,
) {
}
public function handle(AutomatedReportGenerated $event): void
{
if (config('services.userpilot.token') === null) {
return;
}
$automatedReport = $event->automatedReport;
$payload = $this->buildPayload($automatedReport);
$eventName = $this->resolveEventName($automatedReport);
$users = $this->resolveUsers($automatedReport);
if (empty($users)) {
Log::warning('[UserPilot] No recipients found for automated report', [
'report_id' => $automatedReport->getId(),
'is_ask_jiminny' => $automatedReport->isAskJiminnyReport(),
]);
return;
}
Log::info('[UserPilot] Sending automated report event', [
'report_id' => $automatedReport->getId(),
'event_name' => $eventName,
'recipient_count' => count($users),
]);
try {
foreach ($users as $user) {
$this->userPilotClient->track($user, $eventName, $payload);
}
} catch (GuzzleException $e) {
Log::error('[UserPilot] Failed to send automated report event', [
'report_id' => $automatedReport->getId(),
'error' => $e->getMessage(),
]);
$this->release(3600);
}
}
/**
* @return array<UserContract>
*/
private function resolveUsers(AutomatedReport $automatedReport): array
{
if ($automatedReport->isAskJiminnyReport()) {
$creator = $automatedReport->getCreator();
return $creator !== null ? [$creator] : [];
}
return $this->automatedReportsService->getRecipientUserObjects($automatedReport);
}
private function buildPayload(AutomatedReport $automatedReport): array
{
return [
'report_type' => $automatedReport->getType(),
'frequency' => $automatedReport->getFrequency(),
];
}
private function resolveEventName(AutomatedReport $automatedReport): string
{
if ($automatedReport->isAskJiminnyReport()) {
return self::EVENT_NAME_ASK_JIMINNY_REPORT;
}
return self::EVENT_NAME_AUTOMATED_REPORT;
}
}
+++
<?php
declare(strict_types=1);
namespace Tests\Unit\Listeners\AutomatedReports\UserPilot;
use GuzzleHttp\Exception\GuzzleException;
use Jiminny\Events\AutomatedReports\AutomatedReportGenerated;
use Jiminny\Listeners\AutomatedReports\UserPilot\TrackAutomatedReportGeneratedEvent;
use Jiminny\Models\AutomatedReport;
use Jiminny\Models\User;
use Jiminny\Services\Kiosk\AutomatedReports\AutomatedReportsService;
use Jiminny\Services\UserPilot\UserPilotClient;
use PHPUnit\Framework\MockObject\MockObject;
use Tests\TestCase;
class TrackAutomatedReportGeneratedEventTest extends TestCase
{
private UserPilotClient&MockObject $userPilotClient;
private AutomatedReportsService&MockObject $automatedReportsService;
protected function setUp(): void
{
parent::setUp();
$this->userPilotClient = $this->createMock(UserPilotClient::class);
$this->automatedReportsService = $this->createMock(AutomatedReportsService::class);
}
private function makeListener(): TrackAutomatedReportGeneratedEvent
{
return new TrackAutomatedReportGeneratedEvent(
$this->userPilotClient,
$this->automatedReportsService,
);
}
private function makeEvent(AutomatedReport $report): AutomatedReportGenerated
{
return new AutomatedReportGenerated($report);
}
public function testHandleSkipsWhenUserPilotTokenIsNull(): void
{
config(['services.userpilot.token' => null]);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->never())->method('isAskJiminnyReport');
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksCreatorForAskJiminnyReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn($creator);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(123);
$this->automatedReportsService->expects($this->never())->method('getRecipientUserObjects');
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$creator,
'ask-jiminny-report-generated',
['report_type' => AutomatedReportsService::TYPE_ASK_JIMINNY, 'frequency' => 'weekly']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleSkipsTrackingWhenAskJiminnyCreatorIsNull(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(true);
$report->expects($this->once())->method('getCreator')->willReturn(null);
$report->expects($this->once())->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->expects($this->once())->method('getFrequency')->willReturn('weekly');
$report->expects($this->once())->method('getId')->willReturn(456);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleTracksAllRecipientsForExecReport(): void
{
config(['services.userpilot.token' => 'NX-token']);
$userOne = $this->createMock(User::class);
$userTwo = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(789);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$userOne, $userTwo]);
$this->userPilotClient->expects($this->exactly(2))
->method('track')
->willReturnCallback(function ($user, $eventName, $payload) use ($userOne, $userTwo) {
$this->assertTrue($user === $userOne || $user === $userTwo);
$this->assertSame('automated-report-generated', $eventName);
$this->assertSame(['report_type' => 'exec_summary', 'frequency' => 'monthly'], $payload);
return null;
});
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotTrackWhenExecReportHasNoRecipients(): void
{
config(['services.userpilot.token' => 'NX-token']);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(3))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('exec_summary');
$report->expects($this->once())->method('getFrequency')->willReturn('monthly');
$report->expects($this->once())->method('getId')->willReturn(101);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->willReturn([]);
$this->userPilotClient->expects($this->never())->method('track');
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
public function testHandleDoesNotThrowOnGuzzleException(): void
{
config(['services.userpilot.token' => 'NX-token']);
$creator = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->method('isAskJiminnyReport')->willReturn(true);
$report->method('getCreator')->willReturn($creator);
$report->method('getType')->willReturn(AutomatedReportsService::TYPE_ASK_JIMINNY);
$report->method('getFrequency')->willReturn('daily');
$report->method('getId')->willReturn(202);
$guzzleException = $this->createMock(GuzzleException::class);
$this->userPilotClient->expects($this->once())
->method('track')
->with($creator, 'ask-jiminny-report-generated', $this->anything())
->willThrowException($guzzleException);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
$this->addToAssertionCount(1);
}
public function testHandleTracksAutomatedReportWithSingleRecipient(): void
{
config(['services.userpilot.token' => 'NX-token']);
$user = $this->createMock(User::class);
$report = $this->createMock(AutomatedReport::class);
$report->expects($this->exactly(2))->method('isAskJiminnyReport')->willReturn(false);
$report->expects($this->once())->method('getType')->willReturn('team_performance');
$report->expects($this->once())->method('getFrequency')->willReturn('daily');
$report->expects($this->once())->method('getId')->willReturn(303);
$this->automatedReportsService->expects($this->once())
->method('getRecipientUserObjects')
->with($report)
->willReturn([$user]);
$this->userPilotClient->expects($this->once())
->method('track')
->with(
$user,
'automated-report-generated',
['report_type' => 'team_performance', 'frequency' => 'daily']
);
$listener = $this->makeListener();
$listener->handle($this->makeEvent($report));
}
}
Project
Project
New File or Directory…
Expand Selected
Collapse All
Options
Hide...
|
PhpStorm
|
faVsco.js – AutomatedReport.php
|
NULL
|
76703
|
|
Project: faVsco.js, menu
JY-20738-debug-AJ-trackin Project: faVsco.js, menu
JY-20738-debug-AJ-tracking-UP, menu
Start Listening for PHP Debug Connections
AskJiminnyReportActivityServiceTest
Run 'AskJiminnyReportActivityServiceTest'
DMSActivityMorerireroxToolsHelpcalMistorbookmarksJiminny …..vXStarredi• jiminny-x-integrati..8 platform-inner-teamE) Channels# ai-chapter# ai-team# alerts# backend# c-learning-peoplei confusion-clinic# curiosity_labadeal-insichts-dev# engineering# frontend# general# infra-changes# jiminny-bg8 people-with-copilo...8 people-with-zoom-# platform-team# platform-tickets# product_launches# random# releases# sofia-office# support# thank-yous# the Deople of iimi...ProtllesWindow& platform-inner-...& 10MessagesChannel OverviewMoreYesterdayjinnylaop Aor 22nd Added by GitHubNikolay Ivanov 3:24 PMнякой нещо да е настроивал по githubactions. Почна да прави къмити вместо менбез да съм му разрешевал?https:/github.com/lminnv/app/pull/1200//changes/a68f42f210859f838a4fdced451f750627besoioИли нещо аз не разбирам?0AA0e 20 replies Last reply 18...Nikolay Yankov 3:50PMreplied to a uhread: някои нешо ла е насто..лол. ами предлагам маи ла му заораним лапускам към всички ла вилятNikolav Yankov 9.38 AMЩе се забавя за дейлито. Започнете без Мен.Aneliva Angelova 9:43 AMIДобро утро, няма да успея да вляза влейлито. Пествам ньлжовете.Message & platform-inner-team+ Aa I..•) New TabAl reports promotion pages by nik• JY-9712 | Nuges to expire after on8 Jiminnyu Userpilot Logged-activityJY-20157 add not enough activ XPipelines - jiminny/app+ New Tab©github.com/jimjiminny / app 8<> Code87 Pull requests 31( Agents |© Actions•• Wiki © Security and quality 32 ~ Insights 3 Settings@ On April 24 we'll start using GitHub Copilot interaction data for Al model training unless you opt out. Review this update and manage your preferences in your GitHub account settings.JY-20157 add not enough activities notification #12011 •$1 Open LakyLak wants to merge 2 commits into master from JY-20157-AJ-report-not-send-notification@) Conversation o• Commits 2|- Checks 21E Files changed 13A © All commits +Q Filter files...apo/Console/Commands/Reports/AutomatedReportsCommand.ohp@ -61,21 +61,29 @ public function handle(): intv = Console/Commands/Renorts|Snow = Carbon: : now();E AutomatedReportsCommand...v Jobs/AutomatedReportsE RequestGenerateAskJiminnyR...SendReportNotGeneratedMail...v @ Listeners/AutomatedReports/U….E TrackAutomatedReportGener...v # Mail/ReportsS1sMondav = Snow->1SMonday)S1sr1rstDayUtMonth = Snow->day === 1;ScurrentMonth = Snow->month.// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);$this->logger->info(self::LOG_PREFIX . ' Checking conditions', [+ ReportNotGenerated.ohp |"1SMonday' => S1SMonday,~ E Services/Kiosk/AutomatedRepo…..AskJiminnyReportActivityServ…'isFirstDay0fMonth' => SisFirstDay0fMonth,'currentMonth' => ScurrentMonth.E AutomatedReportsService.php'isQuarterlyMonth' => SisQuarterlyMonth,~E resources/views/emails/reportsreport-not-generated.blade.php/I Process dailv revortsl• F tests/UnitSthis->processReports(AutomatedReportsService::FREQUENCYDAILY):~ Jobs/AutomatedReportsE ReguestGenerateAsk JiminnvR....v = listeners/AutomatedRenorts/U.₴ TrackAutomatedReportGener..v E Services/Kiosk/AutomatedRepo…..E AskJiminnyReportActivityServ....AutomatedReportsServiceActi…./ Process weekly renorts on Mondavcif (SisMondav) {64 +67 +74 +86 +@40@ Daily - Platform - nowQ Type to search100% C4 8• Fri 24 Apr 9:46:13• Checks pending Code • (Preview) -+384 -52 9000C 0 I 13 viewedSubmit review+10 -2 mane [ Viewed0 ...Snow = Carhon:.nowdSisMondav = Snow->1SMonday)"Sisweekend = $now->isWeekend():SisFirstDay0fMonth = Snow->day === 1;ScurrentMonth = Snow->month.SisManualTrigger = $this->option('report-id') !== null;// Check if the current month is a quarterly month (January, April, July, October)$isQuarterlyMonth = in_array($currentMonth, [1, 4, 7, 10], true);Sthis->loager->info(self::L0G PREFIX . ' Checkina conditions'. [I"isMonday' => SisMonday,'isweekend' => $isWeekend,'isFirstDay0fMonth' => $isFirstDay0fMonth,'currentMonth' => ScurrentMonth.l'isQuarterlyMonth' => SisQuarterlyMonth,/ Process dailv renorts on weekdavs onlv (skio Saturdav/Sundav)...// Manual triggers via --report-id bypass the weekend skip.if (I Sisweekend || SisManualTriager) {Sthis->processReports(AutomatedReportsService::FREQUENCY_DAILY):} else {ISthis->logger->info(self::L0G PREFIX . ' Skipping daily reports on weekend'):/ Process weekly renorts on Mondavslif (SisMonday) {...
|
PhpStorm
|
faVsco.js – AutomatedReport.php
|
NULL
|
76704
|