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

fix: Add customerId in user table #9

Merged
merged 21 commits into from
Jan 25, 2024
3 changes: 2 additions & 1 deletion build/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ import { ProjectService } from './projects/services/ProjectService';
import { OrganizationService } from './organizations/services/OrganizationService';
import { updateUserService } from './users/services/updateUserService';
import { UserService } from './usersProject/UserService';
export { createUserCollectionWithValidation, connectToDatabase, checkConnectionStatus, subscribeToDatabaseEvents, createEntityCollectionWithValidation, createProjectCollectionWithValidation, createOrganizationCollectionWithValidation, deleteUserService, getOneUserService, getUsersService, createUserService, updateUserService, getUserByEmail, UserService, OrganizationService, EntityService, ProjectService, UserModel, entityModel, organizationModel, IUsers, IUser, IEntityDb, IProjectDb, projectModel, IOrganizationDb, IMemberDb, };
import { updateCustomerId } from './users/services/updateCustomerIdService';
export { createUserCollectionWithValidation, connectToDatabase, checkConnectionStatus, subscribeToDatabaseEvents, createEntityCollectionWithValidation, createProjectCollectionWithValidation, createOrganizationCollectionWithValidation, deleteUserService, getOneUserService, getUsersService, createUserService, updateUserService, getUserByEmail, updateCustomerId, UserService, OrganizationService, EntityService, ProjectService, UserModel, entityModel, organizationModel, IUsers, IUser, IEntityDb, IProjectDb, projectModel, IOrganizationDb, IMemberDb, };
4 changes: 3 additions & 1 deletion build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions build/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export interface IUsers {
complement: string;
quota: UserQuota;
validatedMail: boolean;
paymentInfo: {
customerId: string;
plan: string;
expirationDate: Date;
};
[key: string]: any;
}
export interface IEntityDb {
Expand Down
2 changes: 1 addition & 1 deletion build/projects/validationProjectsDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function createProjectCollectionWithValidation(uri, databaseName, collection_pro
items: { bsonType: 'string' },
description: 'List of associated BIM entities (string array)',
},
phtogrammetry: {
photogrammetry: {
bsonType: 'array',
items: { bsonType: 'string' },
description: 'Photogrammetry entity [unique] (string array)',
Expand Down
5 changes: 5 additions & 0 deletions build/users/services/createUserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ function createUserService(user) {
pointCloudUsedMB: 0,
photogrammetryUsedMB: 0,
},
paymentInfo: {
customerId: user.paymentInfo.customerId,
plan: user.paymentInfo.plan,
expirationDate: user.paymentInfo.expirationDate,
},
});
const savedUser = yield newUser.save();
return savedUser;
Expand Down
2 changes: 2 additions & 0 deletions build/users/services/updateCustomerIdService.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { IUsers } from '../../interfaces';
export declare function updateCustomerId(email: string, updates: Partial<IUsers['paymentInfo']>): Promise<IUsers | null>;
42 changes: 42 additions & 0 deletions build/users/services/updateCustomerIdService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateCustomerId = void 0;
const duna_web_platform_error_defs_1 = require("duna-web-platform-error-defs");
const validationUserModel_1 = require("../validationUserModel");
function updateCustomerId(email, updates) {
return __awaiter(this, void 0, void 0, function* () {
try {
const filter = { email };
const update = {
$set: {
'paymentInfo.customerId': updates.customerId,
'paymentInfo.plan': updates.plan,
'paymentInfo.expirationDate': updates.expirationDate,
},
};
const options = { new: true };
const updatedUser = yield validationUserModel_1.UserModel.findOneAndUpdate(filter, update, options);
if (!updatedUser) {
const err = duna_web_platform_error_defs_1.ErrorMessages.UserNotFound.Message;
throw err;
}
return updatedUser;
}
catch (error) {
console.error('Error in updateCustomerId:', error);
const err = duna_web_platform_error_defs_1.ErrorMessages.InternalServerError;
err.Details = 'Error updating user data.';
throw err;
}
});
}
exports.updateCustomerId = updateCustomerId;
9 changes: 9 additions & 0 deletions build/users/services/updateUserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ function updateUserService(userId, updates) {
existingUser.complement = updates.complement;
if (updates.quota)
existingUser.quota = updates.quota;
if (updates.paymentInfo) {
if (updates.paymentInfo.plan) {
existingUser.paymentInfo.plan = updates.paymentInfo.plan;
}
if (updates.paymentInfo.expirationDate) {
existingUser.paymentInfo.expirationDate =
updates.paymentInfo.expirationDate;
}
}
yield existingUser.save();
return existingUser;
}
Expand Down
5 changes: 5 additions & 0 deletions build/users/validationUserModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const UserSchema = new mongoose_1.default.Schema({
BIMUsedMB: { type: Number, required: true },
photogrammetryUsedMB: { type: Number, required: true },
},
paymentInfo: {
customerId: { type: String, required: false, default: '' },
plan: { type: String, required: false, default: '' },
expirationDate: { type: String, required: false, default: '' },
},
validatedMail: { type: Boolean, required: true, default: false },
});
const UserModel = mongoose_1.default.model('user', UserSchema);
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { ProjectService } from './projects/services/ProjectService'
import { OrganizationService } from './organizations/services/OrganizationService'
import { updateUserService } from './users/services/updateUserService'
import { UserService } from './usersProject/UserService'
import { updateCustomerId } from './users/services/updateCustomerIdService'

export {
createUserCollectionWithValidation,
Expand All @@ -40,6 +41,7 @@ export {
createUserService,
updateUserService,
getUserByEmail,
updateCustomerId,
UserService,
OrganizationService,
EntityService,
Expand Down
5 changes: 5 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export interface IUsers {
complement: string
quota: UserQuota
validatedMail: boolean
paymentInfo: {
customerId: string
plan: string
expirationDate: Date
}
[key: string]: any
}

Expand Down
2 changes: 1 addition & 1 deletion src/projects/validationProjectsDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function createProjectCollectionWithValidation(
description:
'List of associated BIM entities (string array)',
},
phtogrammetry: {
photogrammetry: {
bsonType: 'array',
items: { bsonType: 'string' },
description:
Expand Down
5 changes: 5 additions & 0 deletions src/users/services/createUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export async function createUserService(user: IUsers): Promise<IUsers> {
pointCloudUsedMB: 0,
photogrammetryUsedMB: 0,
},
paymentInfo: {
customerId: user.paymentInfo.customerId,
plan: user.paymentInfo.plan,
expirationDate: user.paymentInfo.expirationDate,
},
})

const savedUser = await newUser.save()
Expand Down
39 changes: 39 additions & 0 deletions src/users/services/updateCustomerIdService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ErrorMessages } from 'duna-web-platform-error-defs'
import { IUsers } from '../../interfaces'
import { UserModel } from '../validationUserModel'

export async function updateCustomerId(
email: string,
updates: Partial<IUsers['paymentInfo']>
): Promise<IUsers | null> {
try {
const filter = { email }
const update = {
$set: {
'paymentInfo.customerId': updates.customerId,
'paymentInfo.plan': updates.plan,
'paymentInfo.expirationDate': updates.expirationDate,
},
}

const options = { new: true }

const updatedUser = await UserModel.findOneAndUpdate(
filter,
update,
options
)

if (!updatedUser) {
const err = ErrorMessages.UserNotFound.Message
throw err
}

return updatedUser
} catch (error) {
console.error('Error in updateCustomerId:', error)
const err = ErrorMessages.InternalServerError
err.Details = 'Error updating user data.'
throw err
}
}
10 changes: 10 additions & 0 deletions src/users/services/updateUserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ export async function updateUserService(
if (updates.complement) existingUser.complement = updates.complement
if (updates.quota) existingUser.quota = updates.quota

if (updates.paymentInfo) {
if (updates.paymentInfo.plan) {
existingUser.paymentInfo.plan = updates.paymentInfo.plan
}
if (updates.paymentInfo.expirationDate) {
existingUser.paymentInfo.expirationDate =
updates.paymentInfo.expirationDate
}
}

await existingUser.save()

return existingUser
Expand Down
5 changes: 5 additions & 0 deletions src/users/validationUserModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const UserSchema = new mongoose.Schema<IUsers>({
BIMUsedMB: { type: Number, required: true },
photogrammetryUsedMB: { type: Number, required: true },
},
paymentInfo: {
customerId: { type: String, required: false, default: '' },
plan: { type: String, required: false, default: '' },
expirationDate: { type: String, required: false, default: '' },
},
validatedMail: { type: Boolean, required: true, default: false },
})

Expand Down
Loading