Skip to content

Commit

Permalink
Merge pull request #2713 from bakaphp/refact/user-purchase-channel
Browse files Browse the repository at this point in the history
feat: add message purchse to channel
  • Loading branch information
kaioken authored Dec 21, 2024
2 parents 8ffdcfb + bda6a79 commit ff88684
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 49 deletions.
28 changes: 16 additions & 12 deletions app/GraphQL/Social/Builders/Messages/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,11 +45,6 @@ public function getAll(
if (! $user->isAppOwner()) {
$messages = Message::fromCompany($user->getCurrentCompany());

/*
if ($viewingOneMessage) {
$messages->first()->isLocked();
}
*/
return $messages;
}

Expand Down Expand Up @@ -84,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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
}

Expand All @@ -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');
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
public function up(): void
{
Schema::table('messages', function (Blueprint $table) {
$table->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');
});
}
};
3 changes: 2 additions & 1 deletion graphql/schemas/Social/message.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ extend type Query @guard {
)

channelMessages(
channel_uuid: String!
channel_uuid: String
channel_slug: String
where: _
@whereConditions(
columns: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

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\Social\Messages\Services\MessageInteractionService;
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;

Expand All @@ -31,18 +35,41 @@ 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,
['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);

$user->set('purchase_channel', $purchaseChannel->uuid);
$messageInteractionService = new MessageInteractionService($message);
$messageInteractionService->purchase($user);

return [
'order' => $order->id,
'message' => $message->id,
'slug' => $message->slug,
'channel' => $purchaseChannel->id,
'channel_name' => $purchaseChannel->name,
];
}
}
13 changes: 12 additions & 1 deletion src/Domains/Social/Channels/Actions/CreateChannelAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Domains/Social/Channels/DataTransferObject/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '',
Expand Down
10 changes: 10 additions & 0 deletions src/Domains/Social/Channels/Models/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++;
Expand All @@ -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,
Expand Down
37 changes: 22 additions & 15 deletions src/Domains/Souk/Orders/Actions/CreateOrderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});
Expand Down
Loading

0 comments on commit ff88684

Please sign in to comment.