Skip to content

Commit

Permalink
Slc4 account name (#130)
Browse files Browse the repository at this point in the history
* docs: move setup guide to wiki

* fix: associate cost to user rather than product

* test: assert unknown uploads

* feature: notification when file is not understood
for #62

* tweak: don't notify for new audits

* wip: split editor UI

* ui: split editor ui

* wip: split classes

* wip: split percentage ui complete

* wip: split percentage ui complete

* test: update qa

* asset: plus minus icons

* feature: splits and profits in product table
closes #105
closes #101

* style: splits list + editor

* bug fix: profit sum

* style: splits list border and margin

* test: assertions for profit/balance

* test: test profit calculations

* ci: wip behat on github actions

* wip: authwave

* feature: login via authwave
closes #119

* feature: refactor upload manager

* build: use new releases on all repos

* test: improve tests

* feature: account name - new settings panel
closes #121

---------

Co-authored-by: rjbirkin <[email protected]>
  • Loading branch information
g105b and richardbirkin authored Nov 10, 2023
1 parent 44fdfd9 commit 78a693a
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 11 deletions.
19 changes: 19 additions & 0 deletions class/Auth/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace SHIFT\Trackshift\Auth;

class Settings {
/** @var array<string, string> */
private array $kvp = [];

public function set(string $key, string $value):void {
$this->kvp[$key] = $value;
}

public function get(string $key):?string {
return $this->kvp[$key] ?? null;
}

public function getKvp():array {
return $this->kvp;
}
}
28 changes: 26 additions & 2 deletions class/Auth/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,32 @@ public function getLatestNotificationCheckTime(User $user):?DateTimeInterface {
return $this->db->fetchDateTime("getLastNotificationCheckTime", $user->id);
}

public function getUserSettings(User $user):Settings {
$settings = new Settings();

foreach($this->db->fetchAll("getSettings", $user->id) as $row) {
$settings->set(
$row->getString("key"),
$row->getString("value"),
);
}

return $settings;
}

public function setUserSettings(User $user, Settings $settings):void {
$this->db->delete("removeUserSettings", $user->id);

foreach($settings->getKvp() as $key => $value) {
$this->db->insert("setUserSetting", [
"userId" => $user->id,
"key" => $key,
"value" => $value,
]);
}
}


private function rowToUser(?Row $row):?User {
if(!$row) {
return null;
Expand All @@ -80,6 +106,4 @@ public function associateAuthwave(User $user, AuthwaveUser $authwaveUser):void {
"authwaveId" => $authwaveUser->id,
]);
}


}
7 changes: 7 additions & 0 deletions class/ServiceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SHIFT\Spotify\SpotifyClient;
use SHIFT\Trackshift\Artist\ArtistRepository;
use SHIFT\Trackshift\Audit\AuditRepository;
use SHIFT\Trackshift\Auth\Settings;
use SHIFT\Trackshift\Auth\User;
use SHIFT\Trackshift\Auth\UserRepository;
use SHIFT\Trackshift\Content\ContentRepository;
Expand Down Expand Up @@ -126,4 +127,10 @@ public function loadAuthenticator():Authenticator {
$session->getStore(UserRepository::SESSION_AUTHENTICATOR_STORE_KEY, true),
);
}

public function loadSettings():Settings {
$userRepo = $this->container->get(UserRepository::class);
$user = $this->container->get(User::class);
return $userRepo->getUserSettings($user);
}
}
14 changes: 7 additions & 7 deletions class/Split/SplitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ public function __construct(
}

/** @return array<Split> */
public function getAll(User $user, bool $withRemainder = false):array {
public function getAll(User $user, ?string $remainderName = null):array {
$splitList = [];

$resultSet = $this->db->fetchAll("getAll", $user->id);
foreach($resultSet as $row) {
array_push(
$splitList,
$this->rowToSplit($row, withRemainderSplitPercentage: $withRemainder),
$this->rowToSplit($row, remainderName: $remainderName),
);
}

return $splitList;
}

/** @return array<SplitPercentage> */
public function getSplitPercentageList(User $user, string $splitId, bool $withRemainderSplitPercentage = false):array {
public function getSplitPercentageList(User $user, string $splitId, ?string $remainderName = null):array {
$resultSet = $this->db->fetchAll("getSplitPercentageList", $splitId, $user->id);

$splitPercentageList = [];
Expand All @@ -46,8 +46,8 @@ public function getSplitPercentageList(User $user, string $splitId, bool $withRe
);
}

if($withRemainderSplitPercentage) {
array_push($splitPercentageList, new RemainderSplitPercentage($splitPercentageList));
if($remainderName) {
array_push($splitPercentageList, new RemainderSplitPercentage($splitPercentageList, $remainderName));
}
return $splitPercentageList;
}
Expand Down Expand Up @@ -94,7 +94,7 @@ public function delete(string $splitId, User $user):void {
$this->db->delete("delete", $splitId, $user->id);
}

private function rowToSplit(?Row $row, ?User $user = null, bool $withRemainderSplitPercentage = false):?Split {
private function rowToSplit(?Row $row, ?User $user = null, ?string $remainderName = null):?Split {
if(!$row) {
return null;
}
Expand All @@ -103,7 +103,7 @@ private function rowToSplit(?Row $row, ?User $user = null, bool $withRemainderSp
$user = $user ?? $this->userRepository->getById($row->getString("userId"));
$product = $this->productRepository->getById($row->getString("productId"));

$splitPercentageList = $this->getSplitPercentageList($user, $id, $withRemainderSplitPercentage);
$splitPercentageList = $this->getSplitPercentageList($user, $id, $remainderName);

return new Split(
$id,
Expand Down
3 changes: 3 additions & 0 deletions page/_component/account-tabs.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
<li>
<a href="/account/splits/">Splits</a>
</li>
<li>
<a href="/account/settings/">Settings</a>
</li>
</ul>
10 changes: 10 additions & 0 deletions page/_component/user-settings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<form method="post">
<label>
<span>Account name</span>
<input name="account_name" data-bind:value="@name" />
</label>

<div class="actions">
<button name="do" value="save">Save</button>
</div>
</form>
2 changes: 2 additions & 0 deletions page/account/settings.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<account-tabs />
<user-settings />
25 changes: 25 additions & 0 deletions page/account/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
use Gt\Dom\HTMLDocument;
use Gt\DomTemplate\Binder;
use Gt\Input\Input;
use SHIFT\Trackshift\Auth\Settings;
use SHIFT\Trackshift\Auth\User;
use SHIFT\Trackshift\Auth\UserRepository;

function go(HTMLDocument $document, Binder $binder, Settings $settings):void {
$kvp = $settings->getKvp();

foreach($document->querySelectorAll("user-settings form input") as $input) {
if(isset($kvp[$input->name])) {
$binder->bindKeyValue($input->name, $kvp[$input->name]);
}
}
}

function do_save(Input $input, User $user, UserRepository $userRepository, Settings $settings):void {
foreach($input as $key => $value) {
$settings->set($key, $value);
}

$userRepository->setUserSettings($user, $settings);
}
4 changes: 3 additions & 1 deletion page/account/splits/@split.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Gt\Ulid\Ulid;
use SHIFT\Trackshift\Artist\ArtistRepository;
use SHIFT\Trackshift\Audit\AuditRepository;
use SHIFT\Trackshift\Auth\Settings;
use SHIFT\Trackshift\Auth\User;
use SHIFT\Trackshift\Product\ProductRepository;
use SHIFT\Trackshift\Split\EmptySplitPercentage;
Expand All @@ -24,6 +25,7 @@ function go(
ArtistRepository $artistRepository,
ProductRepository $productRepository,
SplitRepository $splitRepository,
Settings $settings,
User $user,
):void {
$artistId = $input->getString("artist");
Expand Down Expand Up @@ -66,7 +68,7 @@ function go(
$percentageList = $splitRepository->getSplitPercentageList($user, $id);
}
array_push($percentageList, new EmptySplitPercentage($productId));
array_push($percentageList, new RemainderSplitPercentage($percentageList));
array_push($percentageList, new RemainderSplitPercentage($percentageList, $settings->get("account_name") ?? "You"));

$binder->bindList(
$percentageList,
Expand Down
4 changes: 3 additions & 1 deletion page/account/splits/index.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
use Gt\DomTemplate\Binder;
use SHIFT\Trackshift\Auth\Settings;
use SHIFT\Trackshift\Auth\User;
use SHIFT\Trackshift\Split\SplitRepository;

function go(
SplitRepository $splitRepository,
User $user,
Settings $settings,
Binder $binder,
):void {
$splits = $splitRepository->getAll($user, true);
$splits = $splitRepository->getAll($user, $settings->get("account_name") ?? "You");
$binder->bindList($splits);
}
9 changes: 9 additions & 0 deletions query/User/getSettings.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
select
key,
value

from
Settings

where
userId = ?
3 changes: 3 additions & 0 deletions query/User/removeUserSettings.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
delete from Settings
where
userId = ?
10 changes: 10 additions & 0 deletions query/User/setUserSetting.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
insert into Settings (
userId,
key,
value
)
values (
:userId,
:key,
:value
)
6 changes: 6 additions & 0 deletions query/_migration/015-settings.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
create table Settings (
`userId` text not null references User(id) on update cascade on delete cascade,
key text not null,
value text,
primary key (userId, key)
);
8 changes: 8 additions & 0 deletions style/component/user-settings.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
user-settings {
@extend %o-container;

form {
@extend %d-form-fields;
margin-top: 4rem;
}
}
1 change: 1 addition & 0 deletions style/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
@import "component/upload-table";
@import "component/split-editor";
@import "component/splits-list";
@import "component/user-settings";

@import "page/index";
@import "page/_common";
Expand Down

0 comments on commit 78a693a

Please sign in to comment.