예제 #1
0
        public static async Task <User.User> RetrieveUser(string email)
        {
            var user = new User.User();

            try
            {
                var dynamoDbClient = DynamoDBClientCreator.CreateClient();

                var request = new ScanRequest
                {
                    TableName                 = TableNames.user,
                    FilterExpression          = "email = :email",
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                    {
                        { ":email", new AttributeValue(email) }
                    }
                };

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

                var userItm = response.Items[0];

                user = User.User.deserialiseAsUser(userItm);
            }
            catch (Exception e)
            {
                LambdaLogger.Log("Unable to retrieve codes: " + e.ToString());
            }

            return(user);
        }
예제 #2
0
        public static async Task <Dictionary <string, string> > GetCodes()
        {
            var authCodes = new Dictionary <string, string>();

            try
            {
                var dynamoDbClient = DynamoDBClientCreator.CreateClient();

                var request = new ScanRequest
                {
                    TableName = TableNames.organisation
                };
                var allDocs = await dynamoDbClient.ScanAsync(request).ConfigureAwait(true);

                foreach (var item in allDocs.Items)
                {
                    if (item.ContainsKey("authCode"))
                    {
                        authCodes.Add(item["organisationId"].S, item["authCode"].S);
                    }
                }
            }
            catch (Exception e)
            {
                LambdaLogger.Log("Unable to retrieve codes: " + e.ToString());
            }

            return(authCodes);
        }
예제 #3
0
        public static async Task <List <string> > RetrieveInstallManagers(string organisationId)
        {
            var installerManagers = new List <string>();

            try
            {
                var dynamoDbClient = DynamoDBClientCreator.CreateClient();

                var request = new ScanRequest
                {
                    TableName                 = TableNames.user,
                    FilterExpression          = "organisationId = :organisationId AND accountType = :accountType",
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                    {
                        { ":organisationId", new AttributeValue(organisationId) },
                        { ":accountType", new AttributeValue(UserTypes.installManager) }
                    }
                };

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

                LambdaLogger.Log("GetInstallers:: recieved " + response.Items.Count + " installermanagers from table");

                foreach (var item in response.Items)
                {
                    var user = User.User.deserialiseAsUser(item);
                    installerManagers.Add(user.name);
                }
            }
            catch (Exception e)
            {
                LambdaLogger.Log("Unable to retrieve installmanagers: " + e.ToString());
            }

            return(installerManagers);
        }
예제 #4
0
        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);
        }
예제 #5
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);
        }
예제 #6
0
        public static async Task <User.User> CreateNewUser(Dictionary <string, string> input)
        {
            var user = new User.User();
            //Check user exists
            var dynamoDbClient = DynamoDBClientCreator.CreateClient();

            try
            {
                var request = new ScanRequest
                {
                    TableName                 = TableNames.user,
                    FilterExpression          = "email = :email",
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue>()
                    {
                        { ":email", new AttributeValue(input["email"]) }
                    }
                };

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

                var userItm = response.Items[0];

                user = User.User.deserialiseAsUser(userItm);
                return(user);
            }
            catch (Exception e)
            {
            }

            var userId = Guid.NewGuid().ToString();

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


                foreach (var value in input)
                {
                    item.Add(value.Key, new AttributeValue(value.Value));
                }

                item.Add("userId", new AttributeValue(userId));

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

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

            user.userId         = userId;
            user.accountType    = input["accountType"];
            user.email          = input["email"];
            user.name           = input["name"];
            user.organisationId = input["organisationId"];

            return(user);
        }