@fabriktor/fx 0.0.71 → 0.0.72

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import type { GetByUIDOptions, OperationResultOf, TmpPhonesOperator, TmpUsernamesOperator, UsernameVerification, UsersOperator } from "@fabriktor/client";
2
- import type { InAddress, InUser, User } from "@fabriktor/schema";
2
+ import { UserTypeEmployee, UserTypeEmployer, type InAddress, type InUser, type User } from "@fabriktor/schema";
3
3
  import { type RunSearchOptions, type SearchPage } from "../core/search.js";
4
4
  export type ResolveUsernameOptions = Parameters<UsersOperator["resolveUsername"]>[0];
5
5
  export type VerifyUsernameOptions = Parameters<TmpUsernamesOperator["verify"]>[0];
@@ -16,6 +16,17 @@ export type UpdateUserPhoneOptions = GetByUIDOptions & {
16
16
  phone: string;
17
17
  code: string;
18
18
  };
19
+ export type WorkAccountIdentity = typeof UserTypeEmployee | typeof UserTypeEmployer;
20
+ export type SwitchActiveModeOptions = GetByUIDOptions & {
21
+ id: string;
22
+ account_identity: User["account_identity"];
23
+ active_mode: User["active_mode"];
24
+ };
25
+ export type EstablishWorkIdentityOptions = GetByUIDOptions & {
26
+ id: string;
27
+ current_account_identity: User["account_identity"];
28
+ account_identity: WorkAccountIdentity;
29
+ };
19
30
  export type SendPhoneCodeOptions = Parameters<TmpPhonesOperator["send"]>[0];
20
31
  export type VerifyPhoneCodeOptions = Parameters<TmpPhonesOperator["verify"]>[0];
21
32
  export type UsersFXOptions = {
@@ -27,6 +38,8 @@ export interface UsersFXOperator {
27
38
  searchUsers(opts: SearchUsersOptions): Promise<SearchUsersPage>;
28
39
  updateUserProfile(opts: UpdateUserProfileOptions): Promise<User>;
29
40
  updateUserPhone(opts: UpdateUserPhoneOptions): Promise<User>;
41
+ switchActiveMode(opts: SwitchActiveModeOptions): Promise<User>;
42
+ establishWorkIdentity(opts: EstablishWorkIdentityOptions): Promise<User>;
30
43
  resolveUsername(opts: ResolveUsernameOptions): Promise<string>;
31
44
  verifyUsername(opts: VerifyUsernameOptions): Promise<OperationResultOf<UsernameVerification>>;
32
45
  generateUsernames(opts: GenerateUsernamesOptions): Promise<OperationResultOf<UsernameVerification>>;
@@ -41,6 +54,8 @@ export declare class UsersFX implements UsersFXOperator {
41
54
  searchUsers(opts: SearchUsersOptions): Promise<SearchUsersPage>;
42
55
  updateUserProfile(opts: UpdateUserProfileOptions): Promise<User>;
43
56
  updateUserPhone(opts: UpdateUserPhoneOptions): Promise<User>;
57
+ switchActiveMode(opts: SwitchActiveModeOptions): Promise<User>;
58
+ establishWorkIdentity(opts: EstablishWorkIdentityOptions): Promise<User>;
44
59
  resolveUsername(opts: ResolveUsernameOptions): Promise<string>;
45
60
  verifyUsername(opts: VerifyUsernameOptions): Promise<OperationResultOf<UsernameVerification>>;
46
61
  generateUsernames(opts: GenerateUsernamesOptions): Promise<OperationResultOf<UsernameVerification>>;
@@ -3,6 +3,7 @@
3
3
  // Licensed under the Apache License, Version 2.0 (the "License"); you may
4
4
  // not use this file except in compliance with the License. You may obtain
5
5
  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ import { UserTypeClient, UserTypeEmployee, UserTypeEmployer, } from "@fabriktor/schema";
6
7
  import { errRequired, errResolveFailed, errValidation, } from "../core/err.js";
7
8
  import { runSearch, } from "../core/search.js";
8
9
  import { normalizePhone, normalizePhoneCode, validatePhone, validatePhoneCode, } from "./phone.js";
@@ -13,7 +14,10 @@ const msgPhoneCodeRequired = "Verification code is required.";
13
14
  const msgFirstNameRequired = "First name is required.";
14
15
  const msgLastNameRequired = "Last name is required.";
15
16
  const msgAddressRequired = "A complete address is required.";
16
- const msgUserMissingAfterUpdate = "User could not be resolved after the profile update.";
17
+ const msgUserMissingAfterUpdate = "User could not be resolved after the account update.";
18
+ const msgInvalidActiveMode = "Active mode is not available for this account identity.";
19
+ const msgWorkIdentityRequiresClient = "A work identity can only be established from a client account.";
20
+ const msgInvalidWorkIdentity = "Work identity must be employee or employer.";
17
21
  const resourceUsername = "username";
18
22
  const resourceUser = "user";
19
23
  export class UsersFX {
@@ -60,6 +64,36 @@ export class UsersFX {
60
64
  });
61
65
  return await this.updatedUser(opts.uid);
62
66
  }
67
+ async switchActiveMode(opts) {
68
+ validateActiveMode(opts.account_identity, opts.active_mode);
69
+ await this.users.replaceOneUser({
70
+ id: opts.id,
71
+ in: { active_mode: opts.active_mode },
72
+ });
73
+ return await this.updatedUser(opts.uid);
74
+ }
75
+ async establishWorkIdentity(opts) {
76
+ if (opts.current_account_identity !== UserTypeClient) {
77
+ throw errValidation({
78
+ field: "current_account_identity",
79
+ msg: msgWorkIdentityRequiresClient,
80
+ });
81
+ }
82
+ if (!isWorkAccountIdentity(opts.account_identity)) {
83
+ throw errValidation({
84
+ field: "account_identity",
85
+ msg: msgInvalidWorkIdentity,
86
+ });
87
+ }
88
+ await this.users.replaceOneUser({
89
+ id: opts.id,
90
+ in: {
91
+ account_identity: opts.account_identity,
92
+ active_mode: opts.account_identity,
93
+ },
94
+ });
95
+ return await this.updatedUser(opts.uid);
96
+ }
63
97
  async resolveUsername(opts) {
64
98
  const username = normalizeUsername(opts.username);
65
99
  const out = await this.users.resolveUsername({
@@ -211,3 +245,19 @@ function errPhoneCodeRequired(field) {
211
245
  msg: msgPhoneCodeRequired,
212
246
  });
213
247
  }
248
+ function isWorkAccountIdentity(value) {
249
+ return value === UserTypeEmployee || value === UserTypeEmployer;
250
+ }
251
+ function validateActiveMode(accountIdentity, activeMode) {
252
+ if ((accountIdentity === UserTypeEmployer &&
253
+ (activeMode === UserTypeEmployer || activeMode === UserTypeClient)) ||
254
+ (accountIdentity === UserTypeEmployee &&
255
+ (activeMode === UserTypeEmployee || activeMode === UserTypeClient)) ||
256
+ (accountIdentity === UserTypeClient && activeMode === UserTypeClient)) {
257
+ return;
258
+ }
259
+ throw errValidation({
260
+ field: "active_mode",
261
+ msg: msgInvalidActiveMode,
262
+ });
263
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.71",
3
+ "version": "0.0.72",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,7 +17,7 @@
17
17
  "typescript": "^6.0.3"
18
18
  },
19
19
  "dependencies": {
20
- "@fabriktor/client": "0.0.64",
21
- "@fabriktor/schema": "0.0.46"
20
+ "@fabriktor/client": "0.0.65",
21
+ "@fabriktor/schema": "0.0.47"
22
22
  }
23
23
  }