renamed a few items in the database schema

pull/59/head
Chris Veilleux 2019-03-01 20:33:35 -06:00
parent 0d6cb7d3f1
commit 581bf1812d
18 changed files with 71 additions and 42 deletions

View File

@ -3,14 +3,13 @@ from http import HTTPStatus
from flask import json
from selene.api import SeleneEndpoint, snake_to_camel
from selene.data.device import DeviceRepository
from selene.data.skill import SkillSettingRepository
from selene.util.db import get_db_connection
def _parse_selection_options(skill_settings):
for skill_setting in skill_settings:
for section in skill_setting.settings_definition['sections']:
for section in skill_setting.settings_display['sections']:
for field in section['fields']:
field_name = field.get('name')
if field_name is not None:

View File

@ -1,6 +1,6 @@
INSERT INTO
account.membership (type, rate, rate_period)
VALUES
('Monthly Supporter', 1.99, 'month'),
('Yearly Supporter', 19.99, 'year')
('Monthly Membership', 1.99, 'month'),
('Yearly Membership', 19.99, 'year')
;

View File

@ -1,10 +1,15 @@
CREATE TABLE account.account_membership (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
account_id uuid NOT NULL REFERENCES account.account ON DELETE CASCADE,
membership_id uuid NOT NULL REFERENCES account.membership,
membership_ts_range tsrange NOT NULL,
stripe_customer_id text NOT NULL,
insert_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
id uuid PRIMARY KEY
DEFAULT gen_random_uuid(),
account_id uuid NOT NULL
REFERENCES account.account ON DELETE CASCADE,
membership_id uuid NOT NULL
REFERENCES account.membership,
membership_ts_range tsrange NOT NULL,
payment_method payment_method_enum NOT NULL,
payment_account_id text NOT NULL,
insert_ts TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP,
EXCLUDE USING gist (account_id WITH =, membership_ts_range with &&),
UNIQUE (account_id, membership_id, membership_ts_range)
)

View File

@ -1,7 +1,10 @@
CREATE TABLE account.membership (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
type text NOT NULL UNIQUE,
rate NUMERIC NOT NULL,
rate_period text NOT NULL,
insert_ts TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
id uuid PRIMARY KEY
DEFAULT gen_random_uuid(),
type membership_type_enum NOT NULL
UNIQUE,
rate NUMERIC NOT NULL,
rate_period text NOT NULL,
insert_ts TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP
);

View File

@ -13,8 +13,8 @@ CREATE TABLE device.device_skill (
install_failure_reason text,
install_ts TIMESTAMP,
update_ts TIMESTAMP,
skill_setting_meta_id uuid
REFERENCES skill.setting_meta,
skill_settings_display_id uuid
REFERENCES skill.settings_display,
settings json,
insert_ts TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP,

View File

@ -0,0 +1,10 @@
CREATE TABLE skill.settings_display (
id uuid PRIMARY KEY
DEFAULT gen_random_uuid(),
skill_id uuid NOT NULL
REFERENCES skill.skill ON DELETE CASCADE,
settings_display jsonb NOT NULL,
insert_ts TIMESTAMP NOT NULL
DEFAULT CURRENT_TIMESTAMP,
UNIQUE (skill_id, settings_display)
);

View File

@ -0,0 +1 @@
CREATE TYPE membership_type_enum AS ENUM ('Monthly Membership', 'Yearly Membership');

View File

@ -0,0 +1 @@
CREATE TYPE payment_method_enum AS ENUM ('Stripe', 'PayPal');

View File

@ -19,8 +19,8 @@ from selene.data.account import (
from selene.util.db import get_db_connection
from ..base_endpoint import SeleneEndpoint
MONTHLY_MEMBERSHIP = 'Monthly Supporter'
YEARLY_MEMBERSHIP = 'Yearly Supporter'
MONTHLY_MEMBERSHIP = 'Monthly Membership'
YEARLY_MEMBERSHIP = 'Yearly Membership'
NO_MEMBERSHIP = 'Maybe Later'
@ -55,12 +55,15 @@ class Support(Model):
required=True,
choices=(MONTHLY_MEMBERSHIP, YEARLY_MEMBERSHIP, NO_MEMBERSHIP)
)
stripe_customer_id = StringType()
payment_method = StringType()
payment_account_id = StringType()
def validate_stripe_customer_id(self, data, value):
def validate_payment_account_id(self, data, value):
if data['membership'] != NO_MEMBERSHIP:
if not data['stripe_customer_id']:
raise ValidationError('Membership requires a stripe ID')
if not data['payment_account_id']:
raise ValidationError(
'Membership requires a payment account ID'
)
return value
@ -161,7 +164,8 @@ class AccountEndpoint(SeleneEndpoint):
support = Support(dict(
open_dataset=support_data.get('openDataset'),
membership=support_data.get('membership'),
stripe_customer_id=support_data.get('stripeCustomerId')
payment_method=support_data.get('paymentMethod'),
payment_account_id=support_data.get('paymentAccountId')
))
return support
@ -181,11 +185,12 @@ class AccountEndpoint(SeleneEndpoint):
membership_type = self.request_data['support']['membership']
membership = None
if membership_type != NO_MEMBERSHIP:
stripe_id = self.request_data['support']['stripeCustomerId']
payment_account = self.request_data['support']['paymentAccountId']
membership = AccountMembership(
type=membership_type,
start_date=date.today(),
stripe_customer_id=stripe_id
payment_method=self.request_data['support']['paymentMethod'],
payment_account_id=payment_account
)
account = Account(
email_address=email_address,

View File

@ -16,7 +16,8 @@ class AccountMembership(object):
"""Represents the subscription plan chosen by the user"""
type: str
start_date: date
stripe_customer_id: str
payment_method: str
payment_account_id: str
id: str = None

View File

@ -9,5 +9,5 @@ class AccountSkill(object):
devices: List[str]
display_name: str = None
settings_version: str = None
settings_meta: dict = None
settings_display: dict = None
settings: dict = None

View File

@ -79,7 +79,8 @@ class AccountRepository(object):
args=dict(
account_id=acct_id,
membership_type=membership.type,
stripe_customer_id=membership.stripe_customer_id
payment_method=membership.payment_method,
payment_account_id=membership.payment_account_id
)
)
self.cursor.insert(request)

View File

@ -3,7 +3,8 @@ INSERT INTO
account_id,
membership_id,
membership_ts_range,
stripe_customer_id
payment_method,
payment_account_id
)
VALUES
(
@ -17,5 +18,6 @@ VALUES
type = %(membership_type)s
),
'[now,]',
%(stripe_customer_id)s
%(payment_method)s,
%(payment_account_id)s
)

View File

@ -28,7 +28,8 @@ WITH
'id', am.id,
'type', m.type,
'start_date', lower(am.membership_ts_range)::DATE,
'stripe_customer_id', am.stripe_customer_id
'payment_method', am.payment_method,
'payment_account_id', am.payment_account_id
)
FROM
account.account_membership am

View File

@ -4,6 +4,6 @@ from typing import List
@dataclass
class AccountSkillSetting(object):
settings_definition: dict
settings_display: dict
settings_values: dict
devices: List[str]

View File

@ -22,10 +22,10 @@ class SkillSettingRepository(RepositoryBase):
skill_settings = []
for row in db_result:
settings_definition = row['settings_definition']['skillMetadata']
settings_display = row['settings_display']['skillMetadata']
skill_settings.append(
AccountSkillSetting(
settings_definition=settings_definition,
settings_display=settings_display,
settings_values=row['settings_values'],
devices=row['devices']
)

View File

@ -1,14 +1,14 @@
SELECT
sm.settings_meta::jsonb AS settings_definition,
sd.settings_display::jsonb AS settings_display,
ds.settings::jsonb AS settings_values,
array_agg(d.name) AS devices
FROM
skill.setting_meta sm
INNER JOIN device.device_skill ds ON sm.id = ds.skill_setting_meta_id
skill.settings_display sd
INNER JOIN device.device_skill ds ON sd.id = ds.skill_settings_display_id
INNER JOIN device.device d ON ds.device_id = d.id
WHERE
sm.skill_id = %(skill_id)s
sd.skill_id = %(skill_id)s
AND d.account_id = %(account_id)s
GROUP BY
sm.settings_meta::jsonb,
sd.settings_display::jsonb,
ds.settings::jsonb

View File

@ -2,10 +2,10 @@ WITH
setting_meta AS (
SELECT
s.id,
count(sm.id) > 0 AS has_settings
count(sd.id) > 0 AS has_settings
FROM
skill.skill s
LEFT JOIN skill.setting_meta sm ON s.id = sm.skill_id
LEFT JOIN skill.settings_display sd ON s.id = sd.skill_id
GROUP BY
s.id
),