Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(4123): filter invalid IDIR users from search #4192

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions app/components/users/UserAutocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,22 @@ export default function UserAutocomplete({ onSelect }: { onSelect: (user?: User)
const [data, setData] = useState<User[]>([]);
const [value, setValue] = useState<User>();
const [searching, setSearching] = useState(false);
const throttled = useRef(_throttle((query: string) => searchUsers(query), 500, { trailing: true }));

const fetchOptions = (query: string) => {
setLoading(true);

throttled
.current(query)
?.then((result) => {
const throttled = useRef(
_throttle(
async (query: string) => {
setLoading(true);
const result = await searchUsers(query);
setData(result.data);
setLoading(false);
})
.catch(() => {});
return result.data;
},
500,
{ trailing: true },
),
);

const fetchOptions = (query: string) => {
throttled.current(query);
};

const options = (data || []).map((item) => (
Expand Down Expand Up @@ -121,16 +125,16 @@ export default function UserAutocomplete({ onSelect }: { onSelect: (user?: User)
</div>
</Combobox.Target>

{data?.length ? (
<Combobox.Dropdown className="max-h-80 overflow-y-scroll z-50">
<Combobox.Options>{options}</Combobox.Options>
</Combobox.Dropdown>
) : (
{loading || data?.length === 0 ? (
<Combobox.Dropdown className="">
<Combobox.Options>
<Combobox.Empty>No results found</Combobox.Empty>
</Combobox.Options>
</Combobox.Dropdown>
) : (
<Combobox.Dropdown className="max-h-80 overflow-y-scroll z-50">
<Combobox.Options>{options}</Combobox.Options>
</Combobox.Dropdown>
)}
</Combobox>
);
Expand Down
31 changes: 18 additions & 13 deletions app/helpers/mock-users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,24 @@ export const proxyUsers: MsUser[] = mockFile.mocks.find(
(mock: MockResponse) => mock.request.url === 'https://graph.microsoft.com/v1.0/users?$filter*',
)?.response.body.value;

export const mockUsers = proxyUsers.map((usr) => {
const { firstName, lastName, email, ministry, idir, upn } = processMsUser(usr);
return {
firstName,
lastName,
displayName: formatFullName({ firstName, lastName }),
email,
ministry,
idir,
upn,
roles: _compact([usr.jobTitle]),
} as AppUserWithRoles;
});
export const mockUsers = proxyUsers
.map((usr) => {
const appUser = processMsUser(usr);
if (!appUser) return null;

const { firstName, lastName, email, ministry, idir, upn } = appUser;
return {
firstName,
lastName,
displayName: formatFullName({ firstName, lastName }),
email,
ministry,
idir,
upn,
roles: _compact([usr.jobTitle]),
} as AppUserWithRoles;
})
.filter((v) => v!) as AppUserWithRoles[];

export const mockRoleUsers = mockUsers.filter((usr) => usr.roles.length > 0);
export const mockNoRoleUsers = mockUsers.filter((usr) => usr.roles.length === 0);
Expand Down
10 changes: 5 additions & 5 deletions app/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,11 @@ model Billing {
}

type PublicCloudDataChanges {
profileChanged Boolean
contactsChanged Boolean
membersChanged Boolean
budgetChanged Boolean
billingChanged Boolean
profileChanged Boolean?
contactsChanged Boolean?
membersChanged Boolean?
budgetChanged Boolean?
billingChanged Boolean?
parentPaths String[]
}

Expand Down
27 changes: 16 additions & 11 deletions app/services/db/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Prisma } from '@prisma/client';
import { Prisma, User } from '@prisma/client';
import _castArray from 'lodash-es/castArray';
import _compact from 'lodash-es/compact';
import _forEach from 'lodash-es/forEach';
Expand Down Expand Up @@ -77,16 +77,21 @@ export async function getMatchingUserIds(search: string) {

export async function createProxyUsers() {
const dbUsers = await Promise.all(
proxyUsers.map((puser: MsUser) => {
const { displayName, ...clearnUserData } = processMsUser(puser);

clearnUserData.email = clearnUserData.email.toLowerCase();
return prisma.user.upsert({
where: { email: clearnUserData.email },
update: clearnUserData,
create: clearnUserData,
});
}),
proxyUsers
.map((puser: MsUser) => {
const appUser = processMsUser(puser);
if (!appUser) return null;

const { displayName, ...clearnUserData } = appUser;

clearnUserData.email = clearnUserData.email.toLowerCase();
return prisma.user.upsert({
where: { email: clearnUserData.email },
update: clearnUserData,
create: clearnUserData,
});
})
.filter((v) => v!),
);

return dbUsers;
Expand Down
14 changes: 10 additions & 4 deletions app/services/msgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import { M365_URL } from '@/config';
import { parseMinistryFromDisplayName } from '@/helpers/user';
import { MsUser, AppUser } from '@/types/user';
import { callMsGraph, getAccessToken } from './core';
export function processMsUser(user: MsUser): AppUser {

export function processMsUser(user: MsUser): AppUser | null {
const idir = user.onPremisesSamAccountName;
const upn = user.userPrincipalName;

if (!idir || !upn) return null;

return {
providerUserId: user.id,
upn: user.userPrincipalName,
upn,
email: user.mail.toLowerCase(),
idir: user.onPremisesSamAccountName,
idir,
idirGuid: user.extension_85cc52e9286540fcb1f97ed86114a0e5_bcgovGUID,
displayName: user.displayName,
firstName: user.givenName,
Expand Down Expand Up @@ -75,7 +81,7 @@ export async function listUsersByEmail(email: string) {
}

const data = await res.json();
return (data as { value: MsUser[] }).value.map(processMsUser).filter((user) => !!user.upn);
return (data as { value: MsUser[] }).value.map(processMsUser).filter((user) => !!user);
}

export async function getUserByEmail(email: string) {
Expand Down

This file was deleted.