public static async Task <Property.Property[]> RetrieveProperties(string[] vars)
        {
            //this.userTypes.admin + "_" + this.user.organisationId;
            //this.userTypes.installManager + "_" + this.user.userId;
            //this.userTypes.installer + "_" + this.user.userId; (and date)
            var userType = vars[0].Replace("%20", " ");
            var id       = vars[1].Replace("%20", " ");

            var properties = new Property.Property[0];

            try
            {
                var dynamoDbClient = DynamoDBClientCreator.CreateClient();

                var filterExpression          = string.Empty;
                var expressionAttributeValues = new Dictionary <string, AttributeValue>();

                switch (userType)
                {
                case UserTypes.admin:
                    filterExpression = "organisationId = :organisationId";
                    expressionAttributeValues.Add(":organisationId", new AttributeValue(id));
                    break;

                case UserTypes.installer:
                    filterExpression = "contains(installers, :installer)";
                    expressionAttributeValues.Add(":installer", new AttributeValue(id));
                    break;

                case UserTypes.installManager:
                    filterExpression = "installManager = :installManager";
                    expressionAttributeValues.Add(":installManager", new AttributeValue(id));
                    break;
                }

                var request = new ScanRequest
                {
                    TableName                 = TableNames.property,
                    FilterExpression          = filterExpression,
                    ExpressionAttributeValues = expressionAttributeValues
                };

                var response = await dynamoDbClient.ScanAsync(request).ConfigureAwait(true);

                LambdaLogger.Log("GetProperties:: recieved " + response.Items.Count + " properties from table");

                var tempProperties = new List <Property.Property>();
                foreach (var item in response.Items)
                {
                    var property = Property.Property.deserialiseAsProperty(item);
                    tempProperties.Add(property);
                }

                LambdaLogger.Log("GetProperties:: deserialised " + tempProperties.Count + " properties");

                if (userType.Equals(UserTypes.installer))
                {
                    var validProperties = new List <Property.Property>();
                    foreach (var tempProperty in tempProperties)
                    {
                        //surveyDate today or before and survey not complete
                        //installDate today or before and install not complete
                        if ((DateTime.Parse(tempProperty.surveyDate, new CultureInfo("en-GB")) <= DateTime.Now && !tempProperty.surveyComplete) ||
                            (DateTime.Parse(tempProperty.installDate, new CultureInfo("en-GB")) <= DateTime.Now && !tempProperty.installComplete))
                        {
                            validProperties.Add(tempProperty);
                        }
                        LambdaLogger.Log("Test:: property " + tempProperty.address + " fine");
                    }
                    properties = validProperties.ToArray();
                    LambdaLogger.Log("GetProperties:: " + properties.Length + " valid properties for installer");
                }
                else
                {
                    properties = tempProperties.ToArray();
                }
            }
            catch (Exception e)
            {
                LambdaLogger.Log("Unable to retrieve properties: " + e.ToString());
            }

            return(properties);
        }
Exemple #2
0
        public static async Task <Property.Property> CreateNewProperty(Dictionary <string, object> input)
        {
            var property   = new Property.Property();
            var propertyId = Guid.NewGuid().ToString();

            try
            {
                var dynamoDbClient = DynamoDBClientCreator.CreateClient();

                var item = new Dictionary <string, AttributeValue>();

                item.Add("propertyId", new AttributeValue(propertyId));
                item.Add("address", new AttributeValue(input["address"].ToString()));
                item.Add("formsGenerated", new AttributeValue()
                {
                    BOOL = Boolean.Parse(input["formsGenerated"].ToString())
                });
                item.Add("formsSigned", new AttributeValue()
                {
                    BOOL = Boolean.Parse(input["formsSigned"].ToString())
                });
                item.Add("installComplete", new AttributeValue()
                {
                    BOOL = Boolean.Parse(input["installComplete"].ToString())
                });
                item.Add("installDate", new AttributeValue(input["installDate"].ToString()));
                item.Add("installers", new AttributeValue()
                {
                    L = ((JArray)input["installers"]).Select(s => new AttributeValue(s.Value <string>())).ToList()
                });
                item.Add("installManager", new AttributeValue(input["installManager"].ToString()));
                item.Add("measureCategory", new AttributeValue(input["measureCategory"].ToString()));
                item.Add("measureType", new AttributeValue(input["measureType"].ToString()));
                item.Add("organisationId", new AttributeValue(input["organisationId"].ToString()));
                item.Add("postcode", new AttributeValue(input["postcode"].ToString()));
                item.Add("surveyComplete", new AttributeValue()
                {
                    BOOL = Boolean.Parse(input["surveyComplete"].ToString())
                });
                item.Add("surveyDate", new AttributeValue(input["surveyDate"].ToString()));
                item.Add("tenure", new AttributeValue(input["tenure"].ToString()));

                var request = new PutItemRequest
                {
                    TableName = TableNames.property,
                    Item      = item
                };

                await dynamoDbClient.PutItemAsync(request).ConfigureAwait(true);
            }
            catch (Exception e)
            {
                LambdaLogger.Log("Unable to put property: " + e.ToString());
            }

            property.propertyId      = propertyId;
            property.address         = input["address"].ToString();
            property.formsGenerated  = Boolean.Parse(input["formsGenerated"].ToString());
            property.formsSigned     = Boolean.Parse(input["formsSigned"].ToString());
            property.installComplete = Boolean.Parse(input["installComplete"].ToString());
            property.installDate     = input["installDate"].ToString();
            property.installers      = ((JArray)input["installers"]).Select(s => s.Value <string>()).ToArray();
            property.installManager  = input["installManager"].ToString();
            property.measureCategory = input["measureCategory"].ToString();
            property.measureType     = input["measureType"].ToString();
            property.organisationId  = input["organisationId"].ToString();
            property.postcode        = input["postcode"].ToString();
            property.surveyComplete  = Boolean.Parse(input["surveyComplete"].ToString());
            property.surveyDate      = input["surveyDate"].ToString();
            property.tenure          = input["tenure"].ToString();

            return(property);
        }