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

[IPCT1-754] - migrate phone number to app_user #280

Merged
merged 3 commits into from
May 17, 2022
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
4 changes: 1 addition & 3 deletions packages/api/src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ class UserController {
children,
avatarMediaId,
avatarMediaPath,
trust: {
phone,
},
phone,
},
overwrite,
recover
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const AWS = require('aws-sdk');

module.exports = {
async up(queryInterface, Sequelize) {
if (process.env.NODE_ENV === 'test') {
return;
}

const query = `
UPDATE app_user
SET "phone" = "user".phone
FROM (
select "userAddress", phone
from app_user_through_trust inner join app_user_trust aut on aut.id = app_user_through_trust."appUserTrustId"
) "user"
WHERE "app_user"."address" = "user"."userAddress"`;

await queryInterface.sequelize.query(query, {
raw: true,
type: Sequelize.QueryTypes.UPDATE,
});
},

down(queryInterface, Sequelize) {},
};
91 changes: 17 additions & 74 deletions packages/core/src/services/app/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export default class UserService {
public static appUser = models.appUser;
public static beneficiary = models.beneficiary;
public static manager = models.manager;
public static appUserTrust = models.appUserTrust;
public static appUserThroughTrust = models.appUserThroughTrust;
public static appMediaContent = models.appMediaContent;
public static appMediaThumbnail = models.appMediaThumbnail;
Expand All @@ -50,8 +49,8 @@ export default class UserService {
if (overwrite) {
await this.overwriteUser(user);
} else if (!exists) {
const existsPhone = user.trust?.phone
? await this.existsAccountByPhone(user.trust.phone)
const existsPhone = user.phone
? await this.existsAccountByPhone(user.phone, user.address)
: false;

if (existsPhone)
Expand All @@ -69,19 +68,7 @@ export default class UserService {
if (!exists) {
// create new user, including their phone number information
userFromRegistry = (
await this.appUser.create(
user,
user.trust?.phone
? {
include: [
{
model: models.appUserTrust,
as: 'trust',
},
],
}
: {}
)
await this.appUser.create(user)
).toJSON() as AppUser;
} else {
if (user.pushNotificationToken) {
Expand Down Expand Up @@ -179,19 +166,11 @@ export default class UserService {
public static async overwriteUser(user: AppUserCreationAttributes) {
try {
const usersToInactive = await this.appUser.findAll({
include: [
{
model: this.appUserTrust,
as: 'trust',
where: {
phone: user.trust?.phone,
},
},
],
where: {
address: {
[Op.not]: user.address,
},
phone: user.phone,
},
});

Expand Down Expand Up @@ -259,45 +238,11 @@ export default class UserService {
phone?: string
): Promise<IUserHello> {
const user = await this.appUser.findOne({
include: [
{
model: this.appUserTrust,
as: 'trust',
},
],
where: { address },
});
if (user === null) {
throw new BaseError('USER_NOT_FOUND', address + ' user not found!');
}
if (phone) {
const uu = user.toJSON() as AppUser;
const userTrustId =
uu.trust && uu.trust.length > 0 ? uu.trust[0].id : undefined;
if (userTrustId === undefined) {
try {
await this.sequelize.transaction(async (t) => {
const userTrust = await this.appUserTrust.create(
{
phone,
},
{ transaction: t }
);
await this.appUserThroughTrust.create(
{
userAddress: address,
appUserTrustId: userTrust.id,
},
{ transaction: t }
);
});
} catch (e) {
Logger.error(
'creating trust profile to existing account ' + e
);
}
}
}
return UserService.loadUser(user);
}

Expand Down Expand Up @@ -440,23 +385,21 @@ export default class UserService {
return exists !== null;
}

public static async existsAccountByPhone(phone: string): Promise<boolean> {
const query = `
SELECT app_user_trust.phone, address
FROM app_user_trust
LEFT JOIN app_user_through_trust ON "appUserTrustId" = id
LEFT JOIN "app_user" as "user" ON "user".address = "userAddress"
WHERE app_user_trust.phone = :phone
AND "user".active = TRUE`;

const exists = await sequelize.query(query, {
type: QueryTypes.SELECT,
replacements: {
public static async existsAccountByPhone(
phone: string,
address: string
): Promise<boolean> {
const user = await models.appUser.findOne({
where: {
phone,
},
});
address: {
[Op.not]: address,
},
active: true
}
})

return exists.length > 0;
return !!user;
}

public static async updateLastLogin(id: number): Promise<void> {
Expand Down
11 changes: 2 additions & 9 deletions packages/core/src/services/ubi/community/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,12 @@ export class CommunityListService {
const communityProposals = await this._getOpenProposals();
if (query.ambassadorAddress) {
const ambassador = (await models.appUser.findOne({
attributes: [],
include: [
{
model: models.appUserTrust,
as: 'trust',
attributes: ['phone'],
},
],
attributes: ['phone'],
where: {
address: query.ambassadorAddress,
},
})) as any;
const phone = ambassador?.trust[0]?.phone;
const phone = ambassador?.phone;
if (phone) {
const parsePhone = parsePhoneNumber(phone);
extendedWhere = {
Expand Down
14 changes: 2 additions & 12 deletions packages/core/tests/factories/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ const data = async (props?: ICreateProps) => {
gender: props?.gender ? props?.gender : 'u',
pushNotificationToken: '',
suspect: props?.suspect ? props.suspect : false,
trust: {
phone: props?.phone ? props.phone : faker.phone.phoneNumber(),
},
phone: props?.phone ? props.phone : faker.phone.phoneNumber(),
active: props?.active,
year: props?.year,
};
Expand All @@ -54,15 +52,7 @@ const UserFactory = async (
const result: AppUser[] = [];
for (let index = 0; index < options.n; index++) {
const newUser = (await AppUserModel.create(
await data(options.props ? options.props[index] : undefined),
{
include: [
{
model: AppUserTrustModel,
as: 'trust',
},
],
} as any
await data(options.props ? options.props[index] : undefined)
)) as AppUserModel; // use any :facepalm:
result.push(newUser.toJSON() as AppUser);
}
Expand Down
Loading