Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/org bounty payment #642

Merged
merged 12 commits into from
Sep 13, 2023
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var JwtKey string
var RelayUrl string
var RelayAuthKey string
var InvoiceList = "INVOICELIST"
var BudgetInvoiceList = "BUDGETINVOICELIST"
kevkevinpal marked this conversation as resolved.
Show resolved Hide resolved

func InitConfig() {
Host = os.Getenv("LN_SERVER_BASE_URL")
Expand Down
80 changes: 79 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func (db database) GetListedPosts(r *http.Request) ([]PeopleExtra, error) {
return ms, result.Error
}

func (db database) GetBountiesCounty(personKey string, tabType string) int64 {
func (db database) GetBountiesCount(personKey string, tabType string) int64 {
var count int64

query := db.db.Model(&Bounty{})
Expand Down Expand Up @@ -559,6 +559,12 @@ func (db database) GetBountyByCreated(created uint) (Bounty, error) {
return b, err
}

func (db database) GetBounty(id uint) Bounty {
b := Bounty{}
db.db.Where("id", id).Find(&b)
return b
}

func (db database) UpdateBounty(b Bounty) (Bounty, error) {
db.db.Where("created", b.Created).Updates(&b)
return b, nil
Expand Down Expand Up @@ -1050,3 +1056,75 @@ func (db database) GetUserAssignedOrganizations(pubkey string) []OrganizationUse
db.db.Where("owner_pub_key = ?", pubkey).Find(&ms)
return ms
}

func (db database) AddBudgetHistory(budget BudgetHistory) BudgetHistory {
db.db.Create(&budget)
return budget
}

func (db database) CreateOrganizationBudget(budget BountyBudget) BountyBudget {
db.db.Create(&budget)
return budget
}
kevkevinpal marked this conversation as resolved.
Show resolved Hide resolved

func (db database) UpdateOrganizationBudget(budget BountyBudget) BountyBudget {
db.db.Where("organization = ?", budget.Organization).Updates(budget)
kevkevinpal marked this conversation as resolved.
Show resolved Hide resolved
return budget
}

func (db database) GetBudgetHistoryByCreated(created *time.Time, organization string) BudgetHistory {
ms := BudgetHistory{}
db.db.Where("created = ?", created).Where("organization = ? ", organization).Find(&ms)
return ms
}

func (db database) GetOrganizationBudget(organization string) BountyBudget {
ms := BountyBudget{}
db.db.Where("organization = ?", organization).Find(&ms)
return ms
}
kevkevinpal marked this conversation as resolved.
Show resolved Hide resolved

func (db database) AddAndUpdateBudget(budget BudgetStoreData) BudgetHistory {
db.db.Create(&budget)
created := budget.Created
organization := budget.Organization

budgetHistory := db.GetBudgetHistoryByCreated(created, organization)

if budgetHistory.Organization != "" && budgetHistory.Amount != 0 {
budgetHistory.Status = true
db.db.Where("created = ?", created).Where("organization = ? ", organization).Updates(budgetHistory)

// get organization budget and add payment to total budget
organizationBudget := db.GetOrganizationBudget(organization)

if organizationBudget.Organization == "" {
now := time.Now()
orgBudget := BountyBudget{
Organization: organization,
TotalBudget: budget.Amount,
Created: &now,
Updated: &now,
}
db.CreateOrganizationBudget(orgBudget)
} else {
totalBudget := organizationBudget.TotalBudget
organizationBudget.TotalBudget = totalBudget + budget.Amount
db.UpdateOrganizationBudget(organizationBudget)
}
}

return budgetHistory
}

func (db database) AddPaymentHistory(payment PaymentHistory) PaymentHistory {
db.db.Create(&payment)

// get organization budget and substract payment from total budget
organizationBudget := db.GetOrganizationBudget(payment.Organization)
totalBudget := organizationBudget.TotalBudget
organizationBudget.TotalBudget = totalBudget - payment.Amount
db.UpdateOrganizationBudget(organizationBudget)

return payment
}
15 changes: 15 additions & 0 deletions db/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ func (s StoreData) GetInvoiceCache() ([]InvoiceStoreData, error) {
return c, nil
}

func (s StoreData) SetBudgetInvoiceCache(value []BudgetStoreData) error {
// The invoice should expire every 6 minutes
s.Cache.Set(config.BudgetInvoiceList, value, 6*time.Minute)
return nil
}

func (s StoreData) GetBudgetInvoiceCache() ([]BudgetStoreData, error) {
value, found := s.Cache.Get(config.BudgetInvoiceList)
c, _ := value.([]BudgetStoreData)
if !found {
return []BudgetStoreData{}, errors.New("Budget Invoice Cache not found")
}
return c, nil
}

func (s StoreData) SetSocketConnections(value Client) error {
// The websocket in cache should not expire unless when deleted
s.Cache.Set(value.Host, value, cache.NoExpiration)
Expand Down
31 changes: 30 additions & 1 deletion db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,29 +433,58 @@ type UserRoles struct {
}

type BountyBudget struct {
ID uint `json:"id"`
Organization string `json:"organization"`
TotalBudget uint `json:"total_budget"`
Created *time.Time `json:"created"`
Updated *time.Time `json:"updated"`
}

type BudgetInvoiceRequest struct {
Amount uint `json:"amount"`
SenderPubKey string `json:"sender_pubkey"`
Organization string `json:"organization"`
Websocket_token string `json:"websocket_token,omitempty"`
}

type BudgetStoreData struct {
Amount uint `json:"amount"`
SenderPubKey string `json:"sender_pubkey"`
Organization string `json:"organization"`
Invoice string `json:"invoice"`
Host string `json:"host,omitempty"`
Created *time.Time `json:"created"`
}

type BudgetHistory struct {
ID uint `json:"id"`
Organization string `json:"organization"`
Amount uint `json:"amount"`
SenderPubKey string `json:"sender_pubkey"`
Created *time.Time `json:"created"`
Updated *time.Time `json:"updated"`
Status bool `json:"status"`
}

type PaymentHistory struct {
ID uint `json:"id"`
Organization string `json:"organization"`
SenderPubKey string `json:"sender_pubkey"`
ReceiverPubKey string `json:"receiver_pubkey"`
Amount uint `json:"amount"`
BountyId uint `json:"id"`
BountyId uint `json:"bounty_id"`
Created *time.Time `json:"created"`
}

type KeysendRequest struct {
Amount uint `json:"amount"`
Organization string `json:"organization"`
SenderPubKey string `json:"sender_pubkey"`
ReceiverPubKey string `json:"receiver_pubkey"`
BountyId uint `json:"bounty_id"`
Websocket_token string `json:"websocket_token,omitempty"`
}

func (Person) TableName() string {
return "people"
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/src/config/ModeDispatcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export enum AppMode {
}

const hosts: { [k: string]: AppMode } = {
'localhost:3000': AppMode.TRIBES,
'localhost:3005': AppMode.TRIBES,
'localhost:13000': AppMode.TRIBES,
'localhost:23000': AppMode.TRIBES,
'tribes.sphinx.chat': AppMode.TRIBES,
Expand Down
3 changes: 2 additions & 1 deletion frontend/app/src/config/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const SOCKET_MSG = {
invoice_success: 'invoice_success',
assign_success: 'assign_success',
lnauth_success: 'lnauth_success',
user_connect: 'user_connect'
user_connect: 'user_connect',
budget_success: 'budget_success'
};

let socket: WebSocket | null = null;
Expand Down
3 changes: 3 additions & 0 deletions frontend/app/src/people/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export interface WantedSummaryProps {
formSubmit: (any) => void;
title: string;
organization?: string;
id?: number;
}

export interface CodingBountiesProps {
Expand Down Expand Up @@ -294,6 +295,8 @@ export interface CodingBountiesProps {
extraModalFunction?: () => void;
commitment_fee?: number;
bounty_expires?: string;
organization?: string;
id?: number;
}

export interface CodingViewProps {
Expand Down
10 changes: 5 additions & 5 deletions frontend/app/src/people/main/FocusView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ const B = styled.div<BProps>`
overflow-y: auto;
box-sizing: border-box;
${EnvWithScrollBar({
thumbColor: '#5a606c',
trackBackgroundColor: 'rgba(0,0,0,0)'
})}
thumbColor: '#5a606c',
trackBackgroundColor: 'rgba(0,0,0,0)'
})}
`;
function FocusedView(props: FocusViewProps) {
const {
Expand Down Expand Up @@ -371,8 +371,8 @@ function FocusedView(props: FocusViewProps) {
extraHTML={
ui.meInfo.verification_signature
? {
twitter: `<span>Post this to your twitter account to verify:</span><br/><strong>Sphinx Verification: ${ui.meInfo.verification_signature}</strong>`
}
twitter: `<span>Post this to your twitter account to verify:</span><br/><strong>Sphinx Verification: ${ui.meInfo.verification_signature}</strong>`
}
: {}
}
/>
Expand Down
46 changes: 1 addition & 45 deletions frontend/app/src/people/utils/AssignBounty.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useEffect, useState } from 'react';
import styled from 'styled-components';
import { ConnectCardProps } from 'people/interfaces';
import { useStores } from 'store';
import { EuiGlobalToastList } from '@elastic/eui';
Expand All @@ -8,51 +7,8 @@ import { SOCKET_MSG, createSocketInstance } from 'config/socket';
import Invoice from '../widgetViews/summaries/wantedSummaries/Invoice';
import { colors } from '../../config/colors';
import { Button, Modal } from '../../components/common';
import { InvoiceInput, InvoiceLabel, InvoiceForm, B, N, ModalBottomText } from './style'

interface styledProps {
color?: any;
}

const B = styled.small`
font-weight: bold;
display: block;
margin-bottom: 10px;
`;
const N = styled.div<styledProps>`
font-family: Barlow;
font-style: normal;
font-weight: 500;
font-size: 17px;
line-height: 26px;
text-align: center;
margin-bottom: 10px;
color: ${(p: any) => p?.color && p?.color.grayish.G100};
`;
const ModalBottomText = styled.div<styledProps>`
position: absolute;
bottom: -36px;
width: 310;
background-color: transparent;
display: flex;
justify-content: center;
.bottomText {
margin-left: 12px;
color: ${(p: any) => p?.color && p?.color.pureWhite};
}
`;
const InvoiceForm = styled.div`
margin: 10px 0px;
text-align: left;
`;
const InvoiceLabel = styled.label`
font-size: 0.9rem;
font-weight: bold;
`;
const InvoiceInput = styled.input`
padding: 10px 20px;
border-radius: 10px;
border: 0.5px solid black;
`;
export default function AssignBounty(props: ConnectCardProps) {
const color = colors['light'];
const { person, created, visible } = props;
Expand Down
46 changes: 46 additions & 0 deletions frontend/app/src/people/utils/style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import styled from 'styled-components';

interface styledProps {
color?: any;
}

export const B = styled.small`
font-weight: bold;
display: block;
margin-bottom: 10px;
`;
export const N = styled.div<styledProps>`
font-family: Barlow;
font-style: normal;
font-weight: 500;
font-size: 17px;
line-height: 26px;
text-align: center;
margin-bottom: 10px;
color: ${(p: any) => p?.color && p?.color.grayish.G100};
`;
export const ModalBottomText = styled.div<styledProps>`
position: absolute;
bottom: -36px;
width: 310;
background-color: transparent;
display: flex;
justify-content: center;
.bottomText {
margin-left: 12px;
color: ${(p: any) => p?.color && p?.color.pureWhite};
}
`;
export const InvoiceForm = styled.div`
margin: 10px 0px;
text-align: left;
`;
export const InvoiceLabel = styled.label`
font-size: 0.9rem;
font-weight: bold;
`;
export const InvoiceInput = styled.input`
padding: 10px 20px;
border-radius: 10px;
border: 0.5px solid black;
`;
Loading
Loading