Implement super basic user sharing for budgets

Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
William Brawner 2020-03-21 14:09:14 -06:00
parent 3c4a5bd321
commit e4a3855d83
4 changed files with 258 additions and 211 deletions

View file

@ -88,15 +88,23 @@ class BudgetController extends Controller
$budget->setDescription($description);
$budget = $this->budgetMapper->insert($budget);
$userPermissions = [];
$users[$this->userId] = UserPermission::PERMISSION_MANAGE;
foreach ($users as $user => $permission) {
foreach ($users as $user) {
if ($user['user'] === $this->userId) {
continue;
}
$userPermission = new UserPermission();
$userPermission->setBudgetId($budget->getId());
$userPermission->setUserId($user);
$userPermission->setPermission($permission);
$userPermission->setUserId($user['user']);
$userPermission->setPermission($user['permission']);
$userPermission = $this->userPermissionMapper->insert($userPermission);
array_push($userPermissions, $userPermission);
}
$userPermission = new UserPermission();
$userPermission->setBudgetId($budget->getId());
$userPermission->setUserId($this->userId);
$userPermission->setPermission(UserPermission::PERMISSION_MANAGE);
array_push($userPermissions, $this->userPermissionMapper->insert($userPermission));
$budget->setUsers($userPermissions);
return new DataResponse($budget);
}
@ -121,31 +129,31 @@ class BudgetController extends Controller
if ($userPermission->getPermission() != UserPermission::PERMISSION_MANAGE) {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}
if ($name) {
$budget->setName($name);
}
if ($description) {
$budget->setDescription($description);
}
$budget = $this->budgetMapper->update($budget);
if ($users) {
$this->userPermissionMapper->deleteAll($budget->id);
$userPermissions = [];
$users[$this->userId] = UserPermission::PERMISSION_MANAGE;
foreach ($users as $user => $permission) {
foreach ($users as $user) {
if ($user['user'] === $this->userId) {
continue;
}
$userPermission = new UserPermission();
$userPermission->setBudgetId($budget->getId());
$userPermission->setUserId($user);
$userPermission->setPermission($permission);
$userPermission->setUserId($user['user']);
$userPermission->setPermission($user['permission']);
array_push($userPermissions, $this->userPermissionMapper->insert($userPermission));
}
$userPermission = new UserPermission();
$userPermission->setBudgetId($budget->getId());
$userPermission->setUserId($this->userId);
$userPermission->setPermission(UserPermission::PERMISSION_MANAGE);
array_push($userPermissions, $this->userPermissionMapper->insert($userPermission));
$budget->setUsers($userPermissions);
} else {
$budget->setUsers($userPermissionMapper->findAllByBudgetId($budget->getId()));
}
foreach ($users as $user => $permission) {
$this->logger->error("User: $user Permission: $permission");
}
return new DataResponse($budget);
}

View file

@ -4,6 +4,20 @@
<h2>{{ budget.id ? 'Edit' : 'Add' }} Budget</h2>
<input v-model="budget.name" type="text" placeholder="Name" title="Name" />
<textarea v-model="budget.description" placeholder="Description" title="Description"></textarea>
<div class="sharing">
<h3>Sharing</h3>
<input v-model="user" v-on:keyup.enter="addPermission()" type="test" placeholde="User" title="User" />
<ul v-if="budget.users" class="sharing-users">
<li v-for="user in budget.users">
<span v-if="user.user">
{{ user.user }}
</span>
<span v-if="user.permission">
: {{ user.permission }}
</span>
</li>
</ul>
</div>
<button @click="saveBudget()">Save Budget</button>
</div>
<div v-if="loading" class="icon-loading"></div>
@ -18,16 +32,26 @@ export default {
},
data: function() {
return {
saving: false
saving: false,
user: undefined
};
},
props: {
budget: Object
budget: Object,
},
computed: {
loading: state => state.budget === undefined || state.saving
},
methods: {
addPermission() {
const user = this.user
this.user = undefined;
this.budget.users = this.budget.users.filter(u => u.user != user)
this.budget.users.push({
"user": user,
"permission": 2
})
},
saveBudget() {
this.saving = true;
this.$store.dispatch("budgetFormSaveClicked", this.budget);

View file

@ -11,8 +11,10 @@ export default {
},
data: function() {
return {
budget: {}
};
budget: {
users: []
}
}
}
};
</script>

View file

@ -147,6 +147,14 @@ export default new Vuex.Store({
router.push({ name: "categoryDetails", params: { id: response.data.id } })
})
},
deleteCategoryClicked({ commit, state }, categoryId) {
axios.delete(OC.generateUrl(`/apps/twigs/api/v1.0/categories/${categoryId}`))
.then((response) => {
commit('setCurrentCategory', undefined)
commit('deleteCategory', response.data)
router.push({ name: "budgetDetails", params: { id: state.currentBudget } })
})
},
addTransactionClicked({ commit }) {
router.push({ name: "newTransaction" })
},
@ -250,6 +258,11 @@ export default new Vuex.Store({
[data.categoryId]: data.sum
}
},
deleteCategory(state, category) {
state.categories = [
...state.categories.filter(c => c.id !== category.id),
]
},
addTransaction(state, transaction) {
state.transactions = [
...state.transactions.filter(t => t.id !== transaction.id),