Skip to content

Commit

Permalink
files and customers tables fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rotembarsela committed Aug 24, 2024
1 parent 90e5dab commit 3386728
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 10 deletions.
20 changes: 14 additions & 6 deletions mariadb/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@ CREATE TABLE IF NOT EXISTS users
u_name VARCHAR(255) NOT NULL
);

CREATE TABLE IF NOT EXISTS files
(
f_id INT AUTO_INCREMENT PRIMARY KEY,
f_name VARCHAR(255) NOT NULL,
f_path VARCHAR(255) NOT NULL,
f_upload_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
CREATE TABLE IF NOT EXISTS files (
f_id INT AUTO_INCREMENT PRIMARY KEY,
f_name VARCHAR(255) NOT NULL,
f_path VARCHAR(255) NOT NULL,
f_upload_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
f_last_author VARCHAR(255) NULL,
f_author VARCHAR(255) NULL,
f_created_date DATETIME NULL,
f_modified_date DATETIME NULL,
f_application VARCHAR(255) NULL,
f_app_version VARCHAR(50) NULL,
f_doc_security INT NULL DEFAULT 0,
f_scale_crop BOOLEAN NULL DEFAULT FALSE,
f_worksheets INT NULL
);

CREATE TABLE IF NOT EXISTS customers
Expand Down
2 changes: 1 addition & 1 deletion server/src/entities/customer.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class CustomerEntity {
@Column()
c_phone: string;

@ManyToOne(() => FileEntity, (file) => file.customers, {
@ManyToOne(() => FileEntity, (file) => file.f_customers, {
onDelete: 'CASCADE',
nullable: true,
})
Expand Down
29 changes: 28 additions & 1 deletion server/src/entities/file.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ export class FileEntity {
@Column({ type: 'datetime', default: () => 'CURRENT_TIMESTAMP' })
f_upload_date: Date;

@Column({ nullable: true })
f_last_author: string;

@Column({ nullable: true })
f_author: string;

@Column({ type: 'datetime', nullable: true })
f_created_date: Date;

@Column({ type: 'datetime', nullable: true })
f_modified_date: Date;

@Column({ nullable: true })
f_application: string;

@Column({ nullable: true })
f_app_version: string;

@Column({ nullable: true })
f_doc_security: number;

@Column({ nullable: true, default: false })
f_scale_crop: boolean;

@Column({ nullable: true })
f_worksheets: number;

@OneToMany(() => CustomerEntity, (customer) => customer.file)
customers: Relation<CustomerEntity[]>;
f_customers: Relation<CustomerEntity[]>;
}
28 changes: 26 additions & 2 deletions server/src/excel/excel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,31 @@ export class ExcelService {
const filename = path.basename(filePath);
const targetPath = path.join(userUploadDir, filename);

console.log(userUploadDir);

await this.moveFile(filePath, targetPath);

const workbook = readFile(targetPath);
const metadata = workbook.Props;
this.logProps(workbook.Props);

const fileEntity = this.fileRepository.create({
f_name: filename,
f_path: userUploadDir,
f_last_author: metadata?.LastAuthor || '',
f_author: metadata?.Author || '',
f_created_date: metadata?.CreatedDate
? new Date(metadata.CreatedDate)
: null,
f_modified_date: metadata?.ModifiedDate
? new Date(metadata.ModifiedDate)
: null,
f_application: metadata?.Application || '',
f_app_version: metadata?.AppVersion || '',
f_doc_security:
typeof metadata?.DocSecurity === 'number' ? metadata.DocSecurity : 0,
f_scale_crop: metadata?.ScaleCrop || false,
f_worksheets: workbook.SheetNames.length,
});

await this.fileRepository.save(fileEntity);

await this.saveCustomerData(targetPath, fileEntity.f_id);
Expand Down Expand Up @@ -183,6 +200,7 @@ export class ExcelService {
fileId: number,
): Promise<void> {
const workbook = readFile(filePath);
// this.logProps(workbook.Props);
const sheet = workbook.Sheets[workbook.SheetNames[0]];
const rows = utils.sheet_to_json(sheet);

Expand All @@ -207,6 +225,12 @@ export class ExcelService {
}
}

private logProps = (props: any) => {
for (const [key, value] of Object.entries(props)) {
console.log(`${key}: ${value}`);
}
};

private async validateExcelFile(filePath: string): Promise<boolean> {
try {
const workbook = readFile(filePath);
Expand Down

0 comments on commit 3386728

Please sign in to comment.