-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserStore.cs
130 lines (104 loc) · 4.8 KB
/
UserStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
namespace ColleagueTracker
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
public static class UserStore
{
public static int StoreAndCompare(string storefilename, string deltafilename, List<UserInfo> currentUsers)
{
int deltas = 0;
// read current records
List<string> previousUsers = new List<string>();
if (File.Exists(storefilename))
{
StreamReader usersReader = new StreamReader(storefilename);
previousUsers = JsonConvert.DeserializeObject<List<string>>(usersReader.ReadToEnd());
usersReader.Close();
}
List<string> deltaUsers = new List<string>();
if (File.Exists(deltafilename))
{
StreamReader deltasReader = new StreamReader(deltafilename);
deltaUsers = JsonConvert.DeserializeObject<List<string>>(deltasReader.ReadToEnd());
deltasReader.Close();
}
// compare
// find new hires
foreach (UserInfo currentUser in currentUsers)
{
if(!previousUsers.Contains(currentUser.Login))
{
Console.Write("- NEW: ");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(currentUser.Name);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" is a ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(currentUser.JobTitle);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" managed by ");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write(ADServices.GetUserInfo(currentUser.Manager).Name);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" in ");
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(currentUser.Department);
Console.ForegroundColor = ConsoleColor.Gray;
deltaUsers.Add(string.Format("joined, {0}, {1}", currentUser.Login, DateTime.Now.ToShortDateString()));
deltas++;
}
}
// find those who left the company or moved to another team
foreach (string user in previousUsers)
{
if (!currentUsers.Any(cu => cu.Login == user))
{
UserInfo uinfo = ADServices.GetUserInfo(user);
if (uinfo == null)
{
Console.WriteLine("- LEFT COMPANY: " + user);
}
else
{
Console.Write("- MOVED: ");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(uinfo.Name);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" is a ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(uinfo.JobTitle);
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" managed by ");
Console.ForegroundColor = ConsoleColor.Magenta;
if(uinfo.Manager.Contains(",") && uinfo.Manager.Contains("="))
{
Console.Write(uinfo.Manager.Split(',')[0].Split('=')[1]);
}
else
{
Console.Write(uinfo.Manager);
}
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(" in ");
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(uinfo.Department);
Console.ForegroundColor = ConsoleColor.Gray;
}
deltaUsers.Add(string.Format("left, {0}, {1}", user, DateTime.Now.ToShortDateString()));
deltas++;
}
}
// write updates
StreamWriter usersWriter = new StreamWriter(storefilename);
usersWriter.Write(JsonConvert.SerializeObject(currentUsers.Select(cu => cu.Login).ToList()));
usersWriter.Close();
StreamWriter deltaWriter = new StreamWriter(deltafilename);
deltaWriter.Write(JsonConvert.SerializeObject(deltaUsers));
deltaWriter.Close();
return deltas;
}
}
}