2024-12-09 17:46:13 +01:00
package store
import (
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
)
2024-12-09 20:53:46 +01:00
func ( str * Store ) AddAccountToOrganization ( acctId , orgId int , admin bool , trx * sqlx . Tx ) error {
stmt , err := trx . Prepare ( "insert into organization_members (account_id, organization_id, admin) values ($1, $2, $3)" )
2024-12-09 17:46:13 +01:00
if err != nil {
return errors . Wrap ( err , "error preparing organization_members insert statement" )
}
2024-12-09 20:53:46 +01:00
_ , err = stmt . Exec ( acctId , orgId , admin )
2024-12-09 17:46:13 +01:00
if err != nil {
return errors . Wrap ( err , "error executing organization_members insert statement" )
}
return nil
}
2024-12-09 22:42:30 +01:00
type OrganizationMember struct {
Email string
Admin bool
}
func ( str * Store ) FindAccountsForOrganization ( orgId int , trx * sqlx . Tx ) ( [ ] * OrganizationMember , error ) {
rows , err := trx . Queryx ( "select organization_members.admin, accounts.email from organization_members, accounts where organization_members.organization_id = $1 and organization_members.account_id = accounts.id" , orgId )
if err != nil {
return nil , errors . Wrap ( err , "error querying organization members" )
}
var members [ ] * OrganizationMember
for rows . Next ( ) {
om := & OrganizationMember { }
if err := rows . StructScan ( & om ) ; err != nil {
return nil , errors . Wrap ( err , "error scanning account email" )
}
members = append ( members , om )
}
return members , nil
2024-12-09 20:21:49 +01:00
}
2024-12-09 17:46:13 +01:00
func ( str * Store ) IsAccountInOrganization ( acctId , orgId int , trx * sqlx . Tx ) ( bool , error ) {
2024-12-10 17:46:01 +01:00
stmt , err := trx . Prepare ( "select count(0) from organization_members where account_id = $1 and organization_id = $2" )
2024-12-09 17:46:13 +01:00
if err != nil {
return false , errors . Wrap ( err , "error preparing organization_members count statement" )
}
var count int
if err := stmt . QueryRow ( acctId , orgId ) . Scan ( & count ) ; err != nil {
return false , errors . Wrap ( err , "error executing organization_members count statement" )
}
return count > 0 , nil
}
func ( str * Store ) IsAccountAdminOfOrganization ( acctId , orgId int , trx * sqlx . Tx ) ( bool , error ) {
2024-12-10 17:46:01 +01:00
stmt , err := trx . Prepare ( "select count(0) from organization_members where account_id = $1 and organization_id = $2 and admin" )
2024-12-09 17:46:13 +01:00
if err != nil {
return false , errors . Wrap ( err , "error preparing organization_members count statement" )
}
var count int
if err := stmt . QueryRow ( acctId , orgId ) . Scan ( & count ) ; err != nil {
return false , errors . Wrap ( err , "error executing organization_members count statement" )
}
return count > 0 , nil
}
func ( str * Store ) RemoveAccountFromOrganization ( acctId , orgId int , trx * sqlx . Tx ) error {
2024-12-09 21:04:43 +01:00
stmt , err := trx . Prepare ( "delete from organization_members where account_id = $1 and organization_id = $2" )
2024-12-09 17:46:13 +01:00
if err != nil {
return errors . Wrap ( err , "error preparing organization_members delete statement" )
}
_ , err = stmt . Exec ( acctId , orgId )
if err != nil {
return errors . Wrap ( err , "error executing organization_members delete statement" )
}
return nil
}