feat: normalize add segment api
This commit is contained in:
parent
b9ace172a1
commit
f10d617689
@ -24,6 +24,9 @@ export class ApiStoreChangesCommand
|
||||
extends ApiCallBase
|
||||
implements ICommand<Promise<{ data: HashChangeResponse[] } | undefined>>
|
||||
{
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
async execute(): Promise<{ data: HashChangeResponse[] } | undefined> {
|
||||
await this.initTokenData();
|
||||
if (!this.storeUrl) return;
|
||||
|
@ -21,7 +21,7 @@ import { ICommand } from '../../../../common/model/shared/common.dto';
|
||||
import { fnConsoleLog } from '../../../../common/fn/fn-console';
|
||||
|
||||
export class ApiStoreCommitCommand extends ApiCallBase implements ICommand<Promise<boolean>> {
|
||||
constructor(private tx: string) {
|
||||
constructor(private tx: BeginTxResponse) {
|
||||
super();
|
||||
}
|
||||
async execute(): Promise<boolean> {
|
||||
@ -29,7 +29,7 @@ export class ApiStoreCommitCommand extends ApiCallBase implements ICommand<Promi
|
||||
if (!this.storeUrl) return false;
|
||||
try {
|
||||
const resp = await FetchService.fetch<BeginTxResponse>(
|
||||
`${this.storeUrl}/api/v1/tx/${this.tx}/commit`,
|
||||
`${this.storeUrl}/api/v1/tx/${this.tx.tx}/commit`,
|
||||
{
|
||||
type: 'TEXT',
|
||||
headers: this.getAuthHeaders()
|
||||
|
@ -16,7 +16,6 @@
|
||||
*/
|
||||
export interface BeginTxResponse {
|
||||
tx: string;
|
||||
redirectAddress?: string;
|
||||
locked: boolean;
|
||||
lockedBy?: string;
|
||||
lockExpire: number;
|
||||
|
@ -19,6 +19,7 @@ import { ApiCallBase } from '../../api-call.base';
|
||||
import { FetchService } from '@pinmenote/fetch-service';
|
||||
import { ICommand, ServerErrorDto } from '../../../../../common/model/shared/common.dto';
|
||||
import { ApiErrorCode } from '../../../../../common/model/shared/api.error-code';
|
||||
import { BeginTxResponse } from '../api-store.model';
|
||||
|
||||
export interface ObjAddRequest {
|
||||
type: ObjTypeDto;
|
||||
@ -30,15 +31,15 @@ export interface ObjAddResponse {
|
||||
serverId: number;
|
||||
}
|
||||
|
||||
export class ApiAddObjCommand extends ApiCallBase implements ICommand<Promise<ObjAddResponse | ServerErrorDto>> {
|
||||
constructor(private obj: ObjDto, private hash: string, private tx: string) {
|
||||
export class ApiObjAddCommand extends ApiCallBase implements ICommand<Promise<ObjAddResponse | ServerErrorDto>> {
|
||||
constructor(private obj: ObjDto, private hash: string, private tx: BeginTxResponse) {
|
||||
super();
|
||||
}
|
||||
async execute(): Promise<ObjAddResponse | ServerErrorDto> {
|
||||
await this.initTokenData();
|
||||
if (!this.storeUrl) return { code: ApiErrorCode.INTERNAL, message: 'ApiStoreAddObjCommand' };
|
||||
const resp = await FetchService.fetch<ObjAddResponse | ServerErrorDto>(
|
||||
`${this.storeUrl}/api/v1/obj/${this.tx}`,
|
||||
`${this.storeUrl}/api/v1/obj/${this.tx.tx}`,
|
||||
{
|
||||
headers: this.getAuthHeaders(),
|
||||
method: 'POST',
|
@ -20,6 +20,7 @@ import { ObjTypeDto } from '../../../../../common/model/obj/obj.dto';
|
||||
import { ICommand, ServerErrorDto } from '../../../../../common/model/shared/common.dto';
|
||||
import { ApiErrorCode } from '../../../../../common/model/shared/api.error-code';
|
||||
import { FetchService } from '@pinmenote/fetch-service';
|
||||
import { BeginTxResponse } from '../api-store.model';
|
||||
|
||||
export interface ObjSingleChange {
|
||||
serverId: number;
|
||||
@ -30,7 +31,7 @@ export interface ObjSingleChange {
|
||||
}
|
||||
|
||||
export class ApiObjGetByHashCommand extends ApiCallBase implements ICommand<Promise<ObjSingleChange | ServerErrorDto>> {
|
||||
constructor(private hash: string) {
|
||||
constructor(private hash: string, private tx: BeginTxResponse) {
|
||||
super();
|
||||
}
|
||||
async execute(): Promise<ObjSingleChange | ServerErrorDto> {
|
||||
|
@ -19,6 +19,7 @@ import { BeginTxResponse } from '../api-store.model';
|
||||
import { FetchService } from '@pinmenote/fetch-service';
|
||||
import { ICommand } from '../../../../../common/model/shared/common.dto';
|
||||
import { SyncHashType } from '../../../sync/sync.model';
|
||||
import { fnConsoleLog } from '../../../../../common/fn/fn-console';
|
||||
|
||||
export interface FileDataDto {
|
||||
parent?: string;
|
||||
@ -28,16 +29,33 @@ export interface FileDataDto {
|
||||
}
|
||||
|
||||
export class ApiSegmentAddCommand extends ApiCallBase implements ICommand<Promise<boolean>> {
|
||||
constructor(private tx: string, private file: string, private data: FileDataDto) {
|
||||
constructor(private tx: BeginTxResponse, private file: string, private data: FileDataDto) {
|
||||
super();
|
||||
}
|
||||
async execute(): Promise<boolean> {
|
||||
await this.initTokenData();
|
||||
if (!this.storeUrl) return false;
|
||||
if (await this.hasSegment()) return true;
|
||||
if (await this.hasSegment()) return this.addRef();
|
||||
return await this.addSegment();
|
||||
}
|
||||
|
||||
async addRef(): Promise<boolean> {
|
||||
if (!this.data.parent) return true;
|
||||
const authHeaders = this.getAuthHeaders(false);
|
||||
const resp = await FetchService.fetch<BeginTxResponse>(
|
||||
`${this.storeUrl!}/api/v1/segment/ref/${this.tx.tx}/${this.data.hash}/${this.data.parent}`,
|
||||
{
|
||||
type: 'TEXT',
|
||||
headers: {
|
||||
...authHeaders
|
||||
}
|
||||
},
|
||||
this.refreshParams()
|
||||
);
|
||||
fnConsoleLog('ApiSegmentAddCommand->addRef', resp);
|
||||
return true;
|
||||
}
|
||||
|
||||
async hasSegment(): Promise<boolean> {
|
||||
const authHeaders = this.getAuthHeaders();
|
||||
const resp = await FetchService.fetch<BeginTxResponse>(
|
||||
@ -63,9 +81,8 @@ export class ApiSegmentAddCommand extends ApiCallBase implements ICommand<Promis
|
||||
formData.append('type', this.data.type.toString());
|
||||
|
||||
const authHeaders = this.getAuthHeaders(false);
|
||||
|
||||
const resp = await FetchService.fetch(
|
||||
`${this.storeUrl!}/api/v1/segment/add/${this.tx}`,
|
||||
`${this.storeUrl!}/api/v1/segment/add/${this.tx.tx}`,
|
||||
{
|
||||
headers: {
|
||||
...authHeaders
|
||||
|
@ -17,7 +17,7 @@
|
||||
import { ICommand, ServerErrorDto } from '../../../../common/model/shared/common.dto';
|
||||
import { ObjDto } from '../../../../common/model/obj/obj.dto';
|
||||
import { fnConsoleLog } from '../../../../common/fn/fn-console';
|
||||
import { ApiAddObjCommand, ObjAddResponse } from '../../api/store/obj/api-add-obj.command';
|
||||
import { ApiObjAddCommand, ObjAddResponse } from '../../api/store/obj/api-obj-add.command';
|
||||
import { BeginTxResponse } from '../../api/store/api-store.model';
|
||||
import { ApiErrorCode } from '../../../../common/model/shared/api.error-code';
|
||||
import { ApiObjGetByHashCommand, ObjSingleChange } from '../../api/store/obj/api-obj-get-by-hash.command';
|
||||
@ -28,7 +28,7 @@ export class SyncObjectCommand implements ICommand<Promise<void>> {
|
||||
async execute(): Promise<void> {
|
||||
fnConsoleLog('SyncObjectCommand', this.tx);
|
||||
if (this.obj.server?.id) return;
|
||||
const resp: ObjAddResponse | ServerErrorDto = await new ApiAddObjCommand(this.obj, this.hash, this.tx.tx).execute();
|
||||
const resp: ObjAddResponse | ServerErrorDto = await new ApiObjAddCommand(this.obj, this.hash, this.tx).execute();
|
||||
if ('serverId' in resp) {
|
||||
return await this.saveServerId(resp.serverId);
|
||||
} else if ('code' in resp && resp.code === ApiErrorCode.SYNC_DUPLICATED_HASH) {
|
||||
@ -38,7 +38,7 @@ export class SyncObjectCommand implements ICommand<Promise<void>> {
|
||||
}
|
||||
|
||||
private async setByHash(): Promise<void> {
|
||||
const resp: ObjSingleChange | ServerErrorDto = await new ApiObjGetByHashCommand(this.hash).execute();
|
||||
const resp: ObjSingleChange | ServerErrorDto = await new ApiObjGetByHashCommand(this.hash, this.tx).execute();
|
||||
if ('serverId' in resp) {
|
||||
await this.saveServerId(resp.serverId);
|
||||
return;
|
||||
|
@ -51,14 +51,14 @@ export class SyncSnapshotCommand implements ICommand<Promise<SyncObjectStatus>>
|
||||
|
||||
private async syncSnapshot(snapshot: PageSnapshotDto, parent: string): Promise<void> {
|
||||
// snapshot->info
|
||||
await new ApiSegmentAddCommand(this.tx.tx, JSON.stringify(snapshot.info), {
|
||||
await new ApiSegmentAddCommand(this.tx, JSON.stringify(snapshot.info), {
|
||||
hash: snapshot.info.hash,
|
||||
parent,
|
||||
type: SyncHashType.PageSnapshotInfoDto,
|
||||
key: TEMP_KEY
|
||||
}).execute();
|
||||
// snapshot->data
|
||||
await new ApiSegmentAddCommand(this.tx.tx, JSON.stringify(snapshot.data), {
|
||||
await new ApiSegmentAddCommand(this.tx, JSON.stringify(snapshot.data), {
|
||||
hash: snapshot.data.hash,
|
||||
parent,
|
||||
type: SyncHashType.PageSnapshotDataDto,
|
||||
@ -74,7 +74,7 @@ export class SyncSnapshotCommand implements ICommand<Promise<SyncObjectStatus>>
|
||||
const segment = await new PageSegmentGetCommand(hash).execute();
|
||||
if (!segment) return;
|
||||
|
||||
const isSynchronized = await new ApiSegmentAddCommand(this.tx.tx, JSON.stringify(segment), {
|
||||
const isSynchronized = await new ApiSegmentAddCommand(this.tx, JSON.stringify(segment), {
|
||||
hash,
|
||||
parent,
|
||||
type: this.convertSegmentTypeSyncHashType(segment.type),
|
||||
|
@ -38,7 +38,7 @@ export class SyncTxHelper {
|
||||
const tx = await BrowserStorage.get<BeginTxResponse | undefined>(ObjectStoreKeys.SYNC_TX);
|
||||
if (!tx) return;
|
||||
fnConsoleLog('SyncServerCommand->commit', tx);
|
||||
await new ApiStoreCommitCommand(tx.tx).execute();
|
||||
await new ApiStoreCommitCommand(tx).execute();
|
||||
await BrowserStorage.remove(ObjectStoreKeys.SYNC_TX);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user