Skip to content

Commit

Permalink
Search orders by name (#73)
Browse files Browse the repository at this point in the history
* Add option to search by account name or nickname to /transactions

* Add search bar to /comptoir/c/transactions page

* Increase pagination buttons size in Transactions component

* Reset page number when changing searched name

* Add account name on transaction popup
  • Loading branch information
aripot007 committed Mar 17, 2024
1 parent 2dea5f7 commit 7b87f90
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 32 deletions.
9 changes: 7 additions & 2 deletions backend/api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ func (s *Server) GetTransactions(c echo.Context, params autogen.GetTransactionsP
state = string(*params.State)
}

count, err := s.DBackend.CountAllTransactions(c.Request().Context(), state)
var name string
if params.Name != nil {
name = string(*params.Name)
}

count, err := s.DBackend.CountAllTransactions(c.Request().Context(), state, name)
if err != nil {
logrus.Error(err)
return Error500(c)
Expand All @@ -156,7 +161,7 @@ func (s *Server) GetTransactions(c echo.Context, params autogen.GetTransactionsP
// Make sure the last page is not empty
dbpage, page, limit, maxPage := autogen.Pager(params.Page, params.Limit, &count)

data, err := s.DBackend.GetAllTransactions(c.Request().Context(), dbpage, limit, state)
data, err := s.DBackend.GetAllTransactions(c.Request().Context(), dbpage, limit, state, name)
if err != nil {
logrus.Error(err)
return Error500(c)
Expand Down
22 changes: 16 additions & 6 deletions backend/autogen/bar.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions backend/internal/db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ type DBackend interface {
GetTransactions(ctx context.Context, account string, page uint64, size uint64, state string) ([]*models.Transaction, error)
CountTransactions(ctx context.Context, account string, state string) (uint64, error)

GetAllTransactions(ctx context.Context, page uint64, size uint64, state string) ([]*models.Transaction, error)
GetAllTransactions(ctx context.Context, page uint64, size uint64, state string, name string) ([]*models.Transaction, error)
CountAllTransactions(ctx context.Context, state string, name string) (uint64, error)
GetAllActiveTransactionsItems(ctx context.Context, name string) ([]autogen.TransactionItem, error)
CountAllTransactions(ctx context.Context, state string) (uint64, error)

// Restock's CRUD
CreateRestock(ctx context.Context, t *models.Restock) error
Expand Down
38 changes: 36 additions & 2 deletions backend/internal/db/mongo/transaction_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (b *Backend) CountTransactions(ctx context.Context, accountID string, state
return uint64(count), nil
}

func (b *Backend) GetAllTransactions(ctx context.Context, page uint64, size uint64, state string) ([]*models.Transaction, error) {
func (b *Backend) GetAllTransactions(ctx context.Context, page uint64, size uint64, state string, name string) ([]*models.Transaction, error) {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()

Expand All @@ -66,6 +66,23 @@ func (b *Backend) GetAllTransactions(ctx context.Context, page uint64, size uint
filter["state"] = state
}

if name != "" {
filter["$or"] = []bson.M{
{
"account_name": bson.M{
"$regex": name,
"$options": "i",
},
},
{
"account_nick_name": bson.M{
"$regex": name,
"$options": "i",
},
},
}
}

// Get "size" transactions from "page" using aggregation
var transactions []*models.Transaction
cursor, err := b.db.Collection(TransactionsCollection).Find(ctx, filter, options.Find().SetSkip(int64(page*size)).SetLimit(int64(size)).SetSort(bson.M{"created_at": -1}))
Expand All @@ -81,7 +98,7 @@ func (b *Backend) GetAllTransactions(ctx context.Context, page uint64, size uint
return transactions, nil
}

func (b *Backend) CountAllTransactions(ctx context.Context, state string) (uint64, error) {
func (b *Backend) CountAllTransactions(ctx context.Context, state string, name string) (uint64, error) {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()

Expand All @@ -91,6 +108,23 @@ func (b *Backend) CountAllTransactions(ctx context.Context, state string) (uint6
filter["state"] = state
}

if name != "" {
filter["$or"] = []bson.M{
{
"account_name": bson.M{
"$regex": name,
"$options": "i",
},
},
{
"account_nick_name": bson.M{
"$regex": name,
"$options": "i",
},
},
}
}

count, err := b.db.Collection(TransactionsCollection).CountDocuments(ctx, filter)
if err != nil {
return 0, err
Expand Down
6 changes: 6 additions & 0 deletions bar.openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,12 @@ paths:
schema:
type: string
$ref: "#/components/schemas/TransactionState"
- name: name
in: query
description: Filter by account name
required: false
schema:
type: string
responses:
"200":
description: ""
Expand Down
22 changes: 15 additions & 7 deletions frontend/src/lib/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8111,10 +8111,11 @@ export const TransactionsApiAxiosParamCreator = function (configuration?: Config
* @param {number} [page] Page number
* @param {number} [limit] Number of transactions per page
* @param {TransactionState} [state] Filter by state
* @param {string} [name] Filter by account name
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getTransactions: async (page?: number, limit?: number, state?: TransactionState, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
getTransactions: async (page?: number, limit?: number, state?: TransactionState, name?: string, options: AxiosRequestConfig = {}): Promise<RequestArgs> => {
const localVarPath = `/transactions`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
Expand All @@ -8141,6 +8142,10 @@ export const TransactionsApiAxiosParamCreator = function (configuration?: Config
localVarQueryParameter['state'] = state;
}

if (name !== undefined) {
localVarQueryParameter['name'] = name;
}



setSearchParams(localVarUrlObj, localVarQueryParameter);
Expand Down Expand Up @@ -8424,11 +8429,12 @@ export const TransactionsApiFp = function(configuration?: Configuration) {
* @param {number} [page] Page number
* @param {number} [limit] Number of transactions per page
* @param {TransactionState} [state] Filter by state
* @param {string} [name] Filter by account name
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async getTransactions(page?: number, limit?: number, state?: TransactionState, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<GetTransactions200Response>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.getTransactions(page, limit, state, options);
async getTransactions(page?: number, limit?: number, state?: TransactionState, name?: string, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<GetTransactions200Response>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.getTransactions(page, limit, state, name, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
Expand Down Expand Up @@ -8544,11 +8550,12 @@ export const TransactionsApiFactory = function (configuration?: Configuration, b
* @param {number} [page] Page number
* @param {number} [limit] Number of transactions per page
* @param {TransactionState} [state] Filter by state
* @param {string} [name] Filter by account name
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
getTransactions(page?: number, limit?: number, state?: TransactionState, options?: any): AxiosPromise<GetTransactions200Response> {
return localVarFp.getTransactions(page, limit, state, options).then((request) => request(axios, basePath));
getTransactions(page?: number, limit?: number, state?: TransactionState, name?: string, options?: any): AxiosPromise<GetTransactions200Response> {
return localVarFp.getTransactions(page, limit, state, name, options).then((request) => request(axios, basePath));
},
/**
* Get all items in active transactions (ordered items)
Expand Down Expand Up @@ -8664,12 +8671,13 @@ export class TransactionsApi extends BaseAPI {
* @param {number} [page] Page number
* @param {number} [limit] Number of transactions per page
* @param {TransactionState} [state] Filter by state
* @param {string} [name] Filter by account name
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof TransactionsApi
*/
public getTransactions(page?: number, limit?: number, state?: TransactionState, options?: AxiosRequestConfig) {
return TransactionsApiFp(this.configuration).getTransactions(page, limit, state, options).then((request) => request(this.axios, this.basePath));
public getTransactions(page?: number, limit?: number, state?: TransactionState, name?: string, options?: AxiosRequestConfig) {
return TransactionsApiFp(this.configuration).getTransactions(page, limit, state, name, options).then((request) => request(this.axios, this.basePath));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
- Close the popup
-->
<div class="w-2/3 bg-white rounded-xl z-20 text-black">
<h1 class="p-3 w-full text-center text-xl">Commande de : <span class="font-semibold">{transaction.account_name}</span></h1>
<div class="p-5 h-full pr-4 w-full">
<div class="grid grid-cols-6 gap-2">
{#each newTransaction.items as item, i}
Expand Down
35 changes: 24 additions & 11 deletions frontend/src/lib/components/comptoir/transactions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
let transactions: Array<Transaction> = [];
let maxItemPerTransaction: number = 6;
let interval: number;
let searchName: string | undefined;
let page: number = 0;
let maxPage: number = 0;
Expand Down Expand Up @@ -45,7 +46,7 @@
function reloadTransactions() {
transactionsApi()
.getTransactions(page, amount, st, { withCredentials: true })
.getTransactions(page, amount, st, searchName, { withCredentials: true })
.then((res) => {
page = res.data.page ?? 0;
maxPage = res.data.max_page ?? 0;
Expand Down Expand Up @@ -83,11 +84,23 @@
{/if}

<!-- Good looking dropdown for transaction -->
<div class="w-full">
<div class="flex flex-col">
<div class="flex flex-row justify-between">
<div class="text-lg font-semibold">Transactions</div>
<div class="text-lg font-semibold">
<div class="w-full h-full flex flex-col">
<div class="flex flex-col flex-grow flex-1">
<div class="flex flex-row items-center mt-2">
<div class="flex flex-row items-center space-x-10 grow">
<div class="text-lg font-semibold">Transactions</div>
<input
class="rounded-lg p-2 text-black"
placeholder="Rechercher une personne"
on:input={(e) => {
// @ts-ignore
searchName = e.target.value.toLowerCase();
page = 1;
reloadTransactions();
}}
/>
</div>
<div class="text-lg font-semibold grow">
Filtre :
<button
class="bg-gray-700 p-2 rounded-lg"
Expand All @@ -107,10 +120,10 @@
{/if}
</button>
</div>
<div class="text-lg font-semibold">Montant</div>
<div class="text-lg font-semibold grow text-end">Montant</div>
</div>
<!-- show clearly if there's a scrollbar -->
<div use:dragscroll class="flex flex-col max-h-[80vh] overflow-auto">
<div use:dragscroll class="flex flex-col overflow-auto">
{#each transactions as transaction}
<button
on:click={() => (displayTransaction = transaction)}
Expand Down Expand Up @@ -153,11 +166,11 @@

<!-- Pagination -->
<div class="flex flex-row justify-center mt-5">
<button class="bg-blue-700 p-2 rounded-xl hover:bg-blue-900 transition-all" on:click={prevPage}
<button class="bg-blue-700 p-4 rounded-xl hover:bg-blue-900 transition-all text-2xl" on:click={prevPage}
>&lt;</button
>
<div class="text-lg font-semibold self-center mx-2">{page}/{maxPage}</div>
<button class="bg-blue-700 p-2 rounded-xl hover:bg-blue-900 transition-all" on:click={nextPage}
<div class="text-2xl font-semibold self-center mx-2">{page}/{maxPage}</div>
<button class="bg-blue-700 p-4 rounded-xl hover:bg-blue-900 transition-all text-2xl" on:click={nextPage}
>&gt;</button
>
</div>
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/routes/comptoir/c/transactions/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
{#if changePassword}
<ChangePassword onEnd={() => (changePassword = false)} />
{:else}
<div class="gap-16 p-5 w-full h-full text-white">
<div class="p-5 w-full h-full text-white flex flex-col">
<div class="flex flex-row justify-between gap-16 p-2 w-full text-white">
<div class="flex flex-row">
<button
Expand Down Expand Up @@ -164,7 +164,9 @@
{#if showTransactionItems}
<TransactionsItems />
{:else}
<Transactions amount={6} />
<div class="flex-1">
<Transactions amount={6} />
</div>
{/if}

{#if newRefill}
Expand Down

0 comments on commit 7b87f90

Please sign in to comment.