From d9d9a369835ee52c1327fafb75cf17e5b557fb9d Mon Sep 17 00:00:00 2001 From: kaioken Date: Fri, 20 Dec 2024 21:41:38 -0400 Subject: [PATCH 1/9] feat: add message purchse to channel --- .../Workflows/LinkMessageToOrderActivity.php | 21 +++++++++++++++++++ .../Social/Channels/Models/Channel.php | 10 +++++++++ .../Services/DistributionMessageService.php | 6 +----- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php b/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php index 51d97f381..ef26664cf 100644 --- a/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php +++ b/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php @@ -6,10 +6,13 @@ use Baka\Contracts\AppInterface; use Illuminate\Database\Eloquent\Model; +use Kanvas\Social\Channels\Actions\CreateChannelAction; +use Kanvas\Social\Channels\DataTransferObject\Channel; use Kanvas\Social\Messages\Actions\CreateAppModuleMessageAction; use Kanvas\Social\Messages\Models\Message; use Kanvas\Souk\Orders\Models\Order; use Kanvas\SystemModules\Repositories\SystemModulesRepository; +use Kanvas\Users\Models\Users; use Kanvas\Workflow\Contracts\WorkflowActivityInterface; use Kanvas\Workflow\KanvasActivity; @@ -38,6 +41,24 @@ public function execute(Model $order, AppInterface $app, array $params): array ['message_id' => $message->getId(), 'message' => $message->message] ); $order->saveOrFail(); + $user = $order->user; + + $newPurchaseMessageChannel = new CreateChannelAction( + new Channel( + apps: $app, + companies: $message->company, + users: $order->user, + entity_id: $user->getId(), + entity_namespace: Users::class, + name: 'Purchase Message', + description: 'Purchase Message Channel', + slug: 'PMC-' . $user->uuid + ), + $order->user + ); + + $purchaseChannel = $newPurchaseMessageChannel->execute(); + $purchaseChannel->addMessage($message, $user); return [ 'order' => $order->id, diff --git a/src/Domains/Social/Channels/Models/Channel.php b/src/Domains/Social/Channels/Models/Channel.php index 0a662c50e..7f6f7d353 100644 --- a/src/Domains/Social/Channels/Models/Channel.php +++ b/src/Domains/Social/Channels/Models/Channel.php @@ -5,6 +5,7 @@ namespace Kanvas\Social\Channels\Models; use Baka\Traits\UuidTrait; +use Baka\Users\Contracts\UserInterface; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Kanvas\Social\Messages\Models\Message; @@ -52,4 +53,13 @@ public function messages(): BelongsToMany return $this->belongsToMany(Message::class, 'channel_messages', 'channel_id', 'messages_id') ->withTimestamps(); } + + public function addMessage(Message $message, ?UserInterface $user = null): void + { + $this->messages()->attach($message->id, [ + 'users_id' => $user ? $user->getId() : $message->users_id, + ]); + $this->last_message_id = $message->id; + $this->saveOrFail(); + } } diff --git a/src/Domains/Social/Channels/Services/DistributionMessageService.php b/src/Domains/Social/Channels/Services/DistributionMessageService.php index 4407da48d..864a94d6f 100644 --- a/src/Domains/Social/Channels/Services/DistributionMessageService.php +++ b/src/Domains/Social/Channels/Services/DistributionMessageService.php @@ -11,11 +11,7 @@ class DistributionMessageService { public static function sentToChannelFeed(Channel $channel, Message $message): Channel { - $channel->messages()->attach($message->id, [ - 'users_id' => $message->users_id, - ]); - $channel->last_message_id = $message->id; - $channel->save(); + $channel->addMessage($message); return $channel; } From 0aee0ec33d01966a5f4cefd4a45d1c1ddbe05393 Mon Sep 17 00:00:00 2001 From: kaioken Date: Fri, 20 Dec 2024 22:13:46 -0400 Subject: [PATCH 2/9] refact: add message purchse to channel --- .../Orders/AppleInAppPurchaseMutation.php | 9 +- .../CreateOrderFromAppleReceiptAction.php | 9 +- .../Workflows/LinkMessageToOrderActivity.php | 5 +- .../Channels/Actions/CreateChannelAction.php | 13 ++- .../Channels/DataTransferObject/Channel.php | 2 +- .../Souk/Orders/Actions/CreateOrderAction.php | 37 +++++--- .../InAppPurchase/AppleInAppPurchaseTest.php | 93 +++++++++++++++++++ 7 files changed, 140 insertions(+), 28 deletions(-) diff --git a/app/GraphQL/Souk/Mutations/Orders/AppleInAppPurchaseMutation.php b/app/GraphQL/Souk/Mutations/Orders/AppleInAppPurchaseMutation.php index bc98200d2..5a4123726 100644 --- a/app/GraphQL/Souk/Mutations/Orders/AppleInAppPurchaseMutation.php +++ b/app/GraphQL/Souk/Mutations/Orders/AppleInAppPurchaseMutation.php @@ -41,13 +41,6 @@ public function create(mixed $root, array $request): Order $createOrderFromInAppPurchase = new CreateOrderFromAppleReceiptAction($appleInAppPurchase); - $order = $createOrderFromInAppPurchase->execute(); - - if (! empty($appleInAppPurchase->custom_fields)) { - $order->setCustomFields($appleInAppPurchase->custom_fields); - $order->saveCustomFields(); - } - - return $order; + return $createOrderFromInAppPurchase->execute(); } } diff --git a/src/Domains/Connectors/InAppPurchase/Actions/CreateOrderFromAppleReceiptAction.php b/src/Domains/Connectors/InAppPurchase/Actions/CreateOrderFromAppleReceiptAction.php index 4d5bb31a4..a21e36214 100644 --- a/src/Domains/Connectors/InAppPurchase/Actions/CreateOrderFromAppleReceiptAction.php +++ b/src/Domains/Connectors/InAppPurchase/Actions/CreateOrderFromAppleReceiptAction.php @@ -70,7 +70,14 @@ public function execute(): ModelsOrder $people ); - return (new CreateOrderAction($orderData))->execute(); + $order = (new CreateOrderAction($orderData))->execute(); + + if (! empty($this->appleInAppPurchase->custom_fields)) { + $order->setCustomFields($this->appleInAppPurchase->custom_fields); + $order->saveCustomFields(); + } + + return $order; } private function verifyReceipt(array $receipt): ReceiptResponse diff --git a/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php b/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php index ef26664cf..0cf3ba8b2 100644 --- a/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php +++ b/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php @@ -34,7 +34,7 @@ public function execute(Model $order, AppInterface $app, array $params): array $message = Message::getById($order->get('message_id'), $app); $orderSystemModule = SystemModulesRepository::getByModelName(Order::class); - $createAppModuleMessage = (new CreateAppModuleMessageAction($message, $orderSystemModule, $order->getId()))->execute(); + (new CreateAppModuleMessageAction($message, $orderSystemModule, $order->getId()))->execute(); $order->metadata = array_merge( $order->metadata, @@ -63,7 +63,8 @@ public function execute(Model $order, AppInterface $app, array $params): array return [ 'order' => $order->id, 'message' => $message->id, - 'slug' => $message->slug, + 'channel' => $purchaseChannel->id, + 'channel_name' => $purchaseChannel->name, ]; } } diff --git a/src/Domains/Social/Channels/Actions/CreateChannelAction.php b/src/Domains/Social/Channels/Actions/CreateChannelAction.php index 539e5bc76..ddbdba103 100644 --- a/src/Domains/Social/Channels/Actions/CreateChannelAction.php +++ b/src/Domains/Social/Channels/Actions/CreateChannelAction.php @@ -5,6 +5,8 @@ namespace Kanvas\Social\Channels\Actions; use Baka\Support\Str; +use Kanvas\AccessControlList\Enums\RolesEnums; +use Kanvas\AccessControlList\Repositories\RolesRepository; use Kanvas\Social\Channels\DataTransferObject\Channel as ChannelDto; use Kanvas\Social\Channels\Models\Channel; @@ -26,7 +28,16 @@ public function execute(): Channel 'entity_id' => $this->channelDto->entity_id, 'entity_namespace' => $this->channelDto->entity_namespace, ]); - $channel->users()->attach($this->channelDto->users->id, ['roles_id' => 1]); + + $channel->users()->attach( + $this->channelDto->users->id, + [ + 'roles_id' => RolesRepository::getByNameFromCompany( + name: RolesEnums::ADMIN->value, + app: $this->channelDto->apps, + )->id, + ] + ); return $channel; } diff --git a/src/Domains/Social/Channels/DataTransferObject/Channel.php b/src/Domains/Social/Channels/DataTransferObject/Channel.php index cb3455e5b..966fba570 100644 --- a/src/Domains/Social/Channels/DataTransferObject/Channel.php +++ b/src/Domains/Social/Channels/DataTransferObject/Channel.php @@ -15,7 +15,7 @@ public function __construct( public Apps $apps, public Companies $companies, public Users $users, - public string $entity_id, + public string|int $entity_id, public string $entity_namespace, public string $name = '', public string $description = '', diff --git a/src/Domains/Souk/Orders/Actions/CreateOrderAction.php b/src/Domains/Souk/Orders/Actions/CreateOrderAction.php index 8a92b3d45..2703778bf 100644 --- a/src/Domains/Souk/Orders/Actions/CreateOrderAction.php +++ b/src/Domains/Souk/Orders/Actions/CreateOrderAction.php @@ -4,6 +4,7 @@ namespace Kanvas\Souk\Orders\Actions; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Validator; use Kanvas\AccessControlList\Enums\RolesEnums; @@ -77,22 +78,28 @@ public function execute(): ModelsOrder ); } - $order->user->notify(new NewOrderNotification($order, [ - 'app' => $this->orderData->app, - 'company' => $this->orderData->company, - ])); + try { + $order->user->notify(new NewOrderNotification($order, [ + 'app' => $this->orderData->app, + 'company' => $this->orderData->company, + ])); + } catch (ModelNotFoundException $e) { + } - UserRoleNotificationService::notify( - RolesEnums::ADMIN->value, - new NewOrderStoreOwnerNotification( - $order, - [ - 'app' => $this->orderData->app, - 'company' => $this->orderData->company, - ] - ), - $this->orderData->app - ); + try { + UserRoleNotificationService::notify( + RolesEnums::ADMIN->value, + new NewOrderStoreOwnerNotification( + $order, + [ + 'app' => $this->orderData->app, + 'company' => $this->orderData->company, + ] + ), + $this->orderData->app + ); + } catch (ModelNotFoundException $e) { + } return $order; }); diff --git a/tests/Connectors/Integration/InAppPurchase/AppleInAppPurchaseTest.php b/tests/Connectors/Integration/InAppPurchase/AppleInAppPurchaseTest.php index b59bca1a4..0f456ca41 100644 --- a/tests/Connectors/Integration/InAppPurchase/AppleInAppPurchaseTest.php +++ b/tests/Connectors/Integration/InAppPurchase/AppleInAppPurchaseTest.php @@ -8,11 +8,18 @@ use Kanvas\Connectors\InAppPurchase\Actions\CreateOrderFromAppleReceiptAction; use Kanvas\Connectors\InAppPurchase\DataTransferObject\AppleInAppPurchaseReceipt; use Kanvas\Connectors\InAppPurchase\Enums\ConfigurationEnum; +use Kanvas\Connectors\InAppPurchase\Workflows\LinkMessageToOrderActivity; use Kanvas\Inventory\Products\Actions\CreateProductAction; use Kanvas\Inventory\Products\DataTransferObject\Product; use Kanvas\Inventory\Support\Setup; use Kanvas\Regions\Models\Regions; +use Kanvas\Social\Channels\Models\Channel; +use Kanvas\Social\Messages\Actions\CreateMessageAction; +use Kanvas\Social\Messages\DataTransferObject\MessageInput; +use Kanvas\Social\MessagesTypes\Actions\CreateMessageTypeAction; +use Kanvas\Social\MessagesTypes\DataTransferObject\MessageTypeInput; use Kanvas\Souk\Orders\Models\Order; +use Kanvas\Workflow\Models\StoredWorkflow; use Tests\TestCase; final class AppleInAppPurchaseTest extends TestCase @@ -66,4 +73,90 @@ public function testAppleInAppPurchaseReceipt() $this->assertInstanceOf(Order::class, $order); } + + public function testAppleInAppPurchaseReceiptChannel() + { + $app = app(Apps::class); + $user = auth()->user(); + $company = $user->getCurrentCompany(); + $setupInventory = new Setup($app, $user, $company); + $setupInventory->run(); + $app->set(ConfigurationEnum::APPLE_PAYMENT_SHARED_SECRET->value, getenv('TEST_APPLE_PAYMENT_SHARED_SECRET')); + + $region = Regions::fromApp($app)->fromCompany($company)->first(); + + $receipt = [ + 'productId' => 'model_price_01', + 'transactionId' => '2000000778751927', + 'transactionReceipt' => 'MIIUKQYJKoZIhvcNAQcCoIIUGjCCFBYCAQExDzANBglghkgBZQMEAgEFADCCA18GCSqGSIb3DQEHAaCCA1AEggNMMYIDSDAKAgEIAgEBBAIWADAKAgEUAgEBBAIMADALAgEBAgEBBAMCAQAwCwIBCwIBAQQDAgEAMAsCAQ8CAQEEAwIBADALAgEQAgEBBAMCAQAwCwIBGQIBAQQDAgEDMAwCAQMCAQEEBAwCNTYwDAIBCgIBAQQEFgI0KzAMAgEOAgEBBAQCAgEAMA0CAQ0CAQEEBQIDAr8hMA0CARMCAQEEBQwDMS4wMA4CAQkCAQEEBgIEUDMwNTAYAgEEAgECBBB/iVD6vEMO3K80msWsb6IzMBsCAQACAQEEEwwRUHJvZHVjdGlvblNhbmRib3gwHAIBAgIBAQQUDBJjb20uYWltZW1vLmFwcC5pb3MwHAIBBQIBAQQUsPK+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lpjPOvsi+JaX55NSeBARBSQ4=', + 'transactionDate' => 1731943806000, + ]; + + $productData = new Product( + app: $app, + company: $company, + user: $user, + name: $receipt['productId'], + sku: $receipt['productId'], + warehouses: [[ + 'quantity' => 10, + 'price' => 0.29, + ], + ] + ); + $product = (new CreateProductAction($productData, $user))->execute(); + + $messageTypeDto = MessageTypeInput::from([ + 'apps_id' => $app->getId(), + 'name' => 'purchase', + 'verb' => 'purchase', + ]); + $messageType = (new CreateMessageTypeAction($messageTypeDto))->execute(); + + $createMessageAction = new CreateMessageAction( + new MessageInput( + app: $app, + company: $company, + user: $user, + type: $messageType, + message: 'Purchase Message', + ) + ); + $message = $createMessageAction->execute(); + $createOrderFromReceipt = new CreateOrderFromAppleReceiptAction( + new AppleInAppPurchaseReceipt( + app: $app, + company: $company, + user: $user, + region: $region, + product_id: $receipt['productId'], + transaction_id: $receipt['transactionId'], + receipt: $receipt['transactionReceipt'], + transaction_date: $receipt['transactionDate'], + custom_fields: [ + [ + 'key' => 'message_id', + 'value' => $message->getId(), + ], + ] + ), + runInSandbox: true + ); + + $order = $createOrderFromReceipt->execute($receipt); + + $activity = new LinkMessageToOrderActivity( + 0, + now()->toDateTimeString(), + StoredWorkflow::make(), + [] + ); + + $result = $activity->execute($order, $app, ['company' => $company]); + + $this->assertArrayHasKey('order', $result); + $this->assertArrayHasKey('message', $result); + $this->assertArrayHasKey('channel', $result); + $this->assertEquals(1, Channel::getById($result['channel'])->messages()->count()); + } } From 25204a90d577d1d2a30a2c86a1adb6a6dcb56bcf Mon Sep 17 00:00:00 2001 From: kaioken Date: Fri, 20 Dec 2024 22:32:08 -0400 Subject: [PATCH 3/9] refact: add message purchse to channel --- app/GraphQL/Social/Builders/Messages/MessageBuilder.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/GraphQL/Social/Builders/Messages/MessageBuilder.php b/app/GraphQL/Social/Builders/Messages/MessageBuilder.php index edf835740..a7895eb4a 100644 --- a/app/GraphQL/Social/Builders/Messages/MessageBuilder.php +++ b/app/GraphQL/Social/Builders/Messages/MessageBuilder.php @@ -43,12 +43,6 @@ public function getAll( //Check in this condition if the message is an item and if then check if it has been bought by the current user via status=completed on Order if (! $user->isAppOwner()) { $messages = Message::fromCompany($user->getCurrentCompany()); - - /* - if ($viewingOneMessage) { - $messages->first()->isLocked(); - } - */ return $messages; } From 9f3cf52aff1cafa7edc695c6833cec35fbd3090e Mon Sep 17 00:00:00 2001 From: kaioken Date: Fri, 20 Dec 2024 22:33:57 -0400 Subject: [PATCH 4/9] refact: add message purchse to channel --- .../InAppPurchase/Workflows/LinkMessageToOrderActivity.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php b/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php index 0cf3ba8b2..990f64074 100644 --- a/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php +++ b/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php @@ -60,6 +60,8 @@ public function execute(Model $order, AppInterface $app, array $params): array $purchaseChannel = $newPurchaseMessageChannel->execute(); $purchaseChannel->addMessage($message, $user); + $user->set('purchase_channel', $purchaseChannel->uuid); + return [ 'order' => $order->id, 'message' => $message->id, From 63eaa41938cb49c7924b4ed58053ca819a3baaf0 Mon Sep 17 00:00:00 2001 From: kaioken Date: Sat, 21 Dec 2024 10:08:04 -0400 Subject: [PATCH 5/9] feat: interactions --- ..._132644_add_is_purchase_social_message.php | 4 ++-- ...024_12_21_140530_message_total_purchse.php | 22 ++++++++++++++++++ .../GenerateGoogleUserMessageAction.php | 1 + .../Workflows/LinkMessageToOrderActivity.php | 3 +++ .../Jobs/GenerateUserMessageJob.php | 1 + .../Services/MessageInteractionService.php | 23 ++++++++++++++++++- 6 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 database/migrations/Social/2024_12_21_140530_message_total_purchse.php diff --git a/database/migrations/Social/2024_12_20_132644_add_is_purchase_social_message.php b/database/migrations/Social/2024_12_20_132644_add_is_purchase_social_message.php index f93cad199..e35cbba1a 100644 --- a/database/migrations/Social/2024_12_20_132644_add_is_purchase_social_message.php +++ b/database/migrations/Social/2024_12_20_132644_add_is_purchase_social_message.php @@ -11,7 +11,7 @@ public function up(): void { Schema::table('user_messages', function (Blueprint $table) { - $table->tinyInteger('is_purchase')->after('is_shared')->default(0)->index('is_purchase'); + $table->tinyInteger('is_purchased')->after('is_shared')->default(0)->index('is_purchased'); }); } @@ -21,7 +21,7 @@ public function up(): void public function down(): void { Schema::table('user_messages', function (Blueprint $table) { - $table->dropColumn('is_purchase'); + $table->dropColumn('is_purchased'); }); } }; diff --git a/database/migrations/Social/2024_12_21_140530_message_total_purchse.php b/database/migrations/Social/2024_12_21_140530_message_total_purchse.php new file mode 100644 index 000000000..6de931365 --- /dev/null +++ b/database/migrations/Social/2024_12_21_140530_message_total_purchse.php @@ -0,0 +1,22 @@ +tinyInteger('total_purchased')->after('total_shared')->default(0)->index('total_purchased'); + }); + } + + public function down(): void + { + Schema::table('messages', function (Blueprint $table) { + $table->dropColumn('total_purchased'); + }); + } +}; diff --git a/src/Domains/Connectors/Google/Actions/GenerateGoogleUserMessageAction.php b/src/Domains/Connectors/Google/Actions/GenerateGoogleUserMessageAction.php index f9d086246..8be147140 100644 --- a/src/Domains/Connectors/Google/Actions/GenerateGoogleUserMessageAction.php +++ b/src/Domains/Connectors/Google/Actions/GenerateGoogleUserMessageAction.php @@ -44,6 +44,7 @@ public function execute(int $pageSize = 350): int $query->where('is_liked', 0) ->where('is_disliked', 0) ->where('is_saved', 0) + ->where('is_purchased', 0) ->where('is_shared', 0); }) ->lockForUpdate() diff --git a/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php b/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php index 990f64074..033c96951 100644 --- a/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php +++ b/src/Domains/Connectors/InAppPurchase/Workflows/LinkMessageToOrderActivity.php @@ -10,6 +10,7 @@ use Kanvas\Social\Channels\DataTransferObject\Channel; use Kanvas\Social\Messages\Actions\CreateAppModuleMessageAction; use Kanvas\Social\Messages\Models\Message; +use Kanvas\Social\Messages\Services\MessageInteractionService; use Kanvas\Souk\Orders\Models\Order; use Kanvas\SystemModules\Repositories\SystemModulesRepository; use Kanvas\Users\Models\Users; @@ -61,6 +62,8 @@ public function execute(Model $order, AppInterface $app, array $params): array $purchaseChannel->addMessage($message, $user); $user->set('purchase_channel', $purchaseChannel->uuid); + $messageInteractionService = new MessageInteractionService($message); + $messageInteractionService->purchase($user); return [ 'order' => $order->id, diff --git a/src/Domains/Social/Interactions/Jobs/GenerateUserMessageJob.php b/src/Domains/Social/Interactions/Jobs/GenerateUserMessageJob.php index 07cf1f219..fb9eaa081 100644 --- a/src/Domains/Social/Interactions/Jobs/GenerateUserMessageJob.php +++ b/src/Domains/Social/Interactions/Jobs/GenerateUserMessageJob.php @@ -107,6 +107,7 @@ public function executeRandomMessages(int $pageSize = 350): int $query->where('is_liked', 0) ->where('is_disliked', 0) ->where('is_saved', 0) + ->where('is_purchased', 0) ->where('is_shared', 0); }) ->lockForUpdate() diff --git a/src/Domains/Social/Messages/Services/MessageInteractionService.php b/src/Domains/Social/Messages/Services/MessageInteractionService.php index 889e07dd4..315a7f7ff 100644 --- a/src/Domains/Social/Messages/Services/MessageInteractionService.php +++ b/src/Domains/Social/Messages/Services/MessageInteractionService.php @@ -7,7 +7,9 @@ use Baka\Users\Contracts\UserInterface; use Kanvas\Social\Enums\AppEnum; use Kanvas\Social\Enums\InteractionEnum; +use Kanvas\Social\Interactions\Actions\CreateInteraction; use Kanvas\Social\Interactions\Actions\CreateUserInteractionAction; +use Kanvas\Social\Interactions\DataTransferObject\Interaction; use Kanvas\Social\Interactions\DataTransferObject\UserInteraction; use Kanvas\Social\Interactions\Models\Interactions; use Kanvas\Social\Interactions\Models\UsersInteractions; @@ -83,6 +85,18 @@ public function dislike(UserInterface $who): UsersInteractions return $userInteraction; } + public function purchase(UserInterface $who): UsersInteractions + { + $this->incrementInteractionCount('total_purchased'); + + $userInteraction = $this->createInteraction($who, InteractionEnum::PURCHASE->getValue()); + $userMessage = $this->addToUserMessage($who); + $userMessage->is_purchased = 1; + $userMessage->saveOrFail(); + + return $userInteraction; + } + protected function incrementInteractionCount(string $interactionType): void { $this->message->$interactionType++; @@ -97,7 +111,14 @@ protected function decrementInteractionCount(string $interactionType): void protected function createInteraction(UserInterface $who, string $interactionType, ?string $note = null): UsersInteractions { - $interaction = Interactions::getByName($interactionType, $this->message->app); + //$interaction = Interactions::getByName($interactionType, $this->message->app); + $interaction = (new CreateInteraction( + new Interaction( + $interactionType, + $this->message->app, + $interactionType, + ) + ))->execute(); $createUserInteraction = new CreateUserInteractionAction( new UserInteraction( $who, From 593868bbc8af903002c26e22a9a027d302f21386 Mon Sep 17 00:00:00 2001 From: kaioken Date: Sat, 21 Dec 2024 10:12:25 -0400 Subject: [PATCH 6/9] feat: interactions --- .../Social/2024_12_21_140530_message_total_purchse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/Social/2024_12_21_140530_message_total_purchse.php b/database/migrations/Social/2024_12_21_140530_message_total_purchse.php index 6de931365..947b40568 100644 --- a/database/migrations/Social/2024_12_21_140530_message_total_purchse.php +++ b/database/migrations/Social/2024_12_21_140530_message_total_purchse.php @@ -5,7 +5,7 @@ use Illuminate\Support\Facades\Schema; return new class () extends Migration { - + public function up(): void { Schema::table('messages', function (Blueprint $table) { From 53cbd5503ade5a3883dc1790be2c5c622e9402a5 Mon Sep 17 00:00:00 2001 From: kaioken Date: Sat, 21 Dec 2024 10:12:55 -0400 Subject: [PATCH 7/9] feat: interactions --- .../Social/2024_12_21_140530_message_total_purchse.php | 1 - 1 file changed, 1 deletion(-) diff --git a/database/migrations/Social/2024_12_21_140530_message_total_purchse.php b/database/migrations/Social/2024_12_21_140530_message_total_purchse.php index 947b40568..eee0f5de4 100644 --- a/database/migrations/Social/2024_12_21_140530_message_total_purchse.php +++ b/database/migrations/Social/2024_12_21_140530_message_total_purchse.php @@ -5,7 +5,6 @@ use Illuminate\Support\Facades\Schema; return new class () extends Migration { - public function up(): void { Schema::table('messages', function (Blueprint $table) { From bda6a79781824e2b0bc04de6cd4abaa8568de6cd Mon Sep 17 00:00:00 2001 From: kaioken Date: Sat, 21 Dec 2024 10:54:55 -0400 Subject: [PATCH 8/9] filter by slug --- .../Builders/Messages/MessageBuilder.php | 24 +++++++++++++------ graphql/schemas/Social/message.graphql | 3 ++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/GraphQL/Social/Builders/Messages/MessageBuilder.php b/app/GraphQL/Social/Builders/Messages/MessageBuilder.php index a7895eb4a..d4d60e690 100644 --- a/app/GraphQL/Social/Builders/Messages/MessageBuilder.php +++ b/app/GraphQL/Social/Builders/Messages/MessageBuilder.php @@ -9,6 +9,7 @@ use GraphQL\Type\Definition\ResolveInfo; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\DB; +use InvalidArgumentException; use Kanvas\Apps\Models\Apps; use Kanvas\Social\Enums\AppEnum; use Kanvas\Social\Enums\InteractionEnum; @@ -43,6 +44,7 @@ public function getAll( //Check in this condition if the message is an item and if then check if it has been bought by the current user via status=completed on Order if (! $user->isAppOwner()) { $messages = Message::fromCompany($user->getCurrentCompany()); + return $messages; } @@ -78,13 +80,21 @@ public function getChannelMessages( GraphQLContext $context, ResolveInfo $resolveInfo ): Builder { - return Message::fromApp()->whereHas('channels', function ($query) use ($args) { - $query->where('channels.uuid', $args['channel_uuid']); - }) - ->when(! auth()->user()->isAdmin(), function ($query) { - $query->where('companies_id', auth()->user()->currentCompanyId()); - }) - ->select('messages.*'); + if (isset($args['channel_uuid']) && isset($args['channel_slug'])) { + throw new InvalidArgumentException('Provide only one of channel_uuid or channel_slug, not both.'); + } + + return Message::fromApp() + ->whereHas('channels', function ($query) use ($args) { + if (isset($args['channel_uuid'])) { + $query->where('channels.uuid', $args['channel_uuid']); + } elseif (isset($args['channel_slug'])) { + $query->where('channels.slug', $args['channel_slug']); + } + }) + ->when(! auth()->user()->isAdmin(), function ($query) { + $query->where('companies_id', auth()->user()->currentCompanyId()); + }); } public function getGroupByDate( diff --git a/graphql/schemas/Social/message.graphql b/graphql/schemas/Social/message.graphql index 984c23a03..1b441d76e 100644 --- a/graphql/schemas/Social/message.graphql +++ b/graphql/schemas/Social/message.graphql @@ -287,7 +287,8 @@ extend type Query @guard { ) channelMessages( - channel_uuid: String! + channel_uuid: String + channel_slug: String where: _ @whereConditions( columns: [ From 7e81efb1593288c288d537908110e15ea1addc5b Mon Sep 17 00:00:00 2001 From: kaioken Date: Sun, 22 Dec 2024 19:21:25 -0400 Subject: [PATCH 9/9] feat: order by --- graphql/schemas/Social/message.graphql | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/graphql/schemas/Social/message.graphql b/graphql/schemas/Social/message.graphql index 1b441d76e..fea5b1359 100644 --- a/graphql/schemas/Social/message.graphql +++ b/graphql/schemas/Social/message.graphql @@ -306,6 +306,20 @@ extend type Query @guard { "total_shared" ] ) + orderBy: _ + @orderBy( + columns: [ + "created_at" + "updated_at" + "id" + "total_view" + "total_liked" + "total_shared" + "total_saved" + "total_children" + "total_disliked" + ] + ) ): [Message!]! @paginate( defaultCount: 25