예제 #1
0
파일: code.py 프로젝트: lmullane/sbc-pay
 def find_code_value_by_type_and_code(
         cls,
         code_type: str,
         code: str
 ):
     """Find code values by code type and code."""
     current_app.logger.debug(f'<find_code_value_by_type_and_code : {code_type} - {code}')
     code_response = {}
     if cache.get(code_type):
         filtered_codes = [cd for cd in cache.get(code_type) if cd.get('type') == code or cd.get('code') == code]
         if filtered_codes:
             code_response = filtered_codes[0]
     else:
         if code_type == CodeValue.ERROR.value:
             codes_model = ErrorCode.find_by_code(code)
             error_schema = ErrorCodeSchema()
             code_response = error_schema.dump(codes_model, many=False)
         elif code_type == CodeValue.INVOICE_STATUS.value:
             codes_model = InvoiceStatusCode.find_by_code(code)
             schema = InvoiceStatusCodeSchema()
             code_response = schema.dump(codes_model, many=False)
         elif code_type == CodeValue.CORP_TYPE.value:
             codes_model = CorpType.find_by_code(code)
             schema = CorpTypeSchema()
             code_response = schema.dump(codes_model, many=False)
     current_app.logger.debug('>find_code_value_by_type_and_code')
     return code_response
예제 #2
0
    def find_code_values_by_type(cls, code_type: str):
        """Find code values by code type."""
        current_app.logger.debug(f'<find_code_values_by_type : {code_type}')
        response = {}

        # Get from cache and if still none look up in database
        codes_response = cache.get(code_type)
        if not codes_response:
            if code_type == CodeValue.ERROR.value:
                codes_models = ErrorCode.find_all()
                error_schema = ErrorCodeSchema()
                codes_response = error_schema.dump(codes_models, many=True)
            elif code_type == CodeValue.INVOICE_STATUS.value:
                codes_models = InvoiceStatusCode.find_all()
                code_schema = InvoiceStatusCodeSchema()
                codes_response = code_schema.dump(codes_models, many=True)
            elif code_type == CodeValue.CORP_TYPE.value:
                codes_models = CorpType.find_all()
                code_schema = CorpTypeSchema()
                codes_response = code_schema.dump(codes_models, many=True)
            cache.set(code_type, codes_response)

        response['codes'] = codes_response
        current_app.logger.debug('>find_code_values_by_type')
        return response