Skip to content

Commit

Permalink
(Improve order flow) Refactor validation services (#4024) (minor)
Browse files Browse the repository at this point in the history
### Changed

- Use the same OrderValidationService for all order types
  • Loading branch information
islean authored Dec 16, 2024
1 parent cbeb28c commit 0b0b273
Show file tree
Hide file tree
Showing 42 changed files with 379 additions and 1,057 deletions.
15 changes: 8 additions & 7 deletions cg/meta/orders/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from cg.services.order_validation_service.models.order import Order
from cg.services.orders.submitters.order_submitter import OrderSubmitter
from cg.services.orders.submitters.order_submitter_registry import OrderSubmitterRegistry
from cg.store.models import User
from cg.store.store import Store

LOG = logging.getLogger(__name__)
Expand All @@ -36,17 +37,17 @@ def __init__(
self.ticket_handler = ticket_handler
self.submitter_registry = submitter_registry

def submit(
self,
order_type: OrderType,
raw_order: dict,
) -> dict:
def submit(self, order_type: OrderType, raw_order: dict, user: User) -> dict:
"""Submit a batch of samples.
Main entry point for the class towards interfaces that implements it.
"""
submit_handler: OrderSubmitter = self.submitter_registry.get_order_submitter(order_type)
order: Order = submit_handler.order_validation_service.parse_and_validate(raw_order)
ticket_number: str | None = self.ticket_handler.parse_ticket_number(order.name)
order: Order = submit_handler.order_validation_service.parse_and_validate(
raw_order=raw_order, order_type=order_type
)
ticket_number: int = self.ticket_handler.create_ticket(
order=order, user_name=user.name, user_mail=user.email, order_type=order_type
)
order.ticket_number = ticket_number
return submit_handler.submit_order(order)
47 changes: 5 additions & 42 deletions cg/server/endpoints/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,13 @@
from cg.server.dto.orders.orders_response import Order, OrdersResponse
from cg.server.endpoints.utils import before_request
from cg.server.ext import (
balsamic_umi_validation_service,
balsamic_validation_service,
db,
delivery_message_service,
fastq_validation_service,
lims,
metagenome_validation_service,
microbial_fastq_validation_service,
microsalt_validation_service,
mip_dna_validation_service,
mutant_validation_service,
order_service,
order_submitter_registry,
pacbio_long_read_validation_service,
rml_validation_service,
rna_fusion_validation_service,
taxprofiler_validation_service,
order_validation_service,
ticket_handler,
tomte_validation_service,
)
from cg.store.models import Application, Customer

Expand Down Expand Up @@ -184,8 +172,7 @@ def submit_order(order_type: OrderType):
result: dict = api.submit(
raw_order=request_json,
order_type=order_type,
user_name=g.current_user.name,
user_mail=g.current_user.email,
user=g.current_user,
)

except ( # user misbehaviour
Expand Down Expand Up @@ -272,31 +259,7 @@ def validate_order(order_type: OrderType):
raw_order = request.get_json()
raw_order["project_type"] = order_type
raw_order["user_id"] = g.current_user.id
response = {}
if order_type == OrderType.BALSAMIC:
response = balsamic_validation_service.validate(raw_order)
elif order_type == OrderType.BALSAMIC_UMI:
response = balsamic_umi_validation_service.validate(raw_order)
elif order_type == OrderType.FASTQ:
response = fastq_validation_service.validate(raw_order)
elif order_type == OrderType.METAGENOME:
response = metagenome_validation_service.validate(raw_order)
elif order_type == OrderType.MICROBIAL_FASTQ:
response = microbial_fastq_validation_service.validate(raw_order)
elif order_type == OrderType.MICROSALT:
response = microsalt_validation_service.validate(raw_order)
elif order_type == OrderType.MIP_DNA:
response = mip_dna_validation_service.validate(raw_order)
elif order_type == OrderType.PACBIO_LONG_READ:
response = pacbio_long_read_validation_service.validate(raw_order)
elif order_type == OrderType.SARS_COV_2:
response = mutant_validation_service.validate(raw_order)
elif order_type == OrderType.RML:
response = rml_validation_service.validate(raw_order)
elif order_type == OrderType.RNAFUSION:
response = rna_fusion_validation_service.validate(raw_order)
elif order_type == OrderType.TAXPROFILER:
response = taxprofiler_validation_service.validate(raw_order)
elif order_type == OrderType.TOMTE:
response = tomte_validation_service.validate(raw_order)
response = order_validation_service.get_validation_response(
raw_order=raw_order, order_type=order_type
)
return jsonify(response), HTTPStatus.OK
55 changes: 2 additions & 53 deletions cg/server/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,7 @@
from cg.server.app_config import app_config
from cg.services.application.service import ApplicationsWebService
from cg.services.delivery_message.delivery_message_service import DeliveryMessageService
from cg.services.order_validation_service.workflows.balsamic.validation_service import (
BalsamicValidationService,
)
from cg.services.order_validation_service.workflows.balsamic_umi.validation_service import (
BalsamicUmiValidationService,
)
from cg.services.order_validation_service.workflows.fastq.validation_service import (
FastqValidationService,
)
from cg.services.order_validation_service.workflows.metagenome.validation_service import (
MetagenomeValidationService,
)
from cg.services.order_validation_service.workflows.microbial_fastq.validation_service import (
MicrobialFastqValidationService,
)
from cg.services.order_validation_service.workflows.microsalt.validation_service import (
MicroSaltValidationService,
)
from cg.services.order_validation_service.workflows.mip_dna.validation_service import (
MipDnaValidationService,
)
from cg.services.order_validation_service.workflows.mutant.validation_service import (
MutantValidationService,
)
from cg.services.order_validation_service.workflows.pacbio_long_read.validation_service import (
PacbioLongReadValidationService,
)
from cg.services.order_validation_service.workflows.rml.validation_service import (
RmlValidationService,
)
from cg.services.order_validation_service.workflows.rna_fusion.validation_service import (
RnaFusionValidationService,
)
from cg.services.order_validation_service.workflows.taxprofiler.validation_service import (
TaxprofilerValidationService,
)
from cg.services.order_validation_service.workflows.tomte.validation_service import (
TomteValidationService,
)
from cg.services.order_validation_service.order_validation_service import OrderValidationService
from cg.services.orders.order_service.order_service import OrderService
from cg.services.orders.order_summary_service.order_summary_service import OrderSummaryService
from cg.services.orders.submitters.order_submitter_registry import (
Expand Down Expand Up @@ -136,20 +98,7 @@ def init_app(self, app):
status_db=db,
)

balsamic_validation_service = BalsamicValidationService(store=db)
balsamic_umi_validation_service = BalsamicUmiValidationService(store=db)
fastq_validation_service = FastqValidationService(store=db)
microbial_fastq_validation_service = MicrobialFastqValidationService(store=db)
microsalt_validation_service = MicroSaltValidationService(store=db)
mip_dna_validation_service = MipDnaValidationService(store=db)
metagenome_validation_service = MetagenomeValidationService(store=db)
mutant_validation_service = MutantValidationService(store=db)
pacbio_long_read_validation_service = PacbioLongReadValidationService(store=db)
rml_validation_service = RmlValidationService(store=db)
rna_fusion_validation_service = RnaFusionValidationService(store=db)
taxprofiler_validation_service = TaxprofilerValidationService(store=db)
tomte_validation_service = TomteValidationService(store=db)

order_validation_service = OrderValidationService(store=db)
freshdesk_client = FreshdeskClient(
base_url=app_config.freshdesk_url, api_key=app_config.freshdesk_api_key
)
Expand Down
151 changes: 151 additions & 0 deletions cg/services/order_validation_service/order_type_maps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
from pydantic import BaseModel, ConfigDict

from cg.models.orders.constants import OrderType
from cg.services.order_validation_service.models.order import Order
from cg.services.order_validation_service.workflows.balsamic.models.order import BalsamicOrder
from cg.services.order_validation_service.workflows.balsamic.validation_rules import (
BALSAMIC_CASE_RULES,
BALSAMIC_CASE_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.balsamic_umi.models.order import (
BalsamicUmiOrder,
)
from cg.services.order_validation_service.workflows.balsamic_umi.validation_rules import (
BALSAMIC_UMI_CASE_RULES,
BALSAMIC_UMI_CASE_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.fastq.models.order import FastqOrder
from cg.services.order_validation_service.workflows.fastq.validation_rules import FASTQ_SAMPLE_RULES
from cg.services.order_validation_service.workflows.fluffy.models.order import FluffyOrder
from cg.services.order_validation_service.workflows.fluffy.validation_rules import (
FLUFFY_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.metagenome.models.order import MetagenomeOrder
from cg.services.order_validation_service.workflows.metagenome.validation_rules import (
METAGENOME_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.microbial_fastq.models.order import (
MicrobialFastqOrder,
)
from cg.services.order_validation_service.workflows.microbial_fastq.validation_rules import (
MICROBIAL_FASTQ_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.microsalt.models.order import MicrosaltOrder
from cg.services.order_validation_service.workflows.microsalt.validation_rules import (
MICROSALT_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.mip_dna.models.order import MipDnaOrder
from cg.services.order_validation_service.workflows.mip_dna.validation_rules import (
MIP_DNA_CASE_RULES,
MIP_DNA_CASE_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.mip_rna.models.order import MipRnaOrder
from cg.services.order_validation_service.workflows.mip_rna.validation_rules import (
MIP_RNA_CASE_RULES,
MIP_RNA_CASE_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.mutant.models.order import MutantOrder
from cg.services.order_validation_service.workflows.mutant.validation_rules import (
MUTANT_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.order_validation_rules import ORDER_RULES
from cg.services.order_validation_service.workflows.pacbio_long_read.models.order import PacbioOrder
from cg.services.order_validation_service.workflows.pacbio_long_read.validation_rules import (
PACBIO_LONG_READ_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.rml.models.order import RmlOrder
from cg.services.order_validation_service.workflows.rml.validation_rules import RML_SAMPLE_RULES
from cg.services.order_validation_service.workflows.rna_fusion.models.order import RnaFusionOrder
from cg.services.order_validation_service.workflows.rna_fusion.validation_rules import (
RNAFUSION_CASE_RULES,
RNAFUSION_CASE_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.taxprofiler.models.order import TaxprofilerOrder
from cg.services.order_validation_service.workflows.taxprofiler.validation_rules import (
TAXPROFILER_SAMPLE_RULES,
)
from cg.services.order_validation_service.workflows.tomte.models.order import TomteOrder
from cg.services.order_validation_service.workflows.tomte.validation_rules import (
TOMTE_CASE_RULES,
TOMTE_CASE_SAMPLE_RULES,
)


class RuleSet(BaseModel):
case_rules: list[callable] = []
case_sample_rules: list[callable] = []
order_rules: list[callable] = ORDER_RULES
sample_rules: list[callable] = []

model_config = ConfigDict(arbitrary_types_allowed=True)


ORDER_TYPE_RULE_SET_MAP: dict[OrderType, RuleSet] = {
OrderType.BALSAMIC: RuleSet(
case_rules=BALSAMIC_CASE_RULES, case_sample_rules=BALSAMIC_CASE_SAMPLE_RULES
),
OrderType.BALSAMIC_UMI: RuleSet(
case_rules=BALSAMIC_UMI_CASE_RULES,
case_sample_rules=BALSAMIC_UMI_CASE_SAMPLE_RULES,
),
OrderType.FASTQ: RuleSet(
sample_rules=FASTQ_SAMPLE_RULES,
),
OrderType.FLUFFY: RuleSet(
sample_rules=FLUFFY_SAMPLE_RULES,
),
OrderType.METAGENOME: RuleSet(
sample_rules=METAGENOME_SAMPLE_RULES,
),
OrderType.MICROBIAL_FASTQ: RuleSet(
sample_rules=MICROBIAL_FASTQ_SAMPLE_RULES,
),
OrderType.MICROSALT: RuleSet(
sample_rules=MICROSALT_SAMPLE_RULES,
),
OrderType.MIP_DNA: RuleSet(
case_rules=MIP_DNA_CASE_RULES, case_sample_rules=MIP_DNA_CASE_SAMPLE_RULES
),
OrderType.MIP_RNA: RuleSet(
case_rules=MIP_RNA_CASE_RULES,
case_sample_rules=MIP_RNA_CASE_SAMPLE_RULES,
),
OrderType.PACBIO_LONG_READ: RuleSet(
sample_rules=PACBIO_LONG_READ_SAMPLE_RULES,
),
OrderType.RML: RuleSet(
sample_rules=RML_SAMPLE_RULES,
),
OrderType.RNAFUSION: RuleSet(
case_rules=RNAFUSION_CASE_RULES,
case_sample_rules=RNAFUSION_CASE_SAMPLE_RULES,
),
OrderType.SARS_COV_2: RuleSet(
sample_rules=MUTANT_SAMPLE_RULES,
),
OrderType.TAXPROFILER: RuleSet(
sample_rules=TAXPROFILER_SAMPLE_RULES,
),
OrderType.TOMTE: RuleSet(
case_rules=TOMTE_CASE_RULES,
case_sample_rules=TOMTE_CASE_SAMPLE_RULES,
),
}

ORDER_TYPE_MODEL_MAP: dict[OrderType, type[Order]] = {
OrderType.BALSAMIC: BalsamicOrder,
OrderType.BALSAMIC_UMI: BalsamicUmiOrder,
OrderType.FASTQ: FastqOrder,
OrderType.FLUFFY: FluffyOrder,
OrderType.METAGENOME: MetagenomeOrder,
OrderType.MICROBIAL_FASTQ: MicrobialFastqOrder,
OrderType.MICROSALT: MicrosaltOrder,
OrderType.MIP_DNA: MipDnaOrder,
OrderType.MIP_RNA: MipRnaOrder,
OrderType.PACBIO_LONG_READ: PacbioOrder,
OrderType.RML: RmlOrder,
OrderType.RNAFUSION: RnaFusionOrder,
OrderType.SARS_COV_2: MutantOrder,
OrderType.TAXPROFILER: TaxprofilerOrder,
OrderType.TOMTE: TomteOrder,
}
Loading

0 comments on commit 0b0b273

Please sign in to comment.