-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from icefoganalytics/main
Updates from Icefog
- Loading branch information
Showing
16 changed files
with
300 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
api/src/db/migrations/2024.03.07T00.26.44.add-sync-failure-at-concept-to-users-table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { Migration } from "@/db/umzug" | ||
import { MssqlSimpleTypes } from "@/db/utils/mssql-simple-types" | ||
|
||
export const up: Migration = async ({ context: queryInterface }) => { | ||
await queryInterface.addColumn("users", "last_sync_success_at", { | ||
type: MssqlSimpleTypes.DATETIME2(0), | ||
allowNull: true, | ||
}) | ||
await queryInterface.sequelize.query(/* sql */ ` | ||
UPDATE users | ||
SET last_sync_success_at = last_employee_directory_sync_at | ||
`) | ||
await queryInterface.removeColumn("users", "last_employee_directory_sync_at") | ||
|
||
await queryInterface.addColumn("users", "last_sync_failure_at", { | ||
type: MssqlSimpleTypes.DATETIME2(0), | ||
allowNull: true, | ||
}) | ||
} | ||
|
||
export const down: Migration = async ({ context: queryInterface }) => { | ||
await queryInterface.removeColumn("users", "last_sync_failure_at") | ||
|
||
await queryInterface.addColumn("users", "last_employee_directory_sync_at", { | ||
type: MssqlSimpleTypes.DATETIME2(0), | ||
allowNull: true, | ||
}) | ||
await queryInterface.sequelize.query(/* sql */ ` | ||
UPDATE users | ||
SET last_employee_directory_sync_at = last_sync_success_at | ||
`) | ||
await queryInterface.removeColumn("users", "last_sync_success_at") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { isEmpty, isNil } from "lodash" | ||
|
||
import db, { Role, User, UserGroup, UserGroupMembership } from "@/models" | ||
import { DEFAULT_ORDER } from "@/models/user-groups" | ||
|
||
import { Users } from "@/services" | ||
import BaseService from "@/services/base-service" | ||
|
||
type GroupMembershipAttributes = Partial<UserGroupMembership> | ||
type Attributes = Partial<User> & { | ||
groupMembershipAttributes?: GroupMembershipAttributes | ||
} | ||
|
||
export class CreateService extends BaseService { | ||
private attributes: Partial<User> | ||
private groupMembershipAttributes?: GroupMembershipAttributes | ||
|
||
constructor({ groupMembershipAttributes, ...attributes }: Attributes) { | ||
super() | ||
this.attributes = attributes | ||
this.groupMembershipAttributes = groupMembershipAttributes | ||
} | ||
|
||
async perform(): Promise<User> { | ||
const { auth0Subject, email, firstName, lastName, ...optionalAttributes } = this.attributes | ||
|
||
if (isNil(auth0Subject) || isEmpty(auth0Subject)) { | ||
throw new Error("auth0Subject is required") | ||
} | ||
|
||
if (isNil(email) || isEmpty(email)) { | ||
throw new Error("email is required") | ||
} | ||
|
||
if (isEmpty(firstName)) { | ||
throw new Error("firstName is required") | ||
} | ||
|
||
if (isEmpty(lastName)) { | ||
throw new Error("lastName is required") | ||
} | ||
|
||
return db.transaction(async () => { | ||
const user = await User.create({ | ||
auth0Subject, | ||
email, | ||
firstName, | ||
lastName, | ||
...optionalAttributes, | ||
}) | ||
|
||
await Role.create({ | ||
userId: user.id, | ||
role: Role.Types.USER, | ||
}) | ||
|
||
await this.ensureUserGroupMembership(user, this.groupMembershipAttributes) | ||
|
||
return user.reload({ | ||
include: [ | ||
"roles", | ||
{ | ||
association: "groupMembership", | ||
include: ["department", "division", "branch", "unit"], | ||
}, | ||
], | ||
}) | ||
}) | ||
} | ||
|
||
private async ensureUserGroupMembership( | ||
user: User, | ||
groupMembershipAttributes?: GroupMembershipAttributes | ||
) { | ||
if (!isEmpty(groupMembershipAttributes)) { | ||
return UserGroupMembership.create({ | ||
userId: user.id, | ||
...groupMembershipAttributes, | ||
}) | ||
} | ||
|
||
await Users.YukonGovernmentDirectorySyncService.perform(user) | ||
|
||
if (!isEmpty(user.groupMembership)) { | ||
return user.groupMembership | ||
} | ||
|
||
const [defaultGroup] = await UserGroup.findOrCreate({ | ||
where: { | ||
type: UserGroup.Types.DEPARTMENT, | ||
name: "Unassigned", | ||
}, | ||
defaults: { | ||
name: "Unassigned", | ||
type: UserGroup.Types.DEPARTMENT, | ||
order: DEFAULT_ORDER, | ||
}, | ||
}) | ||
|
||
return UserGroupMembership.create({ | ||
userId: user.id, | ||
departmentId: defaultGroup.id, | ||
}) | ||
} | ||
} | ||
|
||
export default CreateService |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { CreateService } from "./create-service" | ||
export { YukonGovernmentDirectorySyncService } from "./yukon-government-directory-sync-service" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.