db

package
v0.0.0-...-449d51e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2024 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// user roles
	AdminRole   UserRole = "admin"
	ManagerRole UserRole = "manager"
	ViewerRole  UserRole = "viewer"
	// organization types
	AssemblyType               OrganizationType = "assembly"
	AssociationType            OrganizationType = "association"
	ChamberType                OrganizationType = "chamber"
	ReligiousType              OrganizationType = "religious"
	CityType                   OrganizationType = "city"
	CompanyType                OrganizationType = "company"
	CooperativeType            OrganizationType = "cooperative"
	PoliticalPartyType         OrganizationType = "political_party"
	EducationalInstitutionType OrganizationType = "educational"
	UnionType                  OrganizationType = "union"
	NonprofitType              OrganizationType = "nonprofit"
	CommunityType              OrganizationType = "community"
	ProfessionalCollegeType    OrganizationType = "professional_college"
	OthersType                 OrganizationType = "others"
	// verification code types
	CodeTypeVerifyAccount CodeType = "verify_account"
	CodeTypePasswordReset CodeType = "password_reset"
	CodeTypeOrgInvite     CodeType = "organization_invite"
)

Variables

View Source
var (
	ErrNotFound      = fmt.Errorf("not found")
	ErrInvalidData   = fmt.Errorf("invalid data provided")
	ErrAlreadyExists = fmt.Errorf("already exists")
)
View Source
var OrganizationTypesNames = map[OrganizationType]string{
	AssemblyType:               "Assembly",
	AssociationType:            "Association",
	ChamberType:                "Chamber",
	ReligiousType:              "Church / Religious Organization",
	CityType:                   "City / Municipality",
	CompanyType:                "Company / Corporation",
	CooperativeType:            "Cooperative",
	PoliticalPartyType:         "Political Party",
	EducationalInstitutionType: "University / Educational Institution",
	UnionType:                  "Union",
	NonprofitType:              "Nonprofit / NGO",
	CommunityType:              "Community Group",
	ProfessionalCollegeType:    "Professional College",
	OthersType:                 "Others",
}

OrganizationTypesNames is a map that contains the organization type names by type

View Source
var UserRolesNames = map[UserRole]string{
	AdminRole:   "Admin",
	ManagerRole: "Manager",
	ViewerRole:  "Viewer",
}

UserRoleNames is a map that contains the user role names by role

Functions

func HasWriteAccess

func HasWriteAccess(role UserRole) bool

HasWriteAccess function checks if the user role has write access

func IsOrganizationTypeValid

func IsOrganizationTypeValid(ot string) bool

IsOrganizationTypeValid function checks if the organization type is valid

func IsValidUserRole

func IsValidUserRole(role UserRole) bool

IsValidUserRole function checks if the user role is valid

Types

type CodeType

type CodeType string

type Features

type Features struct {
	Personalization bool `json:"personalization" bson:"personalization"`
	EmailReminder   bool `json:"emailReminder" bson:"emailReminder"`
	SmsNotification bool `json:"smsNotification" bson:"smsNotification"`
}

type MongoStorage

type MongoStorage struct {
	// contains filtered or unexported fields
}

MongoStorage uses an external MongoDB service for stoting the user data and election details.

func New

func New(url, database string, plans []*Plan) (*MongoStorage, error)

func (*MongoStorage) Close

func (ms *MongoStorage) Close()

func (*MongoStorage) CreateInvitation

func (ms *MongoStorage) CreateInvitation(invite *OrganizationInvite) error

CreateInvitation creates a new invitation for a user to join an organization.

func (*MongoStorage) DefaultPlan

func (ms *MongoStorage) DefaultPlan() (*Plan, error)

DefaultPlan method returns the default plan plan. If the plan doesn't exist, it returns the specific error.

func (*MongoStorage) DelOrganization

func (ms *MongoStorage) DelOrganization(org *Organization) error

DelOrganization method deletes the organization from the database. If an error occurs, it returns the error.

func (*MongoStorage) DelPlan

func (ms *MongoStorage) DelPlan(plan *Plan) error

DelPlan method deletes the plan with the given ID. If the plan doesn't exist, it returns the specific error.

func (*MongoStorage) DelUser

func (ms *MongoStorage) DelUser(user *User) error

DelUser method deletes the user from the database. If an error occurs, it returns the error.

func (*MongoStorage) DeleteInvitation

func (ms *MongoStorage) DeleteInvitation(invitationCode string) error

DeleteInvitation removes the invitation from the database.

func (*MongoStorage) Import

func (ms *MongoStorage) Import(jsonData []byte) error

Import imports a JSON dataset produced by String() into the database.

func (*MongoStorage) Invitation

func (ms *MongoStorage) Invitation(invitationCode string) (*OrganizationInvite, error)

Invitation returns the invitation for the given code.

func (*MongoStorage) IsMemberOf

func (ms *MongoStorage) IsMemberOf(userEmail, organizationAddress string, role UserRole) (bool, error)

IsMemberOf method checks if the user with the given email is a member of the organization with the given address and role. If the user is a member, it returns true. If the user is not a member, it returns false. If an error occurs, it returns the error.

func (*MongoStorage) Organization

func (ms *MongoStorage) Organization(address string, parent bool) (*Organization, *Organization, error)

Organization method returns the organization with the given address. If the parent flag is true, it also returns the parent organization if it exists. If the organization doesn't exist or the parent organization doesn't exist and it should be returned, it returns the specific error. If other errors occur, it returns the error.

func (*MongoStorage) OrganizationsMembers

func (ms *MongoStorage) OrganizationsMembers(address string) ([]User, error)

OrganizationsMembers method returns the users that are members of the organization with the given address. If an error occurs, it returns the error.

func (*MongoStorage) PendingInvitations

func (ms *MongoStorage) PendingInvitations(organizationAddress string) ([]OrganizationInvite, error)

PendingInvitations returns the pending invitations for the given organization.

func (*MongoStorage) Plan

func (ms *MongoStorage) Plan(planID uint64) (*Plan, error)

Plan method returns the plan with the given ID. If the plan doesn't exist, it returns the specific error.

func (*MongoStorage) PlanByStripeId

func (ms *MongoStorage) PlanByStripeId(stripeID string) (*Plan, error)

PlanByStripeId method returns the plan with the given stripe ID. If the plan doesn't exist, it returns the specific error.

func (*MongoStorage) Plans

func (ms *MongoStorage) Plans() ([]*Plan, error)

Plans method returns all plans from the database.

func (*MongoStorage) ReplaceCreatorEmail

func (ms *MongoStorage) ReplaceCreatorEmail(oldEmail, newEmail string) error

ReplaceCreatorEmail method replaces the creator email in the organizations where it is the creator. If an error occurs, it returns the error.

func (*MongoStorage) Reset

func (ms *MongoStorage) Reset() error

func (*MongoStorage) SetOrganization

func (ms *MongoStorage) SetOrganization(org *Organization) error

SetOrganization method creates or updates the organization in the database. If the organization already exists, it updates the fields that have changed. If the organization doesn't exist, it creates it. If an error occurs, it returns the error.

func (*MongoStorage) SetOrganizationSubscription

func (ms *MongoStorage) SetOrganizationSubscription(address string, orgSubscription *OrganizationSubscription) error

SetOrganizationSubscription method adds the provided subscription to the organization with the given address

func (*MongoStorage) SetPlan

func (ms *MongoStorage) SetPlan(plan *Plan) (uint64, error)

SetPlan method creates or updates the plan in the database. If the plan already exists, it updates the fields that have changed.

func (*MongoStorage) SetUser

func (ms *MongoStorage) SetUser(user *User) (uint64, error)

SetUser method creates or updates the user in the database. If the user already exists, it updates the fields that have changed. If the user doesn't exist, it creates it. If an error occurs, it returns the error.

func (*MongoStorage) SetVerificationCode

func (ms *MongoStorage) SetVerificationCode(user *User, code string, t CodeType, exp time.Time) error

SetVerificationCode method sets the verification code for the user provided. If the user already has a verification code, it updates it. If an error occurs, it returns the error.

func (*MongoStorage) String

func (ms *MongoStorage) String() string

func (*MongoStorage) User

func (ms *MongoStorage) User(id uint64) (*User, error)

User method returns the user with the given ID. If the user doesn't exist, it returns a specific error. If other errors occur, it returns the error.

func (*MongoStorage) UserByEmail

func (ms *MongoStorage) UserByEmail(email string) (*User, error)

UserByEmail method returns the user with the given email. If the user doesn't exist, it returns a specific error. If other errors occur, it returns the error.

func (*MongoStorage) UserByVerificationCode

func (ms *MongoStorage) UserByVerificationCode(code string, t CodeType) (*User, error)

UserByVerificationCode method returns the user with the given verification code. If the user or the verification code doesn't exist, it returns a specific error. If other errors occur, it returns the error. It checks the user verification code in the verifications collection and returns the user with the ID associated with the verification code.

func (*MongoStorage) UserVerificationCode

func (ms *MongoStorage) UserVerificationCode(user *User, t CodeType) (*UserVerification, error)

UserVerificationCode returns the verification code for the user provided. If the user has not a verification code, it returns an specific error, if other error occurs, it returns the error.

func (*MongoStorage) VerifyUserAccount

func (ms *MongoStorage) VerifyUserAccount(user *User) error

VerifyUserAccount method verifies the user provided, modifying the user to mark as verified and removing the verification code. If an error occurs, it returns the error.

type Options

type Options struct {
	MongoURL string
	Database string
}

type Organization

type Organization struct {
	Address         string                   `json:"address" bson:"_id"`
	Website         string                   `json:"website" bson:"website"`
	Type            OrganizationType         `json:"type" bson:"type"`
	Creator         string                   `json:"creator" bson:"creator"`
	CreatedAt       time.Time                `json:"createdAt" bson:"createdAt"`
	Nonce           string                   `json:"nonce" bson:"nonce"`
	Size            string                   `json:"size" bson:"size"`
	Color           string                   `json:"color" bson:"color"`
	Subdomain       string                   `json:"subdomain" bson:"subdomain"`
	Country         string                   `json:"country" bson:"country"`
	Timezone        string                   `json:"timezone" bson:"timezone"`
	Active          bool                     `json:"active" bson:"active"`
	TokensPurchased uint64                   `json:"tokensPurchased" bson:"tokensPurchased"`
	TokensRemaining uint64                   `json:"tokensRemaining" bson:"tokensRemaining"`
	Parent          string                   `json:"parent" bson:"parent"`
	Subscription    OrganizationSubscription `json:"subscription" bson:"subscription"`
	Counters        OrganizationCounters     `json:"counters" bson:"counters"`
}

type OrganizationCollection

type OrganizationCollection struct {
	Organizations []Organization `json:"organizations" bson:"organizations"`
}

type OrganizationCounters

type OrganizationCounters struct {
	SentSMS    int `json:"sentSMS" bson:"sentSMS"`
	SentEmails int `json:"sentEmails" bson:"sentEmails"`
	SubOrgs    int `json:"subOrgs" bson:"subOrgs"`
	Members    int `json:"members" bson:"members"`
}

type OrganizationInvite

type OrganizationInvite struct {
	InvitationCode      string    `json:"invitationCode" bson:"invitationCode"`
	OrganizationAddress string    `json:"organizationAddress" bson:"organizationAddress"`
	CurrentUserID       uint64    `json:"currentUserID" bson:"currentUserID"`
	NewUserEmail        string    `json:"newUserEmail" bson:"newUserEmail"`
	Role                UserRole  `json:"role" bson:"role"`
	Expiration          time.Time `json:"expiration" bson:"expiration"`
}

type OrganizationInvitesCollection

type OrganizationInvitesCollection struct {
	OrganizationInvites []OrganizationInvite `json:"organizationInvites" bson:"organizationInvites"`
}

type OrganizationMember

type OrganizationMember struct {
	Address string   `json:"address" bson:"_id"`
	Role    UserRole `json:"role" bson:"role"`
}

type OrganizationSubscription

type OrganizationSubscription struct {
	PlanID        uint64    `json:"planID" bson:"planID"`
	StartDate     time.Time `json:"startDate" bson:"startDate"`
	EndDate       time.Time `json:"endDate" bson:"endDate"`
	RenewalDate   time.Time `json:"renewalDate" bson:"renewalDate"`
	Active        bool      `json:"active" bson:"active"`
	MaxCensusSize int       `json:"maxCensusSize" bson:"maxCensusSize"`
	Email         string    `json:"email" bson:"email"`
}

type OrganizationType

type OrganizationType string

type Plan

type Plan struct {
	ID              uint64      `json:"id" bson:"_id"`
	Name            string      `json:"name" bson:"name"`
	StripeID        string      `json:"stripeID" bson:"stripeID"`
	StartingPrice   int64       `json:"startingPrice" bson:"startingPrice"`
	Default         bool        `json:"default" bson:"default"`
	Organization    PlanLimits  `json:"organization" bson:"organization"`
	VotingTypes     VotingTypes `json:"votingTypes" bson:"votingTypes"`
	Features        Features    `json:"features" bson:"features"`
	CensusSizeTiers []PlanTier  `json:"censusSizeTiers" bson:"censusSizeTiers"`
}

func ReadPlanJSON

func ReadPlanJSON() ([]*Plan, error)

readPlanJSON reads a JSON file with an array of subscritpions and return it as a Plan array

type PlanCollection

type PlanCollection struct {
	Plans []Plan `json:"plans" bson:"plans"`
}

type PlanLimits

type PlanLimits struct {
	Memberships int `json:"memberships" bson:"memberships"`
	SubOrgs     int `json:"subOrgs" bson:"subOrgs"`
	CensusSize  int `json:"censusSize" bson:"censusSize"`
}

type PlanTier

type PlanTier struct {
	Amount int64 `json:"amount" bson:"amount"`
	UpTo   int64 `json:"upTo" bson:"upTo"`
}

type User

type User struct {
	ID            uint64               `json:"id" bson:"_id"`
	Email         string               `json:"email" bson:"email"`
	Password      string               `json:"password" bson:"password"`
	FirstName     string               `json:"firstName" bson:"firstName"`
	LastName      string               `json:"lastName" bson:"lastName"`
	Organizations []OrganizationMember `json:"organizations" bson:"organizations"`
	Verified      bool                 `json:"verified" bson:"verified"`
}

func (*User) HasRoleFor

func (u *User) HasRoleFor(address string, role UserRole) bool

type UserCollection

type UserCollection struct {
	Users []User `json:"users" bson:"users"`
}

type UserRole

type UserRole string

type UserVerification

type UserVerification struct {
	ID         uint64    `json:"id" bson:"_id"`
	Code       string    `json:"code" bson:"code"`
	Type       CodeType  `json:"type" bson:"type"`
	Expiration time.Time `json:"expiration" bson:"expiration"`
}

type UserVerifications

type UserVerifications struct {
	Verifications []UserVerification `json:"verifications" bson:"verifications"`
}

type VotingTypes

type VotingTypes struct {
	Approval bool `json:"approval" bson:"approval"`
	Ranked   bool `json:"ranked" bson:"ranked"`
	Weighted bool `json:"weighted" bson:"weighted"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL